So I have a txt file which contain values such as x, y position of a image and then the path to the image itself, like:
Code: Select all
400,300,"IMAGE_FILE_PATH"
Code: Select all
400,300,"IMAGE_FILE_PATH"
Code: Select all
fun = true
school = true
function isItFun()
if school then
fun = false
end
if not fun then
me:explode()
end
end
Code: Select all
local images = {}
for line in love.filesystem.lines("yourtextfile.txt") do
local x, y, path = string.match(line, "(%d*),(%d*),\"(.*)\"")
table.insert(images, {x = tonumber(x), y = tonumber(y), path = path})
end
--> images[1].x == 400
--> images[1].y == 300
--> images[1].path == "IMAGE_FILE_PATH"
Code: Select all
return {
{w=400,h=300,f="IMAGE_FILE_PATH"},
}
Code: Select all
local list_of_images = requre 'my_image_files.lua'
This is what I would do. I think it will run faster too.Inny wrote:If you change your txt file to look like this:and rename it to .lua, then you just load the file like any codeCode: Select all
return { {w=400,h=300,f="IMAGE_FILE_PATH"}, }
Code: Select all
local list_of_images = requre 'my_image_files.lua'
If you are saving the file to your save directory I would prefere to use [wiki]love.filesystem.load[/wiki]Inny wrote:Code: Select all
local list_of_images = requre 'my_image_files.lua'
Code: Select all
local ok, err = pcall(love.filesystem.load("my_image_files.lua"))
if ok then
ok, err = pcall(ok)
end
if not ok then
print(err)
end
So I tried this:Garmelon wrote:I would iterate over every line and "split" the string using regular expressions, like this:Just make sure not to put a space between your commas and the values inside the text file.Code: Select all
local images = {} for line in love.filesystem.lines("yourtextfile.txt") do local x, y, path = string.match(line, "(%d*),(%d*),\"(.*)\"") table.insert(images, {x = tonumber(x), y = tonumber(y), path = path}) end --> images[1].x == 400 --> images[1].y == 300 --> images[1].path == "IMAGE_FILE_PATH"
And to put " around your file paths.
Yay, first post!
Code: Select all
tiles = {}
function love.load()
for row in love.filesystem.lines("map1.txt") do
local x, y, path = string.match(row, "(%d*),(%d*),\"(.*)\"")
table.insert(tiles,{x = tonumber(x), y = tonumber(y), path = path})
end
for _, k in pairs(tiles) do
love.graphics.newImage(k.path)
end
end
Code: Select all
main.lua:4: Could not open file map1.txt. Does not exist.
Traceback
[C]: in function 'lines'
main.lua:4: in function 'load'
[C] in function 'xpcall'
Code: Select all
fun = true
school = true
function isItFun()
if school then
fun = false
end
if not fun then
me:explode()
end
end
I think no, the main.lua is in C:/Games/ but the txt file is in C:/Games/maps/Kingdaro wrote:Is the file in the same directory as your main.lua file?
Code: Select all
fun = true
school = true
function isItFun()
if school then
fun = false
end
if not fun then
me:explode()
end
end
Code: Select all
for row in love.filesystem.lines("maps/map1.txt") do
Unfortunately I also tried that but it's the same result:zorg wrote:Then you want that one line to look like this:Code: Select all
for row in love.filesystem.lines("maps/map1.txt") do
Code: Select all
main.lua:4: Could not open file maps/map1.txt. Does not exist.
Traceback
[C]: in function 'lines'
main.lua:4: in function 'load'
[C] in function 'xpcall'
Code: Select all
fun = true
school = true
function isItFun()
if school then
fun = false
end
if not fun then
me:explode()
end
end
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 56 guests