So, I proceed from the traditional game development, which uses the principles of OOP, and from what I saw, you can imitate this using LUA when you know what you are doing. In some code posts, I found out how you can use the director class and create files with a new function (), etc.
What I'm looking for is a way to control my weapons. I have a player and an opponent, and I would prefer to have one class of weapons, say, Canon weapons. I have done the following:
-- 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 just do:
local weaponCanon = require("weaponCanon") weaponCanon.fire(100, 100)
This works great and allows me to use the "private" and "public" mentality when encoding my weapons. The problem is that if I want the player and the opponent to have a canon:
local playerWeapon = require("weaponCanon") local opponentWeapon = require("weaponCanon")
It just returns the same object, not a new instance of this object. Thus, I get only one Canon weapon at the enemyโs location. This is obviously now what I want / need.
There are a lot of weapons in our game, and it would be nice to have only one version of each file with a setting that tells us if this is an enemy weapon or a playerโs weapon. An alternative is to copy each file and create a weapon, PlayerCanon and weaponOpponentCanon, but I compress the thought of modifications to one file and must change 2+ files each time.
How can I make it return an instance and what is the structure of the LUA file for this?
Thanks or any help
-d
David nelson
source share