How do you use Reflection?

Monkey Forums/Monkey Programming/How do you use Reflection?

Tibit(Posted 2012) [#1]
I'd love to see some ideas on how to make use of the new Reflection in Monkey.

Is there something you can do with Reflection in a "better" way than without?

Is there something that was made possible, that was not possible before?

Is there a common problem that it solves or simplifies?

Anything else?

Is this all documentation there is on reflection? Wiki - The Reflection Module


ziggy(Posted 2012) [#2]
It can be very handy to perform class serialization http://en.wikipedia.org/wiki/Serialization


Tibit(Posted 2012) [#3]
Ah that is interesting. Hadn't consider it.

I still feel lost on how. I have used reflection in C#, but not for "realtime" code.

Ziggy could you post a simple example on how to Serialize a class using reflection - the way you would do it yourself.

Because one architectural problem in network programming is that one have to often manually (for optimization purposes) pack and unpack the data - a lot of code that does little.


slenkar(Posted 2012) [#4]
heres how to save your game with reflection
http://monkeycoder.co.nz/Community/posts.php?topic=2487

it really helps me to be able to save my game so I can debug things

you could use the same code to turn an object into a string for network programming

To be honest theres not a lot else people use reflection for, once I used it to create a new class using the classes name as a string.


Samah(Posted 2012) [#5]
@slenkar: To be honest theres not a lot else people use reflection for, once I used it to create a new class using the classes name as a string.

Scripting. I used reflection for MonkeyLua to bind Monkey class fields to table keys.
Also as discussed here, it can also be used for pseudo-function pointers.


wiebow(Posted 2012) [#6]
I'm using it to automatically recognise test methods in test classes for my unit test module:
http://code.google.com/p/mutated-monkey/wiki/unittest


Tibit(Posted 2012) [#7]
Oh I like the UnitTest stuff - that was really smart.

Save & Load game, that is also cool. So you save all "game objects" and then you can load ANY game state.

Seems to good to be true. Since Monkey reflection should be quite fast - where is the limitation? How fast or slow is it?

Samah how do you make use of scripting? I mean I understand how it is "normally" used in games - but how do you make use of it in Monkey? Can you change running code in an iOS game somehow? Or is it just a simple way to add AI-logic, MapEvents, CutScenes and such?

Cool stuff, I'm glad I asked! :)


slenkar(Posted 2012) [#8]
you have to put all your game objects in the fields of one object, because you can only load or save one object.

It does allow you to save and load any game, its extremely useful!

reflection seems quite fast but it does have limits, loading about 400k of saved game takes about 5 seconds on my slow pc, saving takes about 10


Samah(Posted 2012) [#9]
@Tibit: Can you change running code in an iOS game somehow? Or is it just a simple way to add AI-logic, MapEvents, CutScenes and such?

Both. :)

Essentially you just store your script as a string, then execute it from anywhere. With MonkeyLua you can bind Monkey methods/functions/fields to Lua so that you can put all your game logic in there if you want. Just reload the script at runtime (change the string) and it'll run.