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 .
lordlight7
Prole
Posts: 6 Joined: Mon Jan 23, 2012 8:39 am
Post
by lordlight7 » Mon Jan 23, 2012 8:46 am
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
i get the error :
The enemies.ini looks like
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Mon Jan 23, 2012 9:11 am
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.
lordlight7
Prole
Posts: 6 Joined: Mon Jan 23, 2012 8:39 am
Post
by lordlight7 » Mon Jan 23, 2012 9:23 am
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
bartbes
Sex machine
Posts: 4946 Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:
Post
by bartbes » Mon Jan 23, 2012 9:28 am
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.
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Mon Jan 23, 2012 9:33 am
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.
lordlight7
Prole
Posts: 6 Joined: Mon Jan 23, 2012 8:39 am
Post
by lordlight7 » Mon Jan 23, 2012 9:35 am
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
Users browsing this forum: Bing [Bot] , Google [Bot] and 1 guest