How do you read from a 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.
Post Reply
User avatar
deadsource
Prole
Posts: 2
Joined: Wed Feb 05, 2014 4:34 pm

How do you read from a file?

Post 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 ^^
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: How do you read from a file?

Post 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
User avatar
deadsource
Prole
Posts: 2
Joined: Wed Feb 05, 2014 4:34 pm

Re: How do you read from a file?

Post by deadsource »

Well it seems I didn't search hard enough. Thanks ^^
Automatik
Citizen
Posts: 57
Joined: Sun Feb 17, 2013 7:05 pm

Re: How do you read from a file?

Post 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]
]
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: How do you read from a file?

Post by Azhukar »

Automatik wrote:Bad idea.
No.
Automatik wrote:If the user download the file from an untrusted source, it might contain malware.
Yes.
Automatik
Citizen
Posts: 57
Joined: Sun Feb 17, 2013 7:05 pm

Re: How do you read from a file?

Post 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.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: How do you read from a file?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do you read from a file?

Post by Robin »

On the other hand, you can still get stuck in an infinite loop, even with sandboxing.
Help us help you: attach a .love.
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: How do you read from a file?

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How do you read from a file?

Post by kikito »

I will leave this here in case it is useful... :neko:

https://github.com/kikito/sandbox.lua
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest