Problem using love.filesystem.read

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
lordlight7
Prole
Posts: 6
Joined: Mon Jan 23, 2012 8:39 am

Problem using love.filesystem.read

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

Re: Problem using love.filesystem.read

Post 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.
Help us help you: attach a .love.
lordlight7
Prole
Posts: 6
Joined: Mon Jan 23, 2012 8:39 am

Re: Problem using love.filesystem.read

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problem using love.filesystem.read

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

Re: Problem using love.filesystem.read

Post 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.
Help us help you: attach a .love.
lordlight7
Prole
Posts: 6
Joined: Mon Jan 23, 2012 8:39 am

Re: Problem using love.filesystem.read

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests