Page 1 of 2
I'm new to Lua, and do not understand what I'm doing wrong.
Posted: Tue Jul 12, 2011 10:37 pm
by Lua Hal
tab = {"HI;7","New;8"}
npcdata={}
function love.draw()
for index = 1,# tab do
npcdata[string.sub(tab[index],0,string.find(tab[index],";"))] = string.sub(tab[index],string.find(tab[index],";"))
end
love.graphics.print("Hi:" .. npcdata["HI"])
love.graphics.print("New: " .. npdata["New"])
end
I'm trying to make it set the table npdata as follows:
npcdata{
"HI" = "7"
"New" = "8"
}
It will read from a file with data for a game I'm making.
The file will look like this:
Hi;7
New;8
Obviously with meaningful variables though.
Sorry to be a newb, I don't know where else to go for this.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Tue Jul 12, 2011 11:37 pm
by kraftman
It looks like you only want to perform the operation once, so keep it outside of love.draw, since this is called every frame.
I havnt tested this, but you could try something like:
npcdata = {}
for key, value in string.gmatch(file, "(%a);(%d)") do
npcdata[key] = value
end
where file is the file you're reading it from
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Tue Jul 12, 2011 11:58 pm
by Lua Hal
Thanks, but It is part of a larger parser to read from a file for making NPCs with data:
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
]
newNPC[
name;Sign
X;45
Y;45
type;Dialog
dialog;Pete's Apples
texture;Sign.png
]
That's how the file looks, this is the parser:
Code: Select all
function npcparse()
npcs={}
npcnames={}
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,0,6)) == "spawnx" then
spawnx=string.sub(data,7)
elseif string.lower(string.sub(data,0,6)) == "spawny" then
spawny = string.sub(data,7)
elseif string.lower(string.sub(data,0,7)) == "newnpc[" then
keepparsing=true
for k = i,#data+1 do
if data[k] == "]" then
keepparsing=false
end
if keepparsing == true then
if data[k] == "selling(" then
tab = {}
keepscrolling=true
for h = k+1,#data do
if data[h] == ")" then
keepscrolling = false
end
if keepscrolling == true then
table.insert(tab,data[h])
end
for index = 1,# tab do
npcdata[string.sub(tab[index],0,string.find(tab[index],";"))] = string.sub(tab[index],string.find(tab[index],";"))
end
elseif data[k]
end
end--end if
end --end for
end --end if
end --end for
end --end npcparse()
Yes, my code is messy and probably inefficient, sorry for that.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 12:07 am
by Taehl
First, please put your code within a [ code] tag (as I do below). It's much easier to read.
Yes, you'll want to move that out of love.draw as the above poster said. Next, instead of that complicated string.sub line you have, I'd replace it with this:
Code: Select all
function readScore()
npcdata = {}
for k,v in ipairs(tab) do
for key,score in string.gmatch(tab, "(%w+);(%d+)") do
npcdata[key] = score
end
end
end
Note the use of string.gmatch. It's an iterator function which returns each occurrence of a given pattern. In this case, the pattern is letters, then a semicolon, then numbers. This way, you don't need to worry about the length of the names or scores. This also means that you could format tab as "HI;7, New;8" or something like that (that is, as a single string instead of a table), which would be even easier for you to read/write to/from a file.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 1:16 am
by Lua Hal
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(string.sub(data[i],0,7)) == "newnpc[" then
keepparsing=true
for k = i,#data+1 do
if data[k] == "]" then
keepparsing=false
end
if keepparsing == true then
if data[k] == "selling(" then
selling = {}
keepscrolling=true
for h = k+1,#data do
if data[h] == ")" then
keepscrolling = false
end
if keepscrolling == true then
table.insert(selling,data[h])
end
end
else
temptab={}
temptab["selling"] = selling
for key,score in string.gmatch(data[k], "(%w+);(%d+)") do
temptab[key] = score
end
table.insert(npcdata,temptab)
end
end--end if
end --end for
end --end if
end --end for
end --end npcparse()
npcparse()
function love.draw()
for i = 1,#npcdata do
tab=npcdata[i]
love.graphics.print(tab["name"],0,0)
end
end
I'm trying to print the name of all of the loaded NPCs, but the name comes up as nil, why?
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 3:52 am
by Robin
With something that complicated, it would be easier to just make the file Lua, and use require(). If you want to write to it as well, that's possible but slightly harder. See
http://lua-users.org/wiki/TableSerialization for examples for writing a table to a file.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 12:42 pm
by Lua Hal
Robin wrote:With something that complicated, it would be easier to just make the file Lua, and use require(). If you want to write to it as well, that's possible but slightly harder. See
http://lua-users.org/wiki/TableSerialization for examples for writing a table to a file.
I don't plan on writing to it. It makes a very user friendly API for my game if you want to make your own level.
The map will also read from a .txt file, with letters and symbols representing different terrain types. I'd much rather do it this way, if that's possible.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 12:52 pm
by Robin
Lua Hal wrote:I don't plan on writing to it. It makes a very user friendly API for my game if you want to make your own level.
Then I would
definitely suggest using Lua as a parser.
Lua Hal wrote:The map will also read from a .txt file, with letters and symbols representing different terrain types. I'd much rather do it this way, if that's possible.
Sure, for something like that raw data works best.
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 6:13 pm
by Lua Hal
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
Re: I'm new to Lua, and do not understand what I'm doing wro
Posted: Wed Jul 13, 2011 7:23 pm
by tentus
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