How do I make my pawns move correctly?
I want to make a Ludo game with gdevelop. I have used array and tween and also created variables in each square. But when previewed, the pawn just moves straight without looking at the array (I set it to go to array 0 but it goes straight to array 3). Is there anything wrong with my code? And is there any video related to the system of pawn colliding and pawn movement following the dice number? I would really appreciate it. I’m a newbie in making games here, so sorry if I have basic questions . I want to send a video of my pawn movement, but here I don’t know how.
Firstly, don’t use the beginning of scene within a repeat X times. It should be the other way around - a beginning of scene event with the repeat as a subevent.
But that won’t solve your issue. The problem you have is that in the repeat you are adding the same Peta_Biru’s position to Target_X & Target_Y. Nowhere in that event are you changing which Peta_Biru’s position to use. The SceneInstanceCount(Petak_Biru) only returns the number of instances of Petak_Biru are in the scene - it doesn’t repeat over each instance of Petak_Biru.
The solution is a bit more complex. Here’s one that’s a bit more generic so that it can be applied to any board confguration.
I’d suggest you give each Pitak_Biru a unique number identifier named “Step
”, increasing by 1 in the order you’d want the tiles to be travelled. In the image below shows the tile’s “Step
” value. Keep the sequence going in the direction indicated by the arrows and note the tile numbers start at 0 - this is important:
To Dadu object add:
- behaviour “Button States”
- Change the origin to the centre of the object
To Biru object add:
- Behaviours “Button States” and “Tween”
- Variable number “MovesLeft”
- Change the origin to the centre of the object
To Petak_Biru object add:
- Variable number “Step”
Adding the “Button State” behaviour to both Dadu and Biru objects means you can use the Click condition to start the dice roll or player move. Adding a scene text variable “GameState
” (aka Finite State Machine or FSM) will keep track of what part of the playing process the game is at. Setting up the objects as described above and the following events:
will produce this result:
I followed the steps above, but I encountered some issues.
- The pawn’s move is always 1 number above the dice number. Does that code need to be changed?
- The pawn’s move does not want to move diagonally towards another variable
Is there something wrong with my method?
Most likely. In my example project, the 1 image is animation 0, hence setting MovesLeft to the dice animation index + 1. If your animation starts at anything else, then that value will need to be changed.
What do you mean by that? My example was to move it a square at a time from current tile to target tile. Do you want it to move from the current tile straight to the target without going over each tile in between?
Check the value of Step of the tile the pawn should go to but doesn’t (the one pointed to by note #4), and make sure it’s one more than the one the pawn is getting stuck on.