Discord
Login
Community
DARK THEME

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.

RobotClass = class
  CLASS_ID = "Root Class"  
  constructor = function(name)
    this.name = name
  end
  update = function() end
  draw = function() end

end

GameObject = class extends RootClass
  CLASS_ID = "Game Object"
  constructor = function(name)
    super(name)
  end
    
  update = function()
    super()
  end
    
  draw = function()
    screen.drawRect(0, 0, 100, 100, 999)
    super()
  end
end

Game = class extends RootClass
  CLASS_ID = "Game Class"
  constructor = function(name)
    super(name)
    
    this.testObject = new GameObject("Rectangle")
    
    // print(this.GameObject)
    // print(this.testObject)
  end
  
  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 
  
  draw = function()
    screen.clear(999)
    // this.testObject.draw()
    super()
  end
end

global.init = function()
  rc = new RobotClass()
  // 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.
  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(" print game.update ")
  print(""+game.update)
  
  global.draw = function() game.draw() end
end

You're doing more research than me.

I don't know if the microScript language allows local classes and defining classes in a class.

I changed your code to make it more readable and, in my opinion, more correct.

Now an explanation:

  • when you do print class, you will get information about a given instance
  • all fields of this class and "class" will be printed
  • in "class" you can see all methods of a given class
  • if a class inherits from another class, then in "class" there will be "class", if you have a function to read class fields, you will see it
  • some fields are hidden, e.g. when you run my code, print( game ) will not show the "CLASS_ID" field, but if you do print( game.CLASS_ID ) it will return the correct result
  • if you want to print information about a given method/function of a class, it is done differently. For a quick way you can use print( "" + game.update ) to get the bytecode translated into something like the MicroScript machine assembly language .

It works the same way in the console.

Interesting, thank you for your response!

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community