Reflection is your best friend

BlitzMax Forums/BlitzMax Programming/Reflection is your best friend

JoshK(Posted 2009) [#1]
OMG, reflection is teh best!

I set up a type for all my application settings:


Then to make actual settings I can use, I create an extended type like this:
SuperStrict

Import brl.reflection
Import "Settings.bmx"

Global configurationsettings:TConfigurationSettings=New TConfigurationSettings
configurationsettings.start()

Type TConfigurationSettings Extends TSettings {label="Configuration"}
	
	Field Trilinear_Filter:Int=1 {style=5}
	Field Anisotropic_Filter:Float {extra="1,16"}
	Field Texture_Quality:Int=4 {style=1 extra="Very low,Low,Medium,High,Very high"}
	Field Physics_Quality:Int=1 {style=1 extra="Fast,Normal,Exact"}
	Field Volumetric_Lighting:Int=0 {style=5}
	Field SSAO:Int=0 {style=5}
	
EndType


All I have to do is declare the field, and it is accessible by writing the configurationsettings.ssao variable, and the interface is automatically created. The setting is loaded, saved, and the user can edit it in a property grid gadget, and I don't have to manage any of that. When they edit the field with the property gadget, it is just automatically updated, and I can use that value right away without doing anything myself.

Next up I am going to link gadgets so they control variables directly, with no need to actually evaluate the events.


ziggy(Posted 2009) [#2]
Yes, that's great. I remember someone did write also a XML serializer based on reflection, so the objects where stored using a XML based file format.


Arowx(Posted 2009) [#3]
Yes reflection is cool!

I was working on a Reflection to database storage system got it to work with basic types.

But needed a lot more work to get ...
Arrays of objects
interlinked objects
binary objects e.g. images/databanks to work

to correctly retrieve/recall and update a database.

@Leadworkds What's with the {} curly brackets haven't seen them used with BMax before?


plash(Posted 2009) [#4]
What's with the {} curly brackets haven't seen them used with BMax before?
That is metadata.


Space_guy(Posted 2009) [#5]
Metadata is really cool and so is reflection.


Wiebo(Posted 2009) [#6]
Interesting. How is metadata used here? Or in blitzmax in general?

EDIT: nm , I did some digging and found an example. I did not know you could add 'secret' messages to fields or methods. MUST you need reflection to access the metadata?


Jim Teeuwen(Posted 2009) [#7]
Yes.


johnnyfreak(Posted 2009) [#8]
are metadata like c# annotations?


JoshK(Posted 2009) [#9]
And you thought you would never see {} in BlitzMax.


N(Posted 2009) [#10]
are metadata like c# annotations?
No. Metadata is strictly keys and values. Values may be literal strings or numbers (can't use the value of a constant), keys may be any valid identifier (last I checked).


beanage(Posted 2009) [#11]
Indeed, reflection is extremely powerful. Yet using it for an upcoming project, wrote some of a powerful skripting thing with it.. got all in all a little disencouraged when learning to use lua, but kept working on it.. maybe ending summer theres something ready to show..


BLaBZ(Posted 2013) [#12]
Josh you're brilliant!

I downloaded your property grid awhile ago and added a bunch of new features and got the idea to do exactly this metadata+reflection! You always beat me to the punch with these great ideas!

I wish I knew then what I know now.