How do I spawn random objects?

Get help using Construct 2

Post » Fri May 09, 2014 3:19 pm

Hi guys,

I want to randomly pick one object from a group of objects and spawn it in the level, but I cant seem to find the functionality.

Basically I want to do something like the following:
var a = random(0,1)

if (a=>0&&<0.2) then
Spawn.(Object1)
elseif (a=>0.2&&<0.4) then
Spawn.(Object2)
elseif (a=>0.4&&<0.6) then
Spawn.(Object3)
elseif (a=>0.6&&<0.8) then
Spawn.(Object4)
else
Spawn.(Object5)

How do I do this?

Thanks,
Mat
ImageImageImageImageImage
B
19
S
5
G
1
Posts: 614
Reputation: 2,542

Post » Fri May 09, 2014 3:53 pm

You can do it just like that!

You can randomly generate a new number every Tick and store it in a Global variable. You may trigger these actions (calling funcions) or you may use the same Tick trigger (of course doing this way you will probably consume more resources of the system).

Use Events to compare 1 variable to a number and then Spawn that object. Duplicate this event to proper variable and action.

Let me know if that helps you out.
Cheers!
B
24
S
4
G
1
Posts: 177
Reputation: 1,827

Post » Wed May 14, 2014 8:17 pm

Thank you. I also found the ? operator.
ImageImageImageImageImage
B
19
S
5
G
1
Posts: 614
Reputation: 2,542

Post » Fri May 16, 2014 10:30 am

Yes! Nice!
If you can, post your final solution.. this way others can learn from your hard work! =)
B
24
S
4
G
1
Posts: 177
Reputation: 1,827

Post » Fri May 16, 2014 4:29 pm

So what I did was I created a variable for the each of the object types. When I spawn an object I give it that variable unique number. Then when I spawn the the other object from it I give the same unique number to that variable.

Then whenever I want to grab that a specific instance I add the addition comparison condition: Object type 1 ID number = Object type 2 ID number.

Therefore, when I do something like destroy object 2 I get the instance I wanted.
ImageImageImageImageImage
B
19
S
5
G
1
Posts: 614
Reputation: 2,542


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 21 guests