Is it possible to spawn an object from JSON?

0 favourites
  • 4 posts
From the Asset Store
Welcome! I-Spy (Hidden objects) is an educational puzzle that is more than just seek-and-find activities.
  • I'm aware of the ability to save an object's properties as JSON, but do those properties store the type of object it is as well?

    I have a family of "enemy" sprites that I'd like to import/export using JSON. I can save the JSON of each enemy without an issue, but when attempt to restore them all, there's no way for me to know which JSON object belongs to which member of the family.

    Is there a way to work around this? Does the JSON object even store the information I would need to make this work?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You should add a "type" tag to your JSON. Type can be "enemy" or whatever other types you have.

    Then just check the type on the way through the JSON and compare to the string "enemy" and only operate when that condition is true.

    Like:

    {
    type: "enemy",
    name: "jabi-jabi",
    stats: {
      attack: 22
      defense: 42
    }
    }
    [/code:2ty3d8jg]
  • You should add a "type" tag to your JSON. Type can be "enemy" or whatever other types you have.

    Then just check the type on the way through the JSON and compare to the string "enemy" and only operate when that condition is true.

    Like:

    {
    type: "enemy",
    name: "jabi-jabi",
    stats: {
      attack: 22
      defense: 42
    }
    }
    [/code:3fznuhw6]
    

    Thanks for the tip. I already figured out a workaround, in case anyone is curious:

    Since my enemies exist on a tilemap, I loop through the tilemap and pop the JSON of each enemy on to the back of my array when exporting a level. On import, I loop through the tiles again, creating an enemy depending on what tile it hits and activate an event group that contains an "On enemy created" event, so each time an enemy is created at a tile, it loads the first object JSON in the array and removes it. Since I loop through the tiles in the same order on import and export, the order of the JSON objects is always consistent when importing back in.

  • Workarounds are good too.

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