How do I stop spawned objects from overlapping

  • 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

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

  • 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

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

  • [quote:3rgefq4z]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:

  • [quote:udk3m9am]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:

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

  • [quote:2onqbms2]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.

  • [quote:1vrwfzku]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 ?

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

    Thank you again dop2000 I really appreciate your help with this, I will try not to bother you again

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