mercredi 15 juin 2016

Iterating through item variables

I'm asking a question that hasn't been answered very well (and as a result has made me post this question).

I'm trying to dynamically save objects in a game I'm making (I already have a save feature, it's just clunky and I want to make it more efficient). What this dynamic saving involves is checking if all variables of the object you're saving are currently registered using the getClassByAlias function. Here's what the first part of the code looks like:

var objectClass:* = getDefinitionByName(getQualifiedClassName(saveItem));

try {(getClassByAlias(getQualifiedClassName(saveItem)));}
catch (err:Error) {registerClassAlias(getQualifiedClassName(saveItem), objectClass);}

So what this does is it checks if the object's class is currently registered, and registers it if it isn't. Unfortunately I have many classes that contain variables of other custom classes and must also register those classes. I didn't want to manually register every single class so what I've been trying to do is iterate through all the variables of the current object and if that variable's class isn't registered it registers it and then iterates through those variable's variables and continues registering, and so on until all unregistered classes become registered. This will allow me to save literally anything I want because it'll register all the variables that aren't registered on the fly and will make things extremely easier for me.

Seemingly the only way to see an object's variables at run-time is to use the describeType function. This returns an XML instance and you can then access the variables using ..variable (this is an XMLList). This is where I get stuck. I need a way to convert this XML code back into the variable so I can access its variables to check if any of those are unregistered. Here's an example of the XML code that references a custom class variable of this custom class I'm currently iterating through:

<variable name="itemCondition" type="item::ItemCondition">
    <metadata name="__go_to_definition_help">
        <arg key="pos" value="882"/>

I need a way to convert this back into the original ItemCondition so that I can go through its variables and continue registering. This is more or less the breakdown of the code I need (and what I have so far):

private static function registerVariables(saveItem:*):void {
    var objectClass:* = getDefinitionByName(getQualifiedClassName(saveItem));

    try {(getClassByAlias(getQualifiedClassName(saveItem)));}
    catch (err:Error) {registerClassAlias(getQualifiedClassName(saveItem), objectClass);}

    for each (var variable:XML in describeType(saveItem)..variable) {
        var currentVariable:* = SaveAndLoad.xmlToObject(variable);
        var currentClass:* = getDefinitionByName(getQualifiedClassName(currentVariable));

        try {(getClassByAlias(getQualifiedClassName(currentVariable)));}
        catch (err:Error) {
            registerClassAlias(getQualifiedClassName(currentVariable), currentClass);

private static function xmlToObject(variable:XML):* {
    //Code here that gives me the reference to the current variable

Any help would be greatly appreciated. My brother works with Java/C++ and has made something very similar to this, I was hoping there was a way in Flash.

Aucun commentaire:

Enregistrer un commentaire