How do I save several families as JSON?

From the Asset Store
Template for a generic save / load system, fully documented in comments and video
  • Hello;

    I have a UID that is a member of two families, ball and toy and both are sprites. To to save and load it from JSON is this what I need to do?

    pick toy by UID toyString = toy.AsJSON

    pick ball by UID ballString = ball.AsJSON

    then to load it i create a ball or toy by UID and do the following?

    pick ball by UID ball set from ballString

    pick toy by UID toy set from toyString

    The second set from toyString doesn't overwright the stuff that is ball only that I set from ballstring? What about the sprite specific stuff? Does it get its location angle etc from the last toy set from toyString?

    Is there some better way to save every family a UID belongs to as a string?

    thanks

    yours

    winkr7

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • then to load it i create a ball or toy by UID and do the following?

    There are multiple problems with this:

    1. You can't "create by UID", when you create a new object, it will have a new UID, different from what you saved earlier.

    2. When you create a family, a random family object will be created. For example if your Toy family has 3 members (Bear, Monkey and Chicken), when you do "Create Toy", a random animal will be created. So you need to create the right object specifically. For this you need to also save object's name (Toy.ObjectTypeName) and then create this object by name. After that you can use event "System Pick Last Created" and load it from JSON.

    3. Finally, you don't need save/restore two JSON strings for each family. JSON string contains all object properties, which already includes all family properties. So you can use just one (any one) string - Object.AsJSON, Ball.AsJSON or Toy.AsJSON - they all will be identical.

  • Thanks for your time dop2000.

    So I think I am almost there, but I have a few questions.

    I want to store and save the object UID in outString, I don't know if it is also a member of the ball family or also a member of the monkey family or a toy. I just want to send the string out and then rebuild the object.

    How would I get hold of object.AsJSON. outstring=object.AsJSON doesn't exist because I can only pick the UID by family I can't pick it as a sprite or as an object. So I have to save it as either a toy, or a monkey or a ball. I can't save it as a sprite or object (or tell me how if I can). You say they are the same anyway. So lets suppose I get my outstring somehow.

    Now what?

    what object do i create to reload it? If I create a toy and then try set from JSON string outstring what if it was a ball?

    I think I am close, but it seems I have to be specific about the family I am recreating as I have to call create before I can call set from JSON string.

    thanks for your help so far, and I appreciate your time.

    yours

    winkr7

  • To save an object:

    // First you need to pick one instance of object
    // this can be an instance of Object or Toy family or Ball family, for example:
    Touch on touched Toy
    	Set sJson to Toy.AsJSON
    	Set sname to Toy.ObjectTypeName
    

    To re-create the same object:

    System Create object (by name) sName
     System Pick Last Created Toy
    	Toy load from sJson
    
  • dop2000;

    thankyou very much. This is very clear.

    yours

    winkr7

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