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

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

Post 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.
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'm not sure what you mean, using Lua as a parser.
That your code simply has

Code: Select all

require "config.txt"
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".
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

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

Post by thelinx »

Robin wrote:
Lua Hal wrote:I'm not sure what you mean, using Lua as a parser.
That your code simply has

Code: Select all

require "config.txt"
instead of a while complicated parser.
Except that wont work because require doesn't take filenames.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post by bartbes »

And it caches, which you probably don't want.
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 »

Robin wrote:
Lua Hal wrote:I'm not sure what you mean, using Lua as a parser.
That your code simply has

Code: Select all

require "config.txt"
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
Kurosuke needs beta testers
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 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
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 »

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:

Code: Select all

functionName(arguments)
. But in Lua, when the only argument is a literal table or string, you can do this:

Code: Select all

functionName "some string"
or

Code: Select all

functionName {1, 2, 3}
.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 199 guests