Random picking of objects, while avoiding some of them

0 favourites
  • 8 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • I am having problems with how to pick a random object on a grid, while avoiding certain objects.

    Imagine a ball in a small grid. The ball moves from one position on the grid to another that is directly above, below, left or right. It can't move horizontally and it can't go out of the grid. I used pick nearest and it works well. Except I don't want it to move to a position in the grid where it had initially came from. For example, I don't want it to move from position A, to B, than to A again. It can go from A to B, to C, than to A again, but should not return to the last position.

    For my example, we can use the UIDs, since all objects on the grid for the ball to move to is always constant and never gets destroyed. The problem is I really have no clue how to set it to pick nearest while making sure to avoid certain objects.

  • Keep track of the path with instance variables, for example give the grid a cooldown variable.

    Then before picking a random instance, pick instances which have cooldown = 0.

    When the ball moves to a spot, give that spot a cooldown variable of two (if it should not be allowed to move there the next turn, but the following turn). At the end of the event you pick all instances with cooldown > 1 and substract 1 from them.

  • yes, make the grids have an instance bool variable that you flag as a place the ball just was.

    then you would pick like this:

    System Pick random Object

    X Object.wasHere (inverted logic)

    then you need to set it back to false when you go to a new grid position.

  • Thanks mindfaQ and jobel! That was what I tried initially as well, mindfaQ, but it doesn't seem to want to choose based on the instance variables. So it really surprised me that booleans work perfectly jobel. Although now I have another issue, once I set it to true, I can't get it to turn false again. I ran a manual debugger and indeed, all booleans that have become true, stay true even though I set it to return to false. Not sure what I am doing wrong here. The capx is below if you don't mind taking a look.

    Another issue I am having, I set it to pick the nearest sprite from spriteb's position + random(-2,+2). I set it like this simply to get a more random and zig zag movement. It seems to work for the most part, except that now when spriteb moves, it looks like it does a little curve movement, which is weird since I only used it to detect and not for the movement.

    Hopefully someone could take a look and point out where I am going wrong.

  • This uses MoveTo. Also, instead of flashing everything I just made it highlight the 'last' position. Your requirements are still a bit ambiguous though. I've understood it as "the ball can move to any position except the one it has just moved from, and not diagonally. It will do this indefinitely"

  • codah, thanks so much! Yup, actually all the other mechanics of the game has been already finished. It was just that when I did more testing I found this section to contain bugs. Again, thanks codah, much appreciated!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • rekjl

    I don't know if it matters anymore, but I also tried to solve your problem. Added a new global variable and made an else statement for gamephase. Also added little comments to confuse you

    I did not use any 3rd party plugins.

  • allavett, haha thanks, It is much appreciated! It is always good to learn new ways to implement ideas, so thank you for showing me something I didn't know! Hehe exp +5

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