How to load values from a .txt file?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

How to load values from a .txt file?

Post by louie999 »

Here's basically what I want to happen:
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"
then on the love.load() I want to "get" those values from the file, how can I do that? :| Thx in advance....

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
Garmelon
Prole
Posts: 19
Joined: Tue Jun 02, 2015 5:25 pm

Re: How to load values from a .txt file?

Post by Garmelon »

I would iterate over every line and "split" the string using regular expressions, like this:

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"
Just make sure not to put a space between your commas and the values inside the text file.
And to put " around your file paths.

Yay, first post!
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: How to load values from a .txt file?

Post by Inny »

If you change your txt file to look like this:

Code: Select all

return {
{w=400,h=300,f="IMAGE_FILE_PATH"},
}
and rename it to .lua, then you just load the file like any code

Code: Select all

local list_of_images = requre 'my_image_files.lua'
User avatar
I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

Re: How to load values from a .txt file?

Post by I~=Spam »

Inny wrote:If you change your txt file to look like this:

Code: Select all

return {
{w=400,h=300,f="IMAGE_FILE_PATH"},
}
and rename it to .lua, then you just load the file like any code

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.
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: How to load values from a .txt file?

Post by Positive07 »

Inny wrote:

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]

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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Re: How to load values from a .txt file?

Post by louie999 »

Garmelon wrote:I would iterate over every line and "split" the string using regular expressions, like this:

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"
Just make sure not to put a space between your commas and the values inside the text file.
And to put " around your file paths.

Yay, first post!
So I tried this:

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
But I always get the error:

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'
I have no idea why that happens, the file does exist but it still gives me that error :huh:

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: How to load values from a .txt file?

Post by Kingdaro »

Is the file in the same directory as your main.lua file?
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Re: How to load values from a .txt file?

Post by louie999 »

Kingdaro wrote:Is the file in the same directory as your main.lua file?
I think no, the main.lua is in C:/Games/ but the txt file is in C:/Games/maps/

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to load values from a .txt file?

Post by zorg »

Then you want that one line to look like this:

Code: Select all

for row in love.filesystem.lines("maps/map1.txt") do
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Re: How to load values from a .txt file?

Post by louie999 »

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
Unfortunately I also tried that but it's the same result:

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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 56 guests