[SOLVED] Problem implementing classes in Lua

Started by
1 comment, last by Bagheera 16 years, 8 months ago
Here's a simplified version of turretManager.lua:


turretManager = {}
turretManager.__index = turretManager

function turretManager:new()
    local mgr = {}
    setmetatable(mgr, turretManager)
    mgr.mNumTurrets = 5
    return mgr
end

function turretManager:someMethod()
	print(self.mNumTurrets .. "hi")
end

tManager = turretManager.new()



I use tManager from another lua file in the same Lua state:

dofile("TurretManager.lua")
tManager:someMethod()


I get a lua error when I try to use tManager:someMethod(), saying that I'm attempting to concat the field self.mNumTurrets, which is a nul value. WHAT GIVES?! I'm going crazy here. This is exactly according to the Lua tutorials... Or is it? [Edited by - Bagheera on August 17, 2007 11:43:53 AM]
Advertisement
I copied that code and ran it in my interpreter, it worked perfectly. The only thing that I can think of is that you misspelled the name of the table element in your original script.
The problem was that inside of tManager:someMethod() I called other methods via the dot and not the semicolon. For example:

function turretManager:createTurret(pos)	if(self.mTurretToCreate == "Turret 1") then		self.createShells(pos) // HERE			elseif(self.mTurretToCreate == "Turret 2") then		self.createVulcan(pos) // HERE TOO	end	self.mTurretToCreate = ""	self.mNumTurrets = self.mNumTurrets + 1end

This topic is closed to new replies.

Advertisement