How do I stop spawned objects from overlapping

Get help using Construct 2

Post » Fri Sep 08, 2017 11:40 am

Hi

It seems a while since I did this before but I cannot for the life of me remember how. So in my scene or level, I have created spawn points around the area, I then have two other objects which I want to spawn randomly at these spawn points but for the two objects not to spawn at the same place.

So far my logic is called from the on start layout as a function called "spawnobject1" which does the following :

System->repeat x times
System->Pick a random Spawnpoint instance-> Spawnpoint ->spawn object 1 on layer 0 (imagepoint 0)
Spawnpoint->destroy


When this has finished from this function I have a blank sub event which calls the second objects function and that does the same as above with that object, however even though I am destroying the spawn point used, the second object stills ends up at the same position as the first
Regards

TimCS
B
23
S
8
Posts: 132
Reputation: 1,921

Post » Fri Sep 08, 2017 12:00 pm

When do you call the second function? Is it immediately after the first one? Try inserting Wait 0.1 before calling the second one.
As far as I remember, object instances are not destroyed straight away, it happens on the next tick.

As another workaround you can add an instance variable SpawnEnabled to your spawnpoint and use it for enabling/disabling spawning at each location.
Check out my awesome game Doptrix
B
17
S
9
G
27
Posts: 502
Reputation: 15,803

Post » Fri Sep 08, 2017 1:50 pm

dop2000 wrote:When do you call the second function? Is it immediately after the first one? Try inserting Wait 0.1 before calling the second one.
As far as I remember, object instances are not destroyed straight away, it happens on the next tick.

As another workaround you can add an instance variable SpawnEnabled to your spawnpoint and use it for enabling/disabling spawning at each location.

Thanks for the reply dop2000, the second one is called after the first had finished via a blank sub event so I will add a wait before hand and try that
Regards

TimCS
B
23
S
8
Posts: 132
Reputation: 1,921

Post » Fri Sep 08, 2017 2:41 pm

Hey @timcs!

Not a big fan of Wait actions... I'd stick to @dop2000 's second solution, having a variable that tells which spawn points were used... :)

Cheers!
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
19
S
5
G
2
Posts: 534
Reputation: 3,235

Post » Fri Sep 08, 2017 4:10 pm

brunopalermo wrote:Hey @timcs!

Not a big fan of Wait actions... I'd stick to @dop2000 's second solution, having a variable that tells which spawn points were used... :)

Cheers!



Hey @brunopalermo would this variable be assigned to the spawnpoint object ? and then if it was true e.g. used then don't use that spawnpoint ?

** EDIT **

I think I have got this to work, my only problem now is that there are 24 spawnpoints and if I specify 12 for each of the objects to repeat over, it does not always pick them all or if I do spawnpoint.count/2 it still misses some out.

Any ideas the best way to do this ?
Regards

TimCS
B
23
S
8
Posts: 132
Reputation: 1,921

Post » Fri Sep 08, 2017 5:15 pm

Hey @brunopalermo would this variable be assigned to the spawnpoint object ? and then if it was true e.g. used then don't use that spawnpoint ?


Exactly. Using instance variables with spawnpoints you can do lots of things - mark them as used, temporary enable/disable them, keep track or set limit of how many objects (let's say enemies) they spawned, specify which type of enemies each spawnpoint can produce, set an order in which they will be selected for spawning etc.
Instance variables is a powerful stuff :)

In your case, if you have 24 spawnpoint and would like, for example, half of them to spawn zombies and half to spawn vampires, you can add an instance variable EnemyType, set EnemyType="z" for 12 spawnpoint and EnemyType="v" for the remaining 12 and then do something like this:

Image
Last edited by dop2000 on Fri Sep 08, 2017 5:22 pm, edited 1 time in total.
Check out my awesome game Doptrix
B
17
S
9
G
27
Posts: 502
Reputation: 15,803

Post » Fri Sep 08, 2017 5:18 pm

dop2000 wrote:
Hey @brunopalermo would this variable be assigned to the spawnpoint object ? and then if it was true e.g. used then don't use that spawnpoint ?


Exactly. Using instance variables with spawnpoints you can do lots of things - mark them as used, temporary enable/disable them, keep track or set limit of how many objects (let's say enemies) they spawned, specify which type of enemies each spawnpoint can produce, set an order in which they will be selected for spawning etc.
Instance variables is a powerful stuff :)

In your case, if you have 24 spawnpoint and would like, for example, half of them to spawn zombies and half to spawn vampires, you can add an instance variable EnemyType, set EnemyType="z" for 12 spawnpoint and EnemyType="v" for the remaining 12 and then do something like this:

Image


So in your example is the enemytype set against the enemy object or the spawnpoint or both?
Regards

TimCS
B
23
S
8
Posts: 132
Reputation: 1,921

Post » Fri Sep 08, 2017 5:26 pm

So in your example is the enemytype set against the enemy object or the spawnpoint or both?

EnemyType instance variable is defined on the spawnpoint object. And then you set it either to "z" or "v" for each of the 24 spawnpoint instances that you have.
You can do something similar with enemies objects - create a bunch of instance variables to store enemy type, health, damage, armor etc. etc..

Note, that I updated the picture in my previous comment, there was a slight mistake - you need to first pick spawnpoints by EnemyType and then loop through them.
Check out my awesome game Doptrix
B
17
S
9
G
27
Posts: 502
Reputation: 15,803

Post » Fri Sep 08, 2017 8:25 pm

dop2000 wrote:
So in your example is the enemytype set against the enemy object or the spawnpoint or both?

EnemyType instance variable is defined on the spawnpoint object. And then you set it either to "z" or "v" for each of the 24 spawnpoint instances that you have.
You can do something similar with enemies objects - create a bunch of instance variables to store enemy type, health, damage, armor etc. etc..

Note, that I updated the picture in my previous comment, there was a slight mistake - you need to first pick spawnpoints by EnemyType and then loop through them.


Hi dop2000 , I must be missing something with my logic here because using your example (the updated one) no objects are being spawned :(

How does the enemytype variable logic work , if I am calling this by a function, does this need a parameter?

** EDIT **

Found the issue missed out the * loopindex after the wait 0.5 doh! . However with your logic the overlapping issue has come back, should I still be using the used true/false or destroy options ?
Regards

TimCS
B
23
S
8
Posts: 132
Reputation: 1,921

Post » Sat Sep 09, 2017 2:56 am

Please download this demo I created:
https://www.dropbox.com/s/0jptlam49u7ugse/spawningDemo.capx?dl=0
I hope it answers all your current and future questions :)
Check out my awesome game Doptrix
B
17
S
9
G
27
Posts: 502
Reputation: 15,803

Next

Return to How do I....?

Who is online

Users browsing this forum: Darknessed, FabianB and 45 guests