Page 1 of 1
How do you read from a file?
Posted: Wed Feb 05, 2014 4:38 pm
by deadsource
Hey. How do you read form a file in Love? Quite a newbie question but I just can't figure it out. Also tried searching but didn't seem to find an answer.
An example how to read a file into a two dimensional array would be nice
Re: How do you read from a file?
Posted: Wed Feb 05, 2014 5:03 pm
by Azhukar
Azhukar wrote:You can directly load lua files and use those as data storage.
main.lua
Code: Select all
local level1 = love.filesystem.load("level1.lua")()
print(level1[2][1]) --prints 2
level1.lua:
Code: Select all
local leveldata = {
{1,1,1,1},
{2,0,0,1},
{1,1,1,1},
}
return leveldata
Re: How do you read from a file?
Posted: Wed Feb 05, 2014 5:05 pm
by deadsource
Well it seems I didn't search hard enough. Thanks
Re: How do you read from a file?
Posted: Wed Feb 05, 2014 7:03 pm
by Automatik
Azhukar wrote:Azhukar wrote:You can directly load lua files and use those as data storage.
main.lua
Code: Select all
local level1 = love.filesystem.load("level1.lua")()
print(level1[2][1]) --prints 2
level1.lua:
Code: Select all
local leveldata = {
{1,1,1,1},
{2,0,0,1},
{1,1,1,1},
}
return leveldata
Bad idea. If the user download the file from an untrusted source, it might contain malware.
I think you should do this, if you want to get the raw text from a file:
Code: Select all
text = love.filesystem.read('level.txt')
If you want to put more complicated stuff like arrays, you can use Json(
http://json.luaforge.net/ ) to transform text into objects and vice versa.(Json can't contain code)
Code: Select all
object = json:decode(love.filesystem.read('level.json'))
level.json
Code: Select all
[
[0,0,0,1],
[0,1,0,1],
[0,2,0,1],
[1,1,1,1]
]
Re: How do you read from a file?
Posted: Wed Feb 05, 2014 7:19 pm
by Azhukar
Automatik wrote:Bad idea.
No.
Automatik wrote:If the user download the file from an untrusted source, it might contain malware.
Yes.
Re: How do you read from a file?
Posted: Wed Feb 12, 2014 8:12 pm
by Automatik
Azhukar wrote:No.
Ok, sure, if you don't plan on putting a level editor or custom levels or anything like that, it's a fine option.
Re: How do you read from a file?
Posted: Wed Feb 12, 2014 8:14 pm
by Azhukar
Automatik wrote:Ok, sure, if you don't plan on putting a level editor or custom levels or anything like that, it's a fine option.
Even if you do. It's simple to sandbox a lua chunk.
Re: How do you read from a file?
Posted: Thu Feb 13, 2014 10:44 am
by Robin
On the other hand, you can still get stuck in an infinite loop, even with sandboxing.
Re: How do you read from a file?
Posted: Thu Feb 13, 2014 10:52 am
by slime
Robin wrote:On the other hand, you can still get stuck in an infinite loop, even with sandboxing.
http://lua-users.org/lists/lua-l/2013-01/msg00287.html seems pretty solid.
Re: How do you read from a file?
Posted: Thu Feb 13, 2014 6:21 pm
by kikito
I will leave this here in case it is useful...
https://github.com/kikito/sandbox.lua