pwniknoobz wrote:Thanks for the help everyone - I actually decided it was kind of pointless for me to make a stage class, because for every object I created I'd have to modify the stage class to make it use the respective draw/user input functions of the new class. It is much more practical for me to just make a supplemental external function in which to handle all of the drawing. I will just keep track of staging for objects via a 2 dimensional array, i.e. Object
[j], where i is the current stage. Event handling for the objects is simply based on a conditional statement tracking the current stage(global variable). In this manner I don't have to worry about an additional unnecessary stage "object."
You get a lot of power from keeping it as objects, such as a good place to keep common functionality, inheritance, decoupling, and the possibility of pausing or stacking stages. If you are uncertain about whether the OO stuff will work out, maybe it will feel safer for you to abandon it but I think they functionality and experience will be beneficial in the long run. Anyhow, the issue was as pygy said although let me rephrase it because I'm not sure it was understandable.
The thing is that you had a table such as
and created two objects that used this class
Code: Select all
object1, object2 = {}, {}
setmetatable(object1, class)
setmetatable(object2, class)
Now the metatables work as such that if you query an element of the instances that does not exist, it will look in the metatable/class instead.
If you create a new variable with
Code: Select all
object1.y = {1=3}
object2.y = {1=4}
the two instances will create their own distinct variables and things work as you want them to.
Code: Select all
print object1.y[1]
>> 3
print object2.y[1]
>> 4
Changing the variable also works as you expect since they are stored in the object1 and object2 tables, respectively.
Code: Select all
object1.y[1] = 5
object2.y[1] = 6
print object1.y[1]
>> 5
print object2.y[1]
>> 6
However, with
Lua will look for 'x' in the object1 and object2 tables, and if it cannot find it, in the metaclass. So "object1.x" will return "class.x" and the same applies for object2 and hence they change the same array.
In other words, the OO analogue of "class = {x = {}}" is not to declare an instance variable but rather a class variable.