I'm new to Lua, and do not understand what I'm doing wrong.

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.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

I'm new to Lua, and do not understand what I'm doing wrong.

Post 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.
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: I'm new to Lua, and do not understand what I'm doing wro

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

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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.
Help us help you: attach a .love.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: I'm new to Lua, and do not understand what I'm doing wro

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

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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.
Help us help you: attach a .love.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: I'm new to Lua, and do not understand what I'm doing wro

Post 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

Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: Bing [Bot], dusoft and 8 guests