Save/Load Enemies

Get help using Construct 2

Post » Sat Mar 16, 2013 8:14 am

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!
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Sat Mar 16, 2013 8:41 am

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
B
7
S
2
G
3
Posts: 58
Reputation: 2,284

Post » Sat Mar 16, 2013 8:44 am

The enemies aren't actually spawning, though - they're set in the editor and when you "Load Game", it restarts the layout.
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Sat Mar 16, 2013 9:29 am

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.

B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Sun Mar 17, 2013 4:55 am

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

B
22
S
3
Posts: 451
Reputation: 2,135

Post » Sun Mar 17, 2013 6:52 am

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.

Xavier2013-03-17 07:01:30
B
63
S
23
G
17
Posts: 254
Reputation: 14,342

Post » Sun Mar 17, 2013 7:09 am

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.
B
22
S
3
Posts: 451
Reputation: 2,135

Post » Sun Mar 17, 2013 7:25 am

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: http://www.scirra.com/forum/copy-an-entire-array_topic53153_post332888.html#332888Xavier2013-03-17 07:30:54
B
63
S
23
G
17
Posts: 254
Reputation: 14,342

Post » Sun Mar 17, 2013 8:17 am

[QUOTE=ome6a1717] 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

[/QUOTE]

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 :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Sun Mar 17, 2013 8:32 am

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.

Thanks!
B
22
S
3
Posts: 451
Reputation: 2,135

Next

Return to How do I....?

Who is online

Users browsing this forum: Megavolt, mnashwan, shinkan, stylie1 and 17 guests