0 Favourites

How do I Load Family Object from Json?

  • 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?

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • newt , rexrainbow ? Just tagging some people that usually help me and I saw is online now

  • 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?

  • 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.

  • It's a lot simpler than all the extra stuff associated with loading via json.

    Families work best with existing objects, not creating them.

  • 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.

  • 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 .

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