Random picking of objects, while avoiding some of them

Get help using Construct 2

Post » Tue Dec 02, 2014 4:27 pm

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.
B
46
S
23
G
31
Posts: 693
Reputation: 20,637

Post » Tue Dec 02, 2014 5:17 pm

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.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Dec 02, 2014 5:32 pm

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.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Wed Dec 03, 2014 8:56 am

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.
You do not have the required permissions to view the files attached to this post.
B
46
S
23
G
31
Posts: 693
Reputation: 20,637

Post » Wed Dec 03, 2014 12:06 pm

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"
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Wed Dec 03, 2014 12:59 pm

@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!
B
46
S
23
G
31
Posts: 693
Reputation: 20,637

Post » Wed Dec 03, 2014 1:04 pm

@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 :lol:
I did not use any 3rd party plugins.
You do not have the required permissions to view the files attached to this post.
B
13
S
2
G
1
Posts: 24
Reputation: 953

Post » Wed Dec 03, 2014 3:41 pm

@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
B
46
S
23
G
31
Posts: 693
Reputation: 20,637


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests