Random Object outside of collision area

I have a background which have many collision rectangles (something like isometric game example). now I want randomly yo create object where the object will be created only inside the allowed move area so the player can collect it. How I can achieve this.

You can achieve it this way:

At the beginning of the scene iterate along all the collision rectangles objects with a For Each loop, in the For Each loop create an ID variable for each collision rectangle and put a number in it (you know, serializaed… 1, 2, 3, 4, 5… 98765, etc). When you want to create an object just pick up a random number with the RandomInRange() event and then iterate trough all the collision rectangles again searching for the one with that ID. then create the collectible at the coordinates of the collision rectangle with that specific ID.

Thank you erdo-dp.
First I cannot find a way to iterate for each collision rectangle. Then if I could do that still the how to avoid creating the item outside the collisions.
as you said “create the collectible at the coordinates of the collision rectangle with that specific ID” this will create the item inside the collisions not outside.

Try a while loop:

[code]Conditions: Conditions to create the Object
Actions: Create Object at Random(800) ; Random(600)

While Object collides with Background:

    Conditions: No conditions
    Actions: Do = Random(800) ; Random(600) to the position of Object[/code]

I.e. keep changing the position to a random one until it doesn’t collide :slight_smile:

As always, make sure the while condition can fail or you’ll end up with an infinite loop. In this case there should be a good range to be in a non-colliding position.

Well this also might end with infint or a long loop until the random fall in the right position, I am not sure if this is a good idea to keep looping.

Yeah, that’s what I meant with “a good range for non-colliding”. Not only the colliding vs free area ratio is important but also the object size: if the free non-colliding areas are almost the same size than your object then you’re restricting the free position to a very very small range, hanging your game for even several seconds.

But you can give it a try, if it doesn’t work you’ll have to rethink it, instead adding obstacles and picking free spaces randomly you can create spawn objects, pick a random spawn object and create your object there :slight_smile: