Skip to main content
gamedev.net
🔒 Locked

[SOLVED] Problem implementing classes in Lua

Started by Bagheera 1 reply 1.1k views
Original Post
Bagheera
Bagheera
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]
Merick Zero
Merick Zero
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.
Bagheera
Bagheera
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.