I’m trying to make AI that will choose a random point to move to. The point is part of the object points, and I’m not sure how to make it randomly choose a point.
Can you clarify? Does the object have a number of points on it, and one gets picked at random? Or do you want to move to a point of a random object?
The object has different points on it, and I want one to get picked at random. I’m trying to make something similar to a chess bot, and I thought the best way would be to point points on it to move to. Since choosing a random position doesn’t work.
I don’t know if there’s a better way around making something similar to a chess bot, but for now I want to choose a random point from the object itself.
Hope that makes sense
I’d probably just put them into a naming system and use a Random expression:
- Name them Point0, Point1, Point2, Point3, etc.
- On your event to move them, use a local string variable named “Destination” or something like that.
- Set the local variable to “Point” + ToString(Random(3)), every time the event is run it’ll set it to “Point0” through “Point3” randomly.
- As your second action, move your object to ObjectName.PointX(Destination) and the same for y.
If i understand it right, you want to do something simple in an difficult way by using ‘points’ of the object.
I’ll show the easier way,
the scene variables used:
the scene tab set-up:
It is not impossible to do it with points, but know that points X and Y aren’t the same as X and Y positions on the scene screen.
Do to the same you would need to add and subtract then add the movementRange variable:
It would look like this image below, which is just unnecessary.
A mistake in the image, it should not be ‘=set to’ but ‘+add’
I don’t think you understand how sprite points work. When you access them using “PointX("point_name"))
”, they give back an absolute position, not a relative one that you are suggesting.
And I assume there’s another typo in the screen snip - subtracting chessSprite.PointX(“right”) from itself is 0.
What Silver-Streak suggested is a simple and elegant solution. It could even be done in one action - ObjectName.PointX(“Point” + ToString(Random(3))
[edit] ignore that last paragraph. It won’t work because the same randomly generated value need to be used for both the X & Y points. A variable is the only way to achieve this in GDevelop.
You are right, i might not completely understand points yet and made an assumption too quickly.
After emulating Silver_Streak suggestion it works, albeit the initial movement causes an odd teleportation at first when i tried it.
But it is the solution to the question of using points for movement.
I tried it and it worked! I messed up by changing the position to (“Point” + ToString(Random3)), until I realized you wrote Destination instead. Thank you so much!!
Do you guys know if it’s possible to make the engine generate lists of possible moves? By possible I mean valid moves, and I know how to make the engine recognize if it’s valid (e.g. if the piece should go to that position or not). I just don’t know how to make it so it can calculate all possible moves of each piece.
What are the rules for a valid move or position?
The highlight is supposed to indicate where the player can move. But in the events it’s basically, if the left button is released and its on the object, move the piece to the cursor location. If the object is moved, lets say upper right, where there is no highlight, the move is invalid, and change it’s position to its StartX and StartY. The “Invalid” is a boolean that changes other booleans. Like when the object is first clicked, and moved for example.
I’m sure there is a better way to do this but I’m not sure as of right now how to go about it. With the way it is set up right now I’m sure it’ll be hard to generate a list since a valid move is being checked AFTER it is moved.
Hope that makes sense!
And the blue squares are the highlighted ones? Why don’t you check if a blue square is clicked on before moving the piece? Then you don’t have to worry about the startX and startY.
The blue squares are supposed to be tiles you can’t move to. Its supposed to be water.
Ok, so change what I wrote earlier to Why don’t you check if a highlighted square is clicked on before moving the piece?
Because its another animation of bow. It’s not separate from the bow. So when you click on the highlighted squares, you’re really clicking on the bow object.
I do check for when the player clicks on the object, I’m just not sure if there is a more efficient way of checking if a move is valid. And if there is I’d like to know how to go about it. Like for now everything works fine, and I’ve fixed a lot of bugs. It’s just like i said earlier, if there is a more efficient way of checking.