How do you read from a file?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- deadsource
- Prole
- Posts: 2
- Joined: Wed Feb 05, 2014 4:34 pm
How do you read from a file?
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
An example how to read a file into a two dimensional array would be nice
Re: How do you read from a file?
Azhukar wrote:You can directly load lua files and use those as data storage.
main.lualevel1.lua:Code: Select all
local level1 = love.filesystem.load("level1.lua")() print(level1[2][1]) --prints 2
Code: Select all
local leveldata = { {1,1,1,1}, {2,0,0,1}, {1,1,1,1}, } return leveldata
- deadsource
- Prole
- Posts: 2
- Joined: Wed Feb 05, 2014 4:34 pm
Re: How do you read from a file?
Well it seems I didn't search hard enough. Thanks
Re: How do you read from a file?
Bad idea. If the user download the file from an untrusted source, it might contain malware.Azhukar wrote:Azhukar wrote:You can directly load lua files and use those as data storage.
main.lualevel1.lua:Code: Select all
local level1 = love.filesystem.load("level1.lua")() print(level1[2][1]) --prints 2
Code: Select all
local leveldata = { {1,1,1,1}, {2,0,0,1}, {1,1,1,1}, } return leveldata
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')
Code: Select all
object = json:decode(love.filesystem.read('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?
No.Automatik wrote:Bad idea.
Yes.Automatik wrote:If the user download the file from an untrusted source, it might contain malware.
Re: How do you read from a file?
Ok, sure, if you don't plan on putting a level editor or custom levels or anything like that, it's a fine option.Azhukar wrote:No.
Re: How do you read from a file?
Even if you do. It's simple to sandbox a lua chunk.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.
- 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?
On the other hand, you can still get stuck in an infinite loop, even with sandboxing.
Help us help you: attach a .love.
- 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?
http://lua-users.org/lists/lua-l/2013-01/msg00287.html seems pretty solid.Robin wrote:On the other hand, you can still get stuck in an infinite loop, even with sandboxing.
- 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?
When I write def I mean function.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 0 guests