How do I Load JSON from a different layout

0 favourites
  • 10 posts
From the Asset Store
Adjusting the game screen for different resolutions (Letterbox scale)
  • I am trying to let a player keep their items across layouts. I don't want to set the items global, because most items need to persist on their own layout.

    I noticed that when an object is SetFromJSON on a different layout, it disappears. Is there a way I can prevent this from happening? Is there a line I can delete from the JSON that will allow it to remain on the current layout? I'm pretty familiar with regexes, but the JSON seems to be obfuscated.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I was looking through the source hoping that perhaps the layer SID was causing the problem, but that doesn't seem to be the case. Can I partially set from JSON? If I remove a few elements from the JSON, can I still load from JSON and keep the other values as they are when the Item is created? I also noticed that the issue is independant of whether layouts are changed.

    Should I give up and go to some kind of abstract Inventory system? I'm hesitant to do that, since Construct 2 doesn't really handle abstract objects very well: You can't put them in data structures. It seems much more complicated to hang custom events on an array inventory....

    Edit: here is the JSON, after the "l" element was removed.

    More Edit: Using firebug, I was able to catch [code:3991b27o]SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
    Line 609[/code:3991b27o] I'm going to look into why this instance is being passed an empty string. I've attached a screenie of the test event I created. I'd be inclined to leave a capx, but I am using a bunch of 3rd party behaviors.
    In this screenshot, Save Item and Create Instance are both functions from a little custom behavior I made which is able to save the object type of an unknown sprite, and respawn it from sid. It seems to work correctly. If I remove the Set From JSON line, it seems to properly creates a MeleeWeapon or FryingPan or Boots or whatever. I can upload that behavior if someone is interested.
    Edit: caught a major derp in the above screenshot. ItemJSON is now properly set to Item.asJSON, and everything works, at least in this test case without changing layouts. This is the same as my test case. I notice that it does NOT work if I strip out the layer sid, so that must be important somehow. It's also the only data in that JSON that seems like it might be mismatched.  Should I create a JSON mangler to replace the layer sid with the sid of the new "UI" layer?
    To confirm: I have my main project behaving the same way as this demo, which is able to save and restore sprites from JSON, but it just destroys the sprite whenever I try to SetFromJSON on a different layout from the one where the JSON was created. Help!
  • Hi,

    Your event sheet is affected to your two layouts so may be it's the problem because your JSON global variable is set as "" when you load the Layout, so it will be "" when Layout2 is loaded.

    You should try creating a new event sheet to have your global variable loaded only with your first layout.

    Not sure i'm clear enough, let me know

  • Thanks! I was able to resolve the issue with the JSON being "". Have a look at that demo I posted: it has the same issue as my main game.

  • I'm not sure to understand very well.

    Your problem is that you don't have the sprite displayed on layout 2 ?

  • I create a sprite on layout1, then save a JSON snapshot of it. When I move to layout2, I create a new sprite and when I try to apply the JSON snapshot, the new sprite is destroyed. It works fine if I don't switch layouts.

  • Understood, however don't have any idea about this.

    May be you could save as Json and use array to load what you need to load?


  • I've updated my demo so it clearly shows what is going on. You can press C to create a new sprite, and you can click on it to update it's state. You can hover over a sprite and hit S to save it's state, and then L to load. Loading works just fine if you don't switch layouts, and when a sprite gets Set From JSON, it moves to the correct spot and gets it's color and variables set correctly. If you switch layouts though, the sprite is destroyed when you try to load it.

  • Why are you using JSON to make items persistent in layouts? There's a persist behaviour that already does that, seems like you're just making things more difficult than they need to be.

  • If I understand Persist correctly, it only keeps things on the same layout. I want to take things to a different layout, like Global, but only for specific instances. This would also give me the freedom to lock items away as a string for a while.

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