Reflection

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Hi
Any reflection in cerberusX?
How would I loop through fields in a class? Is this possible?
ta
Rich
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
It is the same like in Monkey X. But I have no experience in what you want to do. But I heard that it is possible.
 

Phil7

Moderator
Joined
Jun 26, 2017
I also never used it, but could remember something in the tutorial videos of smalltimeoutlaws. Maybe this video shows what you are looking for. I think exacly that in about the middle of the video.
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Thats perfect!
I found the REFLECTION_FILTER in the docs but with no information

thank you everyone
 
Last edited:

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
TIP 1 for reflection:
If you need access to vars within your Game class
Code:
Function Main:Int()
New Game()
Return 0
End Function

Class Game Extends App
end
your reflection filter needs to look like (game is the name of the file with your code, not your class name)
Code:
#REFLECTION_FILTER="game|mojo.app"
If you need access to Image vars within your code then the reflection code needs to look like this
Code:
#REFLECTION_FILTER="game|mojo.app|mojo.graphics"
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
TIP 2 reflection getting the fields

This is an example. This will set all strings to "hello world", all floats to 4.2,all ints to 42 and load an image into all images.
Not very practical.

Code:
' get class
Local c:ClassInfo = GetClass("Game")
' get fields
Local fields:FieldInfo[] = c.GetFields(False)

i=0
While i<fields.Length()
  Print "var name="+fields[i].Name
  Print "var type="+fields[i].Type.Name

  Select fields[i].Type.Name
  case "cerberus.boxes.StringObject"
    fields[i].SetValue(Self,BoxString(String("hello world")))
  case "cerberus.boxes.FloatObject"
    fields[i].SetValue(Self,BoxFloat(Float(4.2)))
  case "cerberus.boxes.IntObject"
    fields[i].SetValue(Self,BoxInt(Int(42)))
  case "mojo.graphics.Image"
    fields[i].SetValue(Self,LoadImage("image.png"))
  end
  i=i+1
Wend
These videos (thanks to Phil7) show a better example of using a text console to set vars within a game.
 
Last edited:
Top Bottom