Help with reflections

BlitzMax Forums/BlitzMax Beginners Area/Help with reflections

Hi Everybody,

I'm trying to write a function that goes through every object in a scene and saves its state (basically, it will be a project file for a game editor). Since I don't want to hard-code which fields get saved according to which kind of entity, I decided to learn to new reflection features.

It works well for simple fields (int, float, string...) but I'm having trouble when the object contains a field that asks another type, like this:


SuperStrict

'Creates a list containing all root objects
Global Rootlist:TList = CreateList() 

'Defines the types
Type TEntity
	Field Name:String
	Field X:Float, Y:Float
	Field Shape:TShape		'<-------Here's the problem!
End Type

Type TShape
	Field Name:String
	Method DoStuff() 
	End Method
End Type

'Creates the entities using the pre-defined types
Local MyShape:TShape = New TShape
MyShape.Name = "MyShape"
Local MyEntity:TEntity = New TEntity
MyEntity.Name = "MyEntity"
MyEntity.X = 100
MyEntity.Y = 50
MyEntity.Shape = MyShape
Rootlist.AddLast(MyEntity) 

'Main program, prints the name and fields of every entity in the scene
Graphics (800, 600, 0, 30) 

While Not AppTerminate() And Not KeyHit(KEY_ESCAPE) 

	Local Y:Int = 0
	
	For Local Obj:Object = EachIn Rootlist
		Local id:TTypeId = TTypeId.ForObject(Obj)   	'Creates a temporaty "Type Id" object, necessary to inspect other objects
		For Local Fld:TField = EachIn id.EnumFields() 
			Y:+16
			DrawText(Fld.Name(), 16, Y)  						'prints the field name
			DrawText (String(Fld.Get(Obj)), 112, Y)  		'Prints the field contents... but is empty when the field contains another object
		Next
		Y:+16
	Next
	
	Flip
	
Wend



The big problem is: if I run into a field that contains a different type (in this example, a "TShape" object) how do I:

1 - Identify that type?
2 - Identify a specific field within that type? In my example, If I could extract the "name" field from the shape I could easily reconnect the two objects when I load the project file in the editor.

Any help is appreciated! My brain is kinda numb right now...

Cheers,
Leo.