Why can't I save family's object proprieties with JSON?

0 favourites
  • 13 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hello everyone,

    I read a very efficient way to save each object's proprieties in with AsJSON on this topic:

    construct.net/en/forum/construct-3/how-do-i-8/save-sprite-properties-json-166321

    But why doesn't it works with families? I tried to reproduce the same code but on family's objects and even if it keep all informations (positions, angle, size, etc…), it display randomly every objects from the family.

    Here is the code I use
    And here is the messy result I get

    Is there a way to fix this?

  • When you create a family instance, a random family member will be created. Even if you try to load its state from JSON after that, it won't change the object type.

    Do don't use "create family", you need to create the particular object. If you are using an array to store JSONs, you can save the object name in the same array. And then use "Create by name" action.

  • Ok so i I get it right, beside of load state from JSON, I also need to save the object name and then use the create by name action (like "-> System: Create object "SpriteFam" on layer 0 at (0, 0), create hierarchy: False, template: """) ?

    I really am a rookie with the array so how can I add the sprite's name on it after -> MyArray: Insert SpriteFam.AsJSON at index 0 on X axis ?

  • Change array height to 2. Then you can store JSON string at Y=0 and object name at Y=1. Something like this:

    For each SpriteFam
    .. Array insert SpriteFam.AsJSON at index 0
    .. Array set value at X=0, Y=1 to SpriteFam.ObjectTypeName 
    

    To restore:

    Array For each X
    .. Create object by name: array.at(array.curX, 1)
    
    .... (sub-event)
    .... System Pick Last Created SpriteFam 
    ........SpriteFam set from JSON array.curValue
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Alright, it works!!

    Thanks dop2000, I can finally move to my next issue!

    I suppose that if I want to save another data, for exemple an instance of the object, I need to do it in another Y row? (I still don't get when the Z in array is used)

  • Z is another dimension, you probably don't need that here.

    Yes, you can store other data about each object at Y=2, 3, 4 etc.

  • So I add another data, an instance for each objects like TypeA, TypeB and TypeC.

    Everything is well saved in my array and all the new variables are saved on the new Y row. -> MyArray: Set value at (0, 2) to SpriteFam.Type

    My purpose now is to reload the objects by variable and I tried this:

    + RestoreA: On clicked ----+ MyArray: For each XY element --------+ MyArray: Value at (MyArray.CurX, MyArray.CurY) = "TypeA" ---------> System: Create object MyArray.At(MyArray.CurX,1) on layer 0 at (0, 0), create hierarchy: False, template: "" --------+ System: Pick last created SpriteFam ---------> SpriteFam: Set from JSON string MyArray.CurValue

    It almost worked. I have indeed the good objects with the correct variable but once again it change my object's positions and there is even one not positioned at all:

    Where is my mistake?

  • It's difficult to understand that garbled code.

    I would suggest using "For Each X" loop, nor "XY".

    Or use the System For loop:

    System For "x" from 0 to array.width-1
    .. Create object Array.at(loopindex, 2)
    
  • Oops, sorry for the garbled code.

    Here is a clean version:

    + RestoreA: On clicked
    ----+ MyArray: For each XY element
    --------+ MyArray: Value at (MyArray.CurX, MyArray.CurY) = "TypeA"
    ---------> System: Create object MyArray.At(MyArray.CurX,1) on layer 0 at (0, 0), create hierarchy: False, template: ""
    
    --------+ System: Pick last created SpriteFam
    ---------> SpriteFam: Set from JSON string MyArray.CurValue
    

    I tried already using "For Each X" but i doesn't display anything at all.

    Why can't I use "XY" by the way? My variable is stocked on the second Y line, does it make sense to make it check there?

    Now I'm a bit lost…

  • You don't need to go through all values in the array. Say, if there are 100 objects and 5 lines, "For each XY" will process all 500 cells, you don't need that.

    You loop through records of objects, which are on X axis. And for each object you can grab values form the corresponding cells on Y axis.

    For example, if you need to get a value from the second line (Y=1):

    Array.At(loopindex, 1)

    or if you are using "For Each X":

    Array.At(Array.CurX, 1)

  • Ok I get it, I just need to check the third line then.

    But the thing I still don't get is how do I tell my loop to only get the cells with my variable in it?

    This should work right?

  • You are using the wrong condition. "Contains value" searches the entire array. You need "Compare At XY", where X=Array.CurX and Y=2

  • Oh I see!! Alright!!! Now that's working!!!

    Thanks again for your precious help dop2000! You rock!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)