How do I spawn random objects?

0 favourites
  • 5 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • 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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you. I also found the ? operator.

  • Yes! Nice!

    If you can, post your final solution.. this way others can learn from your hard work! =)

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

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