Dynamically load and save objects using Reflection

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Hi
Ive published this module v0.1 https://github.com/pantson/pantson-cerberusx-fileobject
This is very early days to dynamically load and save objects.
Currently it only supports flat objects (no nested objects or arrays) and only supports ints, floats and strings.
This is to help me with my Font Editor and I will support arrays very soon

Usage:
Code:
SaveObject(file:string,obj:Object,object_name:String)
LoadObject(file:string,obj:Object,object_name:String)
Full example in the GIT repo

comments welcome
Rich
 

Phil7

Moderator
Joined
Jun 26, 2017
I just had a quick look at your code and I was wondering, if a json file could be used to implement a recursive writing and reading of Class elements.
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
@Phil7 along with iterating through the class fields and arrays, the next version will need a better output format and JSON would be the way to go. I didn't want to get bogged down with JSON in this version ;-)
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Ive updated the module now so that it supports Arrays of Ints, Strings and Floats. It also supports custom types in types.
I havent got Arrays of custom types working yet

Ive also wrapped a DisplayObject function that will print the object values to screen/log
EDIT - oh and Ive broken the loadobject function ;-)

@Phil7 suggestion of JSON has got me thinking about the next iteration of this module.
JSON seems quite convoluted in CX. Im thinking of creating a JSON parser that will create the structure of your class for you.
something like...
Cerberus X:
class obj
end

test = new obj
# excuse the escaping. its just an example
CreateObject(obj,"{\"hello\":\"\",\"number\":0}")
# or
CreateObject(test,"{\"hello\":\"world\",\"number\":42}")

# world
Print test.hello
# 420
Print test.number*10
 
Last edited:

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Think I'll fix the load mechanism and array of custom objects in the current module and then look at JSON.

Initial thoughts are that JSON is going to need the array of custom objects as a minimum
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Ive been staring at my code fo far too long now. If some of you are feeling brave Im going to ned some help please...
In my module code around line 154, there is a commented out block of code. This in theory should work but the casting isnt working as I expected.
Cerberus X:
                  Local k:Object[] =  ArrayBoxer<Object>.Unbox(fields[i].GetValue(obj))
                  Local m:Int=0
                  While m<k.Length()
                        SaveObject(fl,k[m],prefix+fields[i].Name+"["+m+"].")
                      m+=1
                  Wend
The ArrayBoxer.UnBox seems to work translating ints or floats, but doesnt work with custom types.
What I need to do is cast to an object so that I can pass the objects to the SaveObject.
The value is nested[] in the field, not object[]

When not working with arrays, the code works
Code:
SaveObject(fl,fields[i].GetValue(obj),prefix+fields[i].Name+".")
The above is line 196in my code.

Im either not processing Arrays of custom types incorrectly, or casting incorrectly.

thanks in advance
 
Last edited:

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
another update...
As i havent fixed nested custom types yet, I decided to break it down a bit more.

Ive add 2 more functions
Cerberus X:
Object2String:String[](obj:Object)
String2Object:void(text:String[],obj:Object)
This allows you to do what you what with the text yourself. ie save it to GameState or add it to your own config files.
See the latest example in the repo to see GameState example

Using this method you could even add nested custom types o the config files yourself (until i find a fix)
 
Top Bottom