• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Dynamically load and save objects using Reflection

Rich

Well-known member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
3rd Party Tool Dev
Joined
Sep 9, 2017
Messages
451
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
 
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.
 
@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 ;-)
 
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:
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:
JSON seems quite convoluted in CX
That was my impression too, when I was trying to access nested JsonObjects. Maybe this module needs a next iteration!?
 
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
 
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:
                  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:
Last edited:
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:
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)
 
Back
Top Bottom