How do I make an object remember its previous position

I’m trying to make a game similar to chess, imagine rock paper scissors and chess basically.

The issue I’m running into is that when a player makes an invalid move, I want the player to return back into their previous position. An invalid move also includes when a player clicks a space that already has another piece there.

I’ve already tried drag and cancelable drag, and it would make both pieces return to their respective previous positions. This time I’m trying to make it to the click where they want to go (if its valid obviously)

Add 2 variables to the object to store the X & Y positions. Before you move the piece, store the current position in those variables. If you need to return to the original position, you have it in the variables.

How do I make it so that those variables store the position every time its position is changed?

I believe I answered it before with this, but correct me if you’re not sure about it:

So, when the player clicks on the pieces, that’s when the position would be recorded? That makes sense, and sorry for not realizing it before, thanks!! I’ll try it out right now!

Also, I have another question, I want the player to click on the piece twice. One time to choose the piece they want to move, and the second time is for when they actually move the piece. This would then make it the other players turn (if the move is valid).

I’m using a Boolean for switching turns and number variables for the clicking. But right now, when it is clicked the one time, it immediately turns into the other players turn. Do you know how I would fix that?

There are a few ways.

  1. You can use a text variable to track the step the click is at. So initially set it to something like “waiting”, with subsequent values “firstClick” and “secondClick”. When the mouse is clicked, check that text variable, set it to the next step and act on it.

  2. You could use 2 booleans, one for first click and one for the second. Reset both when the second click as been actioned.

I tried the string (text) version of your suggestion and it works! The only issue is that when the same object collides, for example two blue bows, they both move when I used the variable of X and Y, instead of one. Is there a way to only move one to their previous position? Also, when they are supposed to move to its previous position, they move to someplace else.

I don’t know where I made the mistake or how to fix it T-T

When you check for BlueBow colliding with BlueBow, how does GDevelop know which BlueBow to move? It doesn’t, and the actions are performed on both of them.

To avoid the 2 pieces moving back, flag the one that been clicked. Add a boolean variable to BlueBow, say called “IsClicked”. Then change your “Clicks = 'Zero'” events to:

I did a quick image edit to generate that “screensnip”. Now you’ll have to do a couple more things:

  1. In the middle/second event (where mouse button released is checked) add the action to set BlueBow.IsClicked to false.

  2. In the bottom/third event (where cursor is on BlueBow is checked) add the action to set BlueBow.IsClicked to true.


Then in this event:

add a subevent and add all the actions from the event in the screen snip into that the subevent. To the subevent add the condition “BlueBow.IcClicked = true”.

I think that should fix the issue.

I did everything but for some reason after both pieces have made their move, once its blue’s turn again, I cant move the piece.

If i put an event before “left mouse button was released” thats says “cursor is on bluebow”, i can click on it again, but then it has the same issues as before. I’m not sure if I did something wrong T-T.

And thank you so much for bearing with me and helping me with the game!!! I’m sorry if I’m bothering you at all!!!

Where did the "Set variable Clicks to 'Zero'" go to in that last screen snip?

I had it on, in that screenshot I had moved it into a disabled event. I only moved it because I wanted to check if it would work without it. Ultimately it still had the same issue as I described. I have it in the event just not in that screenshot.

Do you set “Clicks” to “Zero” and “BlueTurn” to true once red has completed their turn?

Also the second condition unnecessary - it’s just checking if a GameBoard Id is equal to itself. The first condition will pick the GameBoard object under the cursor, which will be enough for the condition:
image

Yes, once reds turn is done, it changes into blues turn. Reds events are the same the blue was used to be when I first asked for help btw.

I also put “bluebow collision with bluebow” under blue’s turn, to see if it’d do anything different, and for now it hasn’t changed the outcome. What it did do was change to red’s turn, which it shouldn’t, so I placed it outside of blues turn again.

I also have this in the beginning of the scene, and when I moved it into a disabled event, the blue Bow would go off screen.