Spawn random number of objects

0 favourites
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hey guys i cant figure out a problem.

    I have objects spawning every 7 randomly at one of the 5 spawn points. But i also want the amount of objects to randomly spawn between 1 and 5. But i dont know how. This is what i have now:

    ->system every 7 seconds

    ->system pick random (spawnpoint) instance

    +spawnpoint, spawn object on layer 3

    If you guys could help me, that would be awesome.

    Thanks in advance

  • Use System.repeat!

    set the number of repetitions to random(5)

    so:

    ->system every 7 seconds

    ->system pick random (spawnpoint) instance

    ->system.repeat random(5)

    +spawnpoint, spawn object on layer 3

  • +System: Every 7 seconds
    Local number rndCount = 0
      -> Object: destroy
      -> System: rndCount = floor(random(5))+1
      +System: repeat rndCount times
        +Spawnpoint: [invert] is occupied
        +System: pick random Spawnpoint instance
          -> Spawnpoint: Spawn object on layer 3
          -> Spawnpoint: set occupied to True
  • mrchay i've tried what you said but it didn't work.

    Yann i am bit of a beginner when it comes to construct and i've tried to understand your code but i simply don't xD

  • variable_point(1) >> set variable_point(1).(x,y);

    variable_point(2) >> set variable_point(2).(x,y);

    variable_point(3) >> set variable_point(3).(x,y);

    variable_point(4) >> set variable_point(4).(x,y);

    variable_point(5) >> set variable_point(5).(x,y);

    variable_spawn(1) >> set variable_spawn(1).animation ("animation 1");

    variable_spawn(2) >> set variable_spawn(2).animation ("animation 2");

    variable_spawn(3) >> set variable_spawn(3).animation ("animation 3");

    variable_spawn(4) >> set variable_spawn(4).animation ("animation 4");

    variable_spawn(5) >> set variable_spawn(5).animation ("animation 5");

    *You can loop it if you want...

    every 7 sec >>

    set variable_point = int(var(1,6));

    set variable_spawn = int(var(1,6));

    variable_point = var >>

    spawn (variable_spawn) on variable_point(var).(x,y);

    Also, you can check if the point is overlapped by something and miss it then.

  • hey thank you for your response TELLE, but again i don't understand this code. I am very sorry but i don't understand variables that well.

    This i what i can get out of your code.

    i should make 5 global variables called variable_point(1) - variable_point(5). set them at a x, y postion and after that i lost it.

  • Wreckless

    My code is pretty straight forward and work as you asked :

    You have place spawnpoints in your layout. Just some instances of an sprite named 'Spawnpoint

    'Object' is the object you want to randomly spawn.

    Then it goes like this:

    +System: Every 7 seconds

    All the following will be triggered every 7 seconds

      Local number rndCount = 0

    I create a local variable. This local variable is accessible only under the System: Every 7 seconds event I just talked about and reset to 0 each tick

      -> Object: destroy

    I destroy the object I placed on the canvas to begin with a clean slate (but maybe you want to keep them, in this case you should only put this line under a System: On start of layout to delete the default one)

      -> Spawnpoint: set occupied to false

    I forgot to add that, it's important to reset the boolean to false to be able to pick the spawnpoints. But you shouldn't add this line if you don't destroy the Object

      -> System: rndCount = floor(random(5))+1

    Our aforementionned local variable gets set to a whole number between 1 and 5

    This number is used to know how many repeat loop we will trigger and consequently IS the number of 'Object' that will be randomly spawned.

    Maybe using a local variable is not necessary and you can directly put the floor(random(5))+1 in the following repeat loop. But I wasn't sure.

      +System: repeat rndCount times

    All the following will be triggered rndCount times before even updating the display. So you'll see rndCount 'Object' instantly appear randomly placed

        +Spawnpoint: [invert] is occupied

    Spawpoint has an 'occupied' boolean variable. I use this variable to filter only inoccupied spawnpoints to avoid two objects being spawned on the same spawnpoint.

        +System: pick random Spawnpoint instance

    Amongst the filtered Spawnpoint (the order of condition is then super important), I pick a random one.

          -> Spawnpoint: Spawn object on layer 3

    I make the randomly picked inoccupied Spawnpoint, spawn the object at its place.

          -> Spawnpoint: set occupied to True

    You set the occupied boolean to true to flag the Spawnpoint as occupied and avoid picking it again.

    +System: On start of layout
      -> Object: destroy
    +System: Every 7 seconds
    Local number rndCount = 0
      // optional //
      -> Object: destroy
      -> Spawnpoint: set occupied to False
      //////////////
      -> System: rndCount = floor(random(5))+1
      +System: repeat rndCount times
        +Spawnpoint: [invert] is occupied
        +System: pick random Spawnpoint instance
          -> Spawnpoint: Spawn object on layer 3
          -> Spawnpoint: set occupied to True
  • Thank you for taking the time to explain your code. But i'm still stuck at the local variable part. i drag and dropped a global var in the system event. But i can't change the bolean nor -> System: rndCount = floor(random(5))+1. i do that by adding a system: set value and then selecting the local var right? But the local var isn't showing up

  • you have to nest your event first under the same level as the local variable.

    click and drag the entire event underneath the parent event and slightly to the right.

  • I still can't see it, but maybe if i post a capx you can see what the problem is, but do you know how i post a capx on the forum?

  • Host it on dropbox and post the public link in this topic.

  • Thank you Kyatric for the link! Here is the capx i made to explain my problem. Again im a complete noob so if my questions are rather dumb im sorry for that.

    dl.dropbox.com/u/52744240/spawn%20test/spawnpoint%20test.capx

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Are you missing how to use sub-events ?

  • Again thank you kyatric i thought by adding a condition it was a sub event but i should have done add sub-event. thank you! but i still run into the problem of the set bolean i can post my test capx again:

    dl.dropbox.com/u/52744240/spawn%20test/spawntest.capx

    i hope this is the last revision so i dont have to bother you again.

  • randomSpawn.capx

    You should really read the manual and some tut... seriously

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