Jump to content
  • Advertisement
Sign in to follow this  
Bagheera

[SOLVED] Problem implementing classes in Lua

This topic is 4052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!