Page 1 of 1

Trouble with LUA Classes

Posted: Sun Feb 10, 2008 11:31 pm
by MasterSaitek009
I've started work on an RPG. So lately, I've been fiddling around with classes.

They seem like a good way to make the programming a little easier.

There's something wrong with this code block but I don't know what.

Code: Select all

Item = {itemName = "", itemValue = 0}
function Item:new(o)
	o = o or {}
	setmetatable(o, self)
	self.__index = self
	return o
end
	
Inventory = {items = {}}
function Inventory:new(o)
	o = o or {}
	setmetatable(o, self)
	self.__index = self
	return o
end

Player = {credits = 0, bag = Inventory:new{}}
function Player:new(o)
	o = o or {}
	setmetatable(o, self)
	self.__index = self
	return o
end

function Player:GetItemCount()
	return table.getn(self.bag.items)
end

function Player:GetItemName(x)
	return self.bag.items[x].itemName
end

function Player:RemoveItem(x)
	self.bag.items[x] = nil
end

function Player:AddItem(x, item)
	self.bag.items[x] = item
end


	srp = Item:new{itemName = "Super Pistol", itemValue = 10000}
	lsr = Item:new{itemName = "Death Machine", itemValue = 1000000}
	
	BP = Item:new{itemName = "Pistol", itemValue = 0}
	DR = Item:new{itemName = "Rifle", itemValue = 0}
	ET = Item:new{itemName = "Grenade", itemValue = 0}
	
	a = Player:new{credits = 50}
	a:AddItem(a:GetItemCount() + 1, srp)
	a:AddItem(a:GetItemCount() + 1, lsr)
	
	b = Player:new{credits = 85}
	b:AddItem(b:GetItemCount() + 1, BP)
	b:AddItem(b:GetItemCount() + 1, DR)
	b:AddItem(b:GetItemCount() + 1, ET)

	print(a:GetItemName(1)) -->>Super Pistol
	print(a:GetItemName(2)) -->> Death Machine
	print(a:GetItemCount()) -->> 5
	print(a.credits) -->> 50

	print(b:GetItemName(1)) -->> Super Pistol
	print(b:GetItemName(2)) -->> Death Machine
	print(b:GetItemName(3)) -->> Pistol
	print(b:GetItemCount()) -->> 5
	print(b.credits) -->> 85	
For some reason it's not creating a new Inventory object inside of the Player object.

I'm guessing that it traces back to this line:

Code: Select all

Player = {credits = 0, bag = Inventory:new{}}
But I can't figure out why. :?

Does anyone know why it would be doing this?

Re: Trouble with LUA Classes

Posted: Mon Feb 11, 2008 9:28 am
by [RDH]Zerted
I've never really needed any inheritance, so I've never looked into creating Lua classes. I've always used arrays (which I think the classes are really just syntactic shortcuts. Lua doesn't have a concept of classes.)

You can find stuff on Lua by searching online. Heres the link about classes on that online Lua programming book: http://www.lua.org/pil/16.1.html

Re: Trouble with LUA Classes

Posted: Mon Feb 11, 2008 8:17 pm
by MasterSaitek009
Yeah, I've checked there. Nothing related to this problem. I guess I'll have to find a work around.
[RDH]Zerted wrote:I've always used arrays (which I think the classes are really just syntactic shortcuts. Lua doesn't have a concept of classes.)
Sort of a pseudo-class using Lua's table datatype.

And actually "classes" are used already in the shipped gamemodes.

Code: Select all

cp1 = CommandPost:New{name = "cp1"}
That line is creating a new instance of the CommandPost class. Flags and Objectives are also "classes". Kind of cool.

I posted this problem on Icy North Technologies, an Lua forum. I hope someone will answer me.... Soon.

Anyway, thank you