Could someone explain what is going on?
Here is some code that I wrote:
global.init = function()
local RootClass = class end
RootClass.CLASS_ID = "Root Class"
RootClass.update = function() end
RootClass.draw = function() end
RootClass.constructor = function(name)
this.name = name
end
local Game = class extends RootClass end
Game.CLASS_ID = "Game Class"
Game.constructor = function(name)
super(name)
this.GameObject = class extends RootClass end
this.GameObject.CLASS_ID = "Game Object"
this.GameObject.constructor = function(name)
super(name)
end
this.GameObject.update = function()
super()
end
this.GameObject.draw = function()
screen.drawRect(0, 0, 100, 100, 999)
super()
end
this.testObject = new this.GameObject("Rectangle")
// print(this.GameObject)
// print(this.testObject)
end
Game.update = function()
print(" ")
print("--> this")
print(this)
print(" ")
print("this.CLASS_ID: " + this.CLASS_ID)
print(" ")
print("--> this.class")
print(this.class)
print(" ")
// Is class the class ('Game') itself?
print("this.class.CLASS_ID: " + this.class.CLASS_ID)
print(" ")
print("--> this.class.class")
print(this.class.class)
print(" ")
print(" ")
print("this.class.class.CLASS_ID: " + this.class.class.CLASS_ID)
// this.testObject.update()
super()
end
Game.draw = function()
screen.clear(111)
// this.testObject.draw()
super()
end
// Need to wrap the global update and draw in functions or else
// the code in thegame class's instance's update and draw methods
// with be using the global scope for some reason.
local game = new Game("Game")
global._updated = false
global.update = function()
if not global._updated then game.update() end
global._updated = true
end
print(" ")
print(game)
print(" ")
print(game.update)
global.draw = function() game.draw() end
end
This is what the results were from the console:
microScript 2.0
object
class = [object]
name = "Game"
GameObject = [object]
testObject = [object]
end
[native function]
--> this
object
class = [object]
name = "Game"
GameObject = [object]
testObject = [object]
end
this.CLASS_ID: Game Class
--> this.class
object
class = [object]
CLASS_ID = "Game Class"
constructor = [function]
update = [function]
draw = [function]
end
this.class.CLASS_ID: Game Class
--> this.class.class
object
CLASS_ID = "Root Class"
constructor = [function]
update = [function]
draw = [function]
end
this.class.class.CLASS_ID: Root Class
It seems like when I create an instance of a subclass, it contains a property called class
which contains it's parents's/overridden functions. However, the class
property doesn't have the parent's class's data, but has another class
property within which holds the parent class's data (in this case it would be "Root Class"
as the CLASS_ID
).
Also, when I printed the local game
property near the end, the console doesn't show the update
and draw
functions:
object
class = [object]
name = "Game"
GameObject = [object]
testObject = [object]
end
But when I printed the game's update function print(game.update)
, it was indeed an existing function [native function]
. What am I missing here? Is the class
property 'part' of the game
instance? Sorry if all of this is confusing.