Save/Load Enemies

0 favourites
  • 14 posts
From the Asset Store
Progress\Loading Bar. Now Load you game like a Professional.
  • I have a game where enemies are placed in specific spots on purpose. When I kill an enemy, save, and load that game, the enemies re-spawn (for obvious reasons).

    Is there a way around this without having to create billions of global variables?

    I am using WebStorage to save the current variables.

    Thanks in advance!

  • hey you might want to set group event for each enemies you are spawning and jsut create an varable event where If variable is Greater then or less than enable group event or disable group event. This might take a bit of coding but it might just work

  • The enemies aren't actually spawning, though - they're set in the editor and when you "Load Game", it restarts the layout.

  • You could do something like this (custom ID set in editor would be safer than using UID's I imagine, they could potentially change between runs).

    GlobalVariable string DeadEnemies = ""

    On enemy destroyed

    .. append Enemy.UID & "|" to DeadEnemies

    On game load event

    .. for 0 to tokencount(DeadEnemies, "|") times

    .. pick Enemy with UID tokenat(DeadEnemies, loopIndex, "|")

    ... Move enemy outside layout, make it invisible or whatever you wish to do to hide it.

  • Hey Vee,

    Thanks for the response. Sorry to sound ignorant, but can you kind of walk me through what each of these lines are doing? I'm a bit unfamiliar with tokencount/tokenat/loopindex/append :P

  • Edit: Actually this method won't work because I don't think you can save instance variables.

    If I understand correctly, you want to destoy the enemies that were already killed prior to saving right?

    Just create a new instant variable on the enemies object (or family), name it someting like "death", and when you kill an enemy, change that variable to 1.

    Then make a new action on layout start to destroy the enemies that have that variable to 1.

  • But when you restart the layout, it resets all of the instance variables. That's the issue - the only true way to do it (in my head at least) is to create a global variable for each enemy on the layout and once they are killed, set the global variable to 1 and destroy the enemies on layout start that way.

  • You should do something like Vee41's idea then.

    Using an array could be another idea since you can save a whole array in webstorage. Just create an instance variable on the enemies called ID, and in the editor manually change that variable for each enemy: 0, 1, 2...

    Then when you kill an enemy, set the array's X (get the enemy ID) to 1.

    And when you load the game, on layout start you can add the Array condition "Compare X" with the enemy ID.

    Anyway if you don't understand it I can explain it better, it's something I use in my game to save data.

    To save an array use this simple method:

  • Hey Vee,

    Thanks for the response. Sorry to sound ignorant, but can you kind of walk me through what each of these lines are doing? I'm a bit unfamiliar with tokencount/tokenat/loopindex/append :P

    Heres an example I made earlier, pretty much the same topic: Selective destroy example

    Hopefully you can see the general idea there, it's quite simple really :)

  • I like the idea of using an array, but I've never used them before, so bare with me.

    Here is what I did (and maybe you can tell me what I'm doing wrong):

    When an enemy is killed, I'm using the following event:

    Enemies > On Destroyed

    _____________-Array > Set Value at Enemies.ID(instanced variable) to 1

    On the save button, I'm using:

    Webstorage > Set Local Key "Array" to Array.AsJSON

    On load:

    Array > Load > JSON string "Array"

    When the layout start, I created a subevent:

    Array > Value of Enemies.ID = 1

    For Each Enemies

    ____________- Destroy Enemies

    The other thing is that I want all enemies to respawn when you leave the level and go back into the level, so I added a "Array > Clear" event for that, but it doesn't seem to be working either.


  • Well just in case I made you this capx:

    You should be able to modify it to your needs.

  • Hey Xavier,

    For some reason the CLEAR button doesn't seem to actually set the array to all 0's Is this a bug in C2, or does it not do that?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads

    Got it working. My issue was trying to load "Array" instead of WebStorage.LocalValue("Array").

    Thanks Xavier and Vee!

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