Spawn random number of objects

Get help using Construct 2

Post » Fri Mar 02, 2012 4:01 pm

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
B
5
Posts: 20
Reputation: 640

Post » Fri Mar 02, 2012 4:14 pm

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
B
6
S
1
G
1
Posts: 15
Reputation: 938

Post » Fri Mar 02, 2012 10:11 pm

[code]+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[/code]
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Mar 06, 2012 10:21 am

@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
B
5
Posts: 20
Reputation: 640

Post » Tue Mar 06, 2012 12:12 pm

> 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.TELLES08082012-03-06 12:15:29
ImageImageImageImageImageImage
B
93
S
20
G
12
Posts: 1,212
Reputation: 18,482

Post » Tue Mar 06, 2012 1:53 pm

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.
B
5
Posts: 20
Reputation: 640

Post » Tue Mar 06, 2012 2:24 pm

@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:[code]+System: Every 7 seconds[/code]All the following will be triggered every 7 seconds
[code] Local number rndCount = 0[/code]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
[code] -> Object: destroy[/code]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)
[code] -> Spawnpoint: set occupied to false[/code]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
[code] -> System: rndCount = floor(random(5))+1[/code]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.
[code] +System: repeat rndCount times[/code]All the following will be triggered rndCount times before even updating the display. So you'll see rndCount 'Object' instantly appear randomly placed
[code]    +Spawnpoint: [invert] is occupied[/code]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.
[code]    +System: pick random Spawnpoint instance[/code]Amongst the filtered Spawnpoint (the order of condition is then super important), I pick a random one.
[code]      -> Spawnpoint: Spawn object on layer 3[/code]I make the randomly picked inoccupied Spawnpoint, spawn the object at its place.
[code]      -> Spawnpoint: set occupied to True[/code]You set the occupied boolean to true to flag the Spawnpoint as occupied and avoid picking it again.

[code]+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[/code]




Yann2012-03-06 14:26:22
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Mar 06, 2012 3:46 pm

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


B
5
Posts: 20
Reputation: 640

Post » Tue Mar 06, 2012 4:52 pm

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.Yann2012-03-06 16:52:41
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Mar 07, 2012 10:08 am

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?
B
5
Posts: 20
Reputation: 640

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 21 guests