How do I Load Family Object from Json?

Get help using Construct 2

Post » Wed Jul 12, 2017 7:28 pm

I'm making an level editor and I need to save all placed objects in a json file to be able to load later on.
I'm doing it like this:

Save:

LevelArray.Clear()
foreach PlacedObjects > LevelArray.Push( PlacedObjects.AsJSON)
LevelArray.Push( PlacedObjects.Count )
InvokeDownload ( LevelArray.AsJSON )

Load:

LevelArray.LoadFromJson (Ajax.LastData)
objectsCount.Setvalue(LevelArray.At(0)
LevelArray.Pop(front)
Activate group "Set objects from json"
for( max:objectsCount) > CreateObject (PlacedObjects, "layer", 0,0)
Deactivate group "Set objects from json"

Group "Set objects from json":
PlacedObjects . OnCreated > PlacedObjects.SetFromJSON (LevelArray.At(0)
LevelArray.Pop (front)

Placed objects is a family with many different objects, when saving the object state as JSON, does it save which objects it is? Will that be enough for solving my problem?
Or do I need to divide it for each kind of object?
Any toughts on that?
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

Post » Wed Jul 12, 2017 8:06 pm

@newt , @rexrainbow ? Just tagging some people that usually help me and I saw is online now :)
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

Post » Wed Jul 12, 2017 8:16 pm

Image ImageImage
B
169
S
50
G
174
Posts: 8,330
Reputation: 110,804

Post » Wed Jul 12, 2017 9:16 pm

newt wrote:https://www.scirra.com/tutorials/526/how-to-make-savegames

@newt That's a work around to save the whole game state.
But how should I go about saving only these specific objects?
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

Post » Wed Jul 12, 2017 9:21 pm

Image ImageImage
B
169
S
50
G
174
Posts: 8,330
Reputation: 110,804

Post » Wed Jul 12, 2017 9:38 pm

newt wrote:https://www.scirra.com/manual/160/no-save

@newt I really can't think how this can be organized. I would need to have the NoSave behaviour on all objects of my project beside the PlacedObjects family.
It just feels kinda messy, isn't there a way to make something like what I was going to? I might be missing something.
By the way I imagine families don't know which object they are from your answer.
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

Post » Wed Jul 12, 2017 9:58 pm

It's a lot simpler than all the extra stuff associated with loading via json.
Families work best with existing objects, not creating them.
Image ImageImage
B
169
S
50
G
174
Posts: 8,330
Reputation: 110,804

Post » Wed Jul 12, 2017 10:34 pm

LevelArray.Clear() sets every element to zero.
Pointless to use push after that.

Or the array had the size (0,1,1), then push will work as expect, but the clear is pointless
Or the array has elements (10,1,1) then the push will make a 11th X index, clear and push are pointless.

Instead of clear, set the size to (0,1,1). That is a truly empty array.

The AsJSON does not contain the graphics. An array with the AsJSON strings is not enough.
Creating an member from a family creates a random member.
So, the family needs an instance variable identifying the member.
That variable needs to be stored in the array too.

After loading the array, while running trough it
you will have to create/destroy a family member (in a while loop) until it matches that variable. That is not easy, and can eat time. Then set it to the AsJSON string.

Or.
Create the correct sprite (not family) depending on that variable.
That is just a lot of events, 1 creation event for each possible sprite.

Compared to that, Newt's solution is a breeze.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Thu Jul 13, 2017 1:51 am

Thanks alot both of you guys, for the current time @newt 's solution should work fine. Already implemented it and it's all good.
But I won't be able to use that solution as the project grows, I'll need to choose between layouts, backgrounds, sizes, etc.. so saving just these itens info is really what I want.
I was just looking for a way to optimize creating all objects one by one, with many creation events.

I'll try to make something using the instance variable, just not sure yet how to access it. Any help with an example? I mean, once I parse the JSON to an array each index will be an object, I know that all of them are "PlacedObjects(Family)", but how would I acces an instance variable of it?
Any way I can imagine now would be instantiating a PlacedObject(random), set it to the AsJSON string, get the type instanceVar, then destroy it and call another function CreateObject(type, layer, x, y), which returns instanced object UID.
Then pick PlacedObjects by UID using the returned UID and set its info to the AsJSON string from the array?

Any better ideas? This way I would be creating each object twice, not sure on how to use a while loop and have access to the instance variable there.

Btw I was using array with size(0,1,1)..thanks for clarifying that @99Instances2Go .
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, Jackriduk and 24 guests