2

So, i've been working with ByteArrays a lot recently and i'm running into some annoying problems that make me want to rip my hair out.

basically, i'm trying to save project data for an application that i'm making to compile characters for a game into one file. the project consists of custom objects, vectors, vectors of vectors, and even vectors of vectors of custom objects! i figured the best way to write all this data properly would be to use the IExternalizable interface with the readExternal and writeExternal commands. so here's what i'm doing.

i write all the project data to an object, then write that object into a ByteArray, and save it to a file:

// probject means project object !
mProbject= { };

// Register vector as class, just to prevent any errors about that just in case
registerClassAlias("vec_core.timeline.KeyFrame", Vector.<KeyFrame> as Class);

// single KeyFrame Object
mProbject.currentFrame = Main.getInstance().animationList.selectedItem.currentKeyFrame; 

// a Vector.<Vector.<KeyFrame>>
mProbject.frames = Main.getInstance().animationList.keyFrameVectorVector; 

// an unsigned int
mProbject.selectedItemIndex = Main.getInstance().animationList.entries.indexOf(Main.getInstance().animationList.selectedItem);  

// Vector.<String>
mProbject.animationNames = Main.getInstance().animationList.animationNames; 

// String
mProbject.projectPath = nativePath;

//String
mProbject.projectName = name; 

mByteArray = new ByteArray();
mByteArray.writeObject(mProbject);
mByteArray.compress();

return mByteArray;

inside the KeyFrame class though, there is two more vectors of custom objects:

private var mHitboxes:Vector.<Hitbox>;
private var mHitboxSprites:Vector.<HitboxSprite>;

so i set up both of those classes and my KeyFrame class to use IExternalizable:

public class HitboxSprite extends Sprite implements IExternalizable
{
    public function readExternal(input:IDataInput):void
    {
        trueBounds.x = input.readFloat();
        trueBounds.y = input.readFloat();
        trueBounds.width = input.readFloat();
        trueBounds.height = input.readFloat();
        mHitbox = input.readObject();
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeFloat(trueBounds.x);
        output.writeFloat(trueBounds.y);
        output.writeFloat(trueBounds.width);
        output.writeFloat(trueBounds.height);
        output.writeObject(mHitbox);            
    }
}


public class Hitbox implements IExternalizable
{
    public function readExternal(input:IDataInput):void
    {
        mName = input.readUTF();
        mType = input.readUnsignedInt();
        mEnabled = input.readBoolean();
        mKnockback = input.readBoolean();
        x = input.readFloat();
        y = input.readFloat();
        width = input.readFloat();
        height = input.readFloat();
       addMultipleTags(input.readUTF());
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeUTF(mName);
        output.writeUnsignedInt(mType);
        output.writeBoolean(mEnabled);
        output.writeBoolean(mKnockback);
        output.writeFloat(mX);
        output.writeFloat(mY);
        output.writeFloat(mWidth);
        output.writeFloat(mHeight);
        output.writeUTF(getAllTags());
    }
}

public class KeyFrame implements IExternalizable
{
    public function readExternal(input:IDataInput):void
    {
        mID = input.readUnsignedInt();
        mLabel = input.readUTF();
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeUnsignedInt(mID);
        output.writeUTF(mLabel);
    }
}

but when it gets to the writeObject() method of the "root" ByteArray, i get the error:

[Fault] exception, information=ArgumentError: Error #2004: One of the parameters is invalid.

this is probably the single most annoying problem i've ever had. it seems like i've tried everything and nothing is working. does anyone else have experience with this? am i doing something wrong?? i'd appreciate any help i can get on this. i just wanna continue making my game :<

dyxribo
  • 33
  • 5
  • Maybe you're looking for [this](http://stackoverflow.com/questions/14576627/how-to-work-registerclassalias-method-for-custom-mxml-components)? – Marco Aurélio Deleu Oct 13 '15 at 20:54
  • @MarcoAurélioDeleu my friend, you've saved my life. i almost lost my mind, and so, i thank you. that question didn't appear when i searched for this problem, so i'm thankful for you pointing me to that answer! the adobe docs just tell you to register the class, but not that it will work for custom objects only if you register the object! if you want, you can make your comment an answer and i will gladly accept it :> – dyxribo Oct 13 '15 at 21:45

1 Answers1

1

Like mentioned in the comment section, you were looking for this. What rang the bell for me was the "Sprite" class you extended. That made me suspicious in the matter that you were trying to externalize visualizable content.

Community
  • 1
  • 1
Marco Aurélio Deleu
  • 4,279
  • 4
  • 35
  • 63
  • oh, yeah! i didn't even think about that! i completely forgot that externalizing visual objects such as sprite was different than a normal object. still, i didn't know that externalizing a sprite in this manner was even possible. i guess you learn something new everyday, lol. thanks again! – dyxribo Oct 13 '15 at 22:27