Game Design - OOP Style Objects

So I come from the traditional game development that uses OOP principles and from what I’ve seen you can mimic this using LUA once you know what you are doing. In some of the code postings I found out how you can use the director class and create files that have a new() function etc.

What I’m looking for is a way to manage my weapons. I have a player and an opponent and I would prefer to have one weapon class, say weaponCanon. What I have done is:

-- private vars here  
local power  
local canonSprite  
local whatever  
  
local someFunction = function()  
...  
end  
  
-- Private stuff here  
local weaponCanon = {}  
  
weaponCanon.fire = function(atX, atY)  
...  
end  
  
weaponCanon.reset = function()  
...  
end  
  
return weaponCanon  

Then in my level code I simply do:

local weaponCanon = require("weaponCanon")  
weaponCanon.fire(100, 100)  

This works great and allows me to use a “private” and “public” mentality when coding up my weapons. The problem is that if I want the player and opponent to have a canon:

local playerWeapon = require("weaponCanon")  
local opponentWeapon = require("weaponCanon")  

This simply returns the same object instead of a new instance to that object. So I only get one weaponCanon at the opponentWeapon location. This is obviously now what I want/need.

Our game has many weapons in it and it would be nice to only have one version of each file with a setting telling us if its the opponents weapon or players weapon. The alternative is to copy each file and create a weaponPlayerCanon and a weaponOpponentCanon but I cringe at the thought of modifications to one file and having to change 2+ files every time.

How can I make it return an instance and what is the structure of the LUA file to do so?

Thanks or any and all help

-d [import]uid: 27681 topic_id: 11213 reply_id: 311213[/import]

read: http://lua-users.org/wiki/SimpleLuaClasses [import]uid: 4596 topic_id: 11213 reply_id: 40706[/import]

What the 2nd poster said, or just create a newWeaponCannon() function that returns a new instance of the weapon sprite/displayobject/etc. [import]uid: 58455 topic_id: 11213 reply_id: 40734[/import]