Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing works but unserializing crashes

I'm trying to set up a save function in my HaxeFlixel game.

Some background: The object in question is an instance of Player, which extends FlxSprite. Save data is stored in an instance of a custom class I made for it. That instance is stored in a StringMap (the keys are save names), which is saved by serializing it to a variable in a FlxSave.

Creating the save data and writing it works fine. However, reading the save data back in crashes the game with the message "Invalid field: pixels". pixels is a field from FlxSprite, but it's not the first such field in the serialized string, so it's probably not that.

If it's useful, the declaration of that field is y6:pixelsn - that is:

  • y begin a field, which is named...
  • 6: a string of length 6...
  • pixels (the string)
  • n null
like image 988
Viko Riféo Avatar asked Oct 20 '22 07:10

Viko Riféo


1 Answers

From this line of code you can see that pixels is actually not a variable* at runtime. So the unserializer would crash when it tries to assign value to pixels. But more investigation is need on why the serializer serialized the pixels fields at the first place, because it shouldn't really exist at runtime.

Note*: the accessors of pixels are (get, set), which makes pixels not a real property at runtime. Read more here.

As a general rule, I don't recommend serializing a FlxSprite (or other complex objects) directly. Rather, you should extract the desired information (e.g. x/y position or hp, etc) and serialize only those.

like image 99
KevinResoL Avatar answered Oct 23 '22 06:10

KevinResoL