Page 2 of 2
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 7:47 pm
by Lua Hal
tentus wrote:Lua Hal wrote:I'm not sure what you mean, using Lua as a parser.
Also, I re-wrote it trying to make it more efficient, but it still doesn't even do the most simple part.
Code: Select all
function npcparse()
npcdata={}
data={}
for line in love.filesystem.lines("config.txt") do
table.insert(data,line)
end
for i = 1,#data do
if string.lower(string.sub(data[i],0,6)) == "spawnx" then
spawnx=string.sub(data[i],7)
elseif string.lower(string.sub(data[i],0,6)) == "spawny" then
spawny=string.sub(data[i],7)
elseif string.lower(data[i]) == "newnpc(" then
newtab={}
for k = i+1,#data do
if k == ")" then
ending=k
end
end
for h=i+1,ending-1 do
if data[h] == "selling(" then
temptab={}
newtab={temptab}
for x=h,#data do
if data[x] == ")" then
newending=x
end
end
for l = h+1,newending-1 do
table.insert(temptab,data[l])
end
else
for key,score in string.gmatch(data[i], "(%w+);(%d+)") do
newtab[key] = score
end
end
end--endfor
end--endif
end--endnpcparse
npcparse()
function love.draw()
love.graphics.print("X:" .. spawnx,0,12)
love.graphics.print("Y: "..spawny,0,12)
end
end
You have an end in the wrong place. I corrected your indentation and it became obvious. Here's what I made:
Code: Select all
function npcparse()
npcdata={}
data={}
for line in love.filesystem.lines("config.txt") do
table.insert(data,line)
end
for i = 1, #data do
if string.lower(string.sub(data[i],0,6)) == "spawnx" then
spawnx = string.sub(data[i],7)
elseif string.lower(string.sub(data[i],0,6)) == "spawny" then
spawny = string.sub(data[i],7)
elseif string.lower(data[i]) == "newnpc(" then
newtab={}
for k = i+1,#data do
if k == ")" then
ending=k
end
end
for h=i+1,ending-1 do
if data[h] == "selling(" then
temptab={}
newtab={temptab}
for x=h, #data do
if data[x] == ")" then
newending = x
end
end
for l = h+1, newending-1 do
table.insert(temptab,data[l])
end
else
for key,score in string.gmatch(data[i], "(%w+);(%d+)") do
newtab[key] = score
end
end
end
end
end
end
npcparse()
function love.draw()
love.graphics.print("X:" .. spawnx, 0, 12)
love.graphics.print("Y: "..spawny, 0, 12)
end
Did that work when you tried it?
I'm getting a C++ runtime error.
Maybe it wasn't a good idea to save that as my game, Lol.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 8:50 pm
by Robin
Lua Hal wrote:I'm not sure what you mean, using Lua as a parser.
That your code simply has
instead of a while complicated parser.
Config.txt would then look like this:
Code: Select all
spawnx=6
spawny=6
newNPC{name="Shopkeeper",
X=30,
Y=30,
type="Shop",
selling = {1,2,3},
texture="Shopkeeper.png"
}
newNPC{
name="Blacksmith",
X=34,
Y=34,
type="Shop",
selling={4,5,6,7,8,9,10,11,12,13,14,15,16},
texture="Blacksmith.png"
}
The only thing you would need is a function called "newNPC".
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 9:28 pm
by thelinx
Robin wrote:Lua Hal wrote:I'm not sure what you mean, using Lua as a parser.
That your code simply has
instead of a while complicated parser.
Except that wont work because require doesn't take filenames.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 9:49 pm
by bartbes
And it caches, which you probably don't want.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 9:52 pm
by tentus
Robin wrote:Lua Hal wrote:I'm not sure what you mean, using Lua as a parser.
That your code simply has
instead of a while complicated parser.
Config.txt would then look like this:
Code: Select all
spawnx=6
spawny=6
newNPC{name="Shopkeeper",
X=30,
Y=30,
type="Shop",
selling = {1,2,3},
texture="Shopkeeper.png"
}
newNPC{
name="Blacksmith",
X=34,
Y=34,
type="Shop",
selling={4,5,6,7,8,9,10,11,12,13,14,15,16},
texture="Blacksmith.png"
}
The only thing you would need is a function called "newNPC".
In Kurosuke I use this code to load levels:
Code: Select all
love.filesystem.load("levels/"..currentLevel..".lua")()
A level file (level_4.lua for example) would look
something like this:
Code: Select all
-- Level 4 by tentus
-- Created on 01/27/11 23:01:07
levelversion = 110226
killdepth = 9600
song = "level2"
ent.home = {
home:new(8016, 8672),
home:new(8304, 8672)
}
The level file sets a couple of lua variables, including a table of entities (in this case, "home" entities which have x and y coords in their starting parameters). It gets executed by the second set of parenthesis after love.filesystem.load().
I use a very similar chunk of code to load and execute user settings:
Code: Select all
love.filesystem.setIdentity("kurosuke") -- name of our save folder
if love.filesystem.exists("settings.lua") then -- check for a settings config file
love.filesystem.load("settings.lua")() -- execute the settings in it
end
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Thu Jul 14, 2011 1:01 am
by Lua Hal
I don't understand, how would I make newNPC{ a function?
I see how the "parsing" works, but how will I make it work for me?
Appended code:
Code: Select all
function npcparse()
love.filesystem.load("config.txt")()
end
function love.draw()
npcparse()
h=data[1]
love.graphics.print(h["name"])
end
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Thu Jul 14, 2011 10:17 am
by Robin
The other guys are right, you should use love.filesystem.load rather than require.
Lua Hal wrote:I don't understand, how would I make newNPC{ a function?
You don't. You make a function called newNPC:
Code: Select all
function newNPC(t)
-- do something with t
end
Normally you call a function like this:
. But in Lua, when the only argument is a literal table or string, you can do this:
or
.