How do I Load JSON from a different layout

Get help using Construct 2

Post » Wed Mar 11, 2015 3:07 pm

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.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Thu Mar 12, 2015 2:55 am

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.
Code: Select all
{"c2":true,"ivs":{"4983201361434822":"2d12"},"w":{"x":40,"y":214.20833333333331,"w":24,"h":24,"zi":16},"behs":{"2355029582157182":{"SlotUID":611},"3459439661079695":{"enabled":true},"8791895563351892":{"dx":0,"dy":0,"cancelStep":0,"enabled":true,"stepMode":0,"pxPerStep":5}},"data":{"a":9827539049161972,"f":12,"cas":0,"fs":0,"ar":0,"at":0}}


More Edit: Using firebug, I was able to catch
Code: Select all
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
http://localhost:50000/commonace.js
Line 609
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!
You do not have the required permissions to view the files attached to this post.
Last edited by bladedpenguin on Thu Mar 12, 2015 11:34 pm, edited 2 times in total.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Thu Mar 12, 2015 5:48 pm

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 :)
B
14
S
4
G
2
Posts: 190
Reputation: 3,938

Post » Thu Mar 12, 2015 9:58 pm

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.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Thu Mar 12, 2015 10:41 pm

I'm not sure to understand very well.
Your problem is that you don't have the sprite displayed on layout 2 ?
B
14
S
4
G
2
Posts: 190
Reputation: 3,938

Post » Thu Mar 12, 2015 10:48 pm

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.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Thu Mar 12, 2015 11:16 pm

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?
Sorry...
B
14
S
4
G
2
Posts: 190
Reputation: 3,938

Post » Thu Mar 12, 2015 11:37 pm

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.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Fri Mar 13, 2015 12:39 am

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.
The moderators are corrupt and ban for no reason, especially that condescending neckbeard asshole Kyatric. The forums are filled with fanboys.
Banned User
B
22
S
7
G
1
Posts: 558
Reputation: 2,925

Post » Fri Mar 13, 2015 12:50 am

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.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536


Return to How do I....?

Who is online

Users browsing this forum: Fib, imhotep22, raulcg1981, shukra and 1 guest