Sign in to follow this  
Bagheera

[SOLVED] Problem implementing classes in Lua

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 + 1
end



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this