How do I use Set from JSON?

0 favourites
  • 9 posts
From the Asset Store
The collection includes pre-categorized selections that can be the definitive tone setting backdrop to gameplay.
  • Hello,

    I am trying to swap 2 objects together (which have lots of instance variables), so I thought "Why not try Set from JSON?".

    I saved the AsJSON state of the first object in a variable, and used Set from JSON of the 2nd object, but the 2nd object disappeared.

    When I checked the stored JSON, it did not contain any data related to my variables.

    Is there a specific way for this action to work?

    Thank you!

    Tagged:

  • If you want to make a copy of an object you can right click on it and use 'clone'

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you want to make a copy of an object you can right click on it and use 'clone'

    Thank you.

    What I want is to make a copy of an instance of an object at run time, instead of cloning an object at edit time.

  • Is the initial instance created at runtime?

  • Is the initial instance created at runtime?

    No, both instances are created during edit time.

    Then at runtime, the player changes the instances' states and variables.

    At one point, I need to swap the states of 2 instances.

    I tried to use:

    state1 = instance1.AsJSON

    state2 = instance2.AsJSON

    instance1 -> Set From JSON (state2)

    instance2 -> Set From JSON (state1)

    But it didn't work.

    I had to set the state manually (i.e. save each instance variable to a temporary variable, then swap them, and so on...).

    My question is: is there a way to use Set From JSON to set ALL the instance variables (i.e. state, in general) at once, instead of 1 by 1?

  • I don't see a need to use JSON, you could append all variables into a string and then run through it again with tokenat to set the values on another instance. Saving to a temporary or local variable to swap them is fine though.

  • I don't see a need to use JSON, you could append all variables into a string and then run through it again with tokenat to set the values on another instance. Saving to a temporary or local variable to swap them is fine though.

    That's somehow what I did.

    But it took more time than needed. I hoped AsJSON could do the job in one line.

  • > Is the initial instance created at runtime?

    No, both instances are created during edit time.

    Then at runtime, the player changes the instances' states and variables.

    At one point, I need to swap the states of 2 instances.

    I tried to use:

    state1 = instance1.AsJSON

    state2 = instance2.AsJSON

    instance1 -> Set From JSON (state2)

    instance2 -> Set From JSON (state1)

    But it didn't work.

    I had to set the state manually (i.e. save each instance variable to a temporary variable, then swap them, and so on...).

    My question is: is there a way to use Set From JSON to set ALL the instance variables (i.e. state, in general) at once, instead of 1 by 1?

    This should actually work, may be a wrong order in operations etc.

    Can you post a c3p?

  • Well, after further reasoning, the method to swap the objects by simply swapping their states via JSON appears to be futile since their swap will not be recognizable (same position, size, angle, etc.).

    You can, however store their appearance in local variables, then perform the JSON swap, then overwrite the appearance variables.

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