Hey ArdentKid,
Thanks for the reply, definitely helped. I don’t know why I didn’t just try that at first. I’m sorry to bother you again, but I have another question regarding an OO approach in LUA. Sorry if it is a bit long, but I would really appreciate your opinion.
How would you handle Subclassing/Inheritance in your implementation (specially regarding the Instances recycling)?
To keep with the running example (even though I don’t have ‘enemies’ in my app haha), One could keep the single ‘Enemy’ class and create some logic to differentiate between types of instances and create or re-use the specific instances (i.e. with the specific image/sprites and specific properties/values) when necessary, but it makes more sense to make them into their own subclasses.
My issue is exactly how to handle the inheritance while maintaining the idea of the instance recycling.
Here is how I see it. Please let me know what you think.
First the parent Enemy class. It only defines and prepares an instance and attaches any properties and functions that every Enemy type should have. It does not keep track of instances. This is up to the subclasses
[lua]–Enemy.lua
local Enemy = {
– All class properties Enemy
}
function Enemy:New(config)
– Create new Image/Sprite using parameters included in ‘config’
– Attach all instance level properties that all enemy objects should have with values from ‘config’
– Attach all instance level functions that all enemy objects should have
– Return the instance
end
– All other function definitions (like the instance functions) for this parent class
return Enemy[/lua]
Now the subclass. This uses the parent class to create a basic instance, attaches all properties/functions specific to the sublcass instances and also keeps track of its own instances for recycling.
[lua]–SubEnemy.lua
local Enemy = EnemyClass – EnemyClass = require( “Enemy” ) has already been called on whatever script will include this Enemy sublcass
local SubEnemy = {
– All class properties for Subenemy
Instances = {},
defaultConfig = nil – This would be the default values of all properties (including Image/Sprite info) for all instances of this class
}
–This function would be called right after loading the script with a table of all the default values to be used when generating new instances
function SubEnemy:Initialize(config)
defaultConfig = config
end
function SubEnemy:New(config)
local seInstance = Enemy:New(config) – The ‘config’ used would be either the parameter to this function or, if that is nil, then the ‘defaultConfig’ of the class
– Attach all instance level properties of subEnemy objects with values from ‘config’, if ‘config’ is nil, use class defaultConfig
– Attach all instance level functions subEnemy objects
– Override any property/function necessary by just re-defining and re-assigning
table.insert(self.Instances, seInstance)
return seInstance
end
function SubEnemy:Get(config)
– Check for available instance, if so, reset using ‘config’, if ‘config’ is nil, use class defaultConfig
– If no instance is available, create a new one (passing ‘config’)
– return the new instance
end
function SubEnemy:Dispose(obj)
– As your implementation
end
– All other function definitions (like the instance functions) for this parent class
return SubEnemy[/lua] [import]uid: 142439 topic_id: 33679 reply_id: 135010[/import]