Page 1 of 1

Problem using love.filesystem.read

Posted: Mon Jan 23, 2012 8:46 am
by lordlight7
I need to read numbers from a text file to load the enemy table.The first number is the number of enemies so i did :

Code: Select all

enemynumber=love.filesystem.read("enemies.ini")
also tried

Code: Select all

enemynumber=love.filesystem.read("enemies.ini", "*number")
-got this from a lua tutorial
I get no errors here but when i try to

Code: Select all

for i=1,enemynumber do
i get the error :

Code: Select all

for limit must be a number
The enemies.ini looks like

Code: Select all

3
100 200
300 400
500 600

Re: Problem using love.filesystem.read

Posted: Mon Jan 23, 2012 9:11 am
by Robin
You are trying to read the whole file, so that doesn't work.

You could use love.filesystem.lines:

Code: Select all

enemies = {}

for line in love.filesystem.lines("enemies.ini") do
   enemies[#enemies + 1] = line -- and parse the line
end
If you do that, you need to remove the first line (the one that says "3").

Another option is to use Lua:

enemies.lua:

Code: Select all

return {
{100, 200},
{300, 400},
{500, 600},
}
main.lua:

Code: Select all

enemies = love.filesystem.load('enemies.lua')()
Hope that helps.

Re: Problem using love.filesystem.read

Posted: Mon Jan 23, 2012 9:23 am
by lordlight7
The 100;200 and 300;400 and 500;600 are the x and y for my enemies.
So how can i parse the line to get something like

Code: Select all

for line in love.filesystem.lines("enemies.ini") do
   enemy = {} 
   enemy.x = ?
  enemy.y = ? 
  table.insert(enemies,enemy)
end

Re: Problem using love.filesystem.read

Posted: Mon Jan 23, 2012 9:28 am
by bartbes
There are two reasons this goes wrong, first of all, you're reading the entire file, when you really only want to be reading the first line (you should probably take a look at love.filesystem.lines, maybe that'll allow you to skip the number of lines altogether).
And second, the result of a read is always a string, and for wants a number, thankfully, there's tonumber.

EDIT: Posted in the other topic (!), merged them now.

Re: Problem using love.filesystem.read

Posted: Mon Jan 23, 2012 9:33 am
by Robin
You will want to use Lua for that. We all do. Just sayin'.

In the meanwhile, you could use:

Code: Select all

for line in love.filesystem.lines("enemies.ini") do
       local x, y = line:match '(%d*);(%d*)%W*'
       local enemy = {x = tonumber(x), y = tonumber(y)} -- that shouldn't be global
      table.insert(enemies,enemy)
end
See string.match.

But really, using Lua for your enemies file will make things a lot less complicated, especially later on.

Re: Problem using love.filesystem.read

Posted: Mon Jan 23, 2012 9:35 am
by lordlight7
i also tried with io.read

Code: Select all

io.input("enemies.ini/lua")
enemynumber=tonumber(io.read("*number"))
before posting this

thanks for the help anyway,i will try everything later,no time now ..
+karma :3