Loading file with lua - problem

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.
Post Reply
User avatar
Linuus
Prole
Posts: 10
Joined: Tue Jan 01, 2013 10:07 pm

Loading file with lua - problem

Post by Linuus »

Hello! I've created a simple mapeditor for a game I am making. It's just a array/list/table or whatever it's called within another, the index of the first indicating the x coordinate and the one within the y coordinate, and the value is the type of tile.
This is saved to a file, in which every 32 lines represents the content of the "outer" array, each of the lines have 20 numbers seperated by spaces.

Now I want to open this file in the game and asign it to an array again like this:

Code: Select all

map = {}
    i = 1
    file = io.open("/Users/linlin/LÖVE/Game2/map.txt", "r")
    for line in file:lines() do 
        map[i] = {}
        map[i] = split(lista, " ")
        i = i + 1
    end
    file.close()
but the game crashes with the text: "attempt to call global 'split' ( a nil value )"

How would you do this?

Thanks
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Loading file with lua - problem

Post by slime »

Use the love.filesystem module for file reading and writing.

Example:

Code: Select all

for line in love.filesystem.lines("map.txt") do
   [...]
end
User avatar
Linuus
Prole
Posts: 10
Joined: Tue Jan 01, 2013 10:07 pm

Re: Loading file with lua - problem

Post by Linuus »

Still can't get it to work :/
And trying to figure out what's wrong is a nightmare when I can't print the variables.
Is there any way to bring up the a console to which I can print stuff? I'm on OSX, and i think i read somewhere around here that it only worked on Windows... Is that correct?
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Loading file with lua - problem

Post by Boolsheet »

You should be able to start LÖVE from the terminal to get the output of stdout. See the Mac OS X section of Getting Started.
Shallow indentations.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Loading file with lua - problem

Post by Santos »

So if it's started from the terminal, Lua's print function will output to the terminal, so you could do something like:

Code: Select all

function love.love
    a = 123
    print(a)
end
Also, Lua doesn't actually come with a function named "split", perhaps a function from here is what you're looking for?
User avatar
Linuus
Prole
Posts: 10
Joined: Tue Jan 01, 2013 10:07 pm

Re: Loading file with lua - problem

Post by Linuus »

Thank you! I got it to work, it was pretty easy once I got a way to debug it ;)
The problem was that I forgot to make the strings into numbers after loading them.

Yeah i made my own split function after looking at some examples online, I've studied python so I'm used to that function.

Thank you all again for the quick replies :awesome:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot], Google [Bot] and 12 guests