[SOLVED] How to switch coordinates of two objects?

Hello again GDevelop forum!
I’m doing this card game and I’ve stumbled across another problem on my way.

I have a card that slots into its own place, but when another card is placed upon it they should switch places. Card1 that was there should move to the slot the other card was in before.

These are the two cards:
When dropping the 2 on the A like this (dragging it on the other):
This should happen:

I’ve managed to code that if you drag the 2 on the A, the 2 repells and returns to it’s original position.
But how do I code that those two cards switch places when put on each other?
I came up with a solution, but it included the possibility to reference objects in variables which apparently isn’t possible in GDevelop. If that would’ve been the case I could’ve sent the original object name with the other card so it knows who to switch with.

Appreciating any help I can get.
Please include a code snippet or an example with your explanation, since I’m relatively new to GDevelop, it could really help - so I won’t need to ask more questions about what you mean :slight_smile: !
Thanks in advance!

Here’s something that works. You’ll need to set up the following :

  1. You’ll have to have some unique id on each card instance.
  2. You’ll also have to add all the cards to an object group named Card.
  3. And you’ll need to create a new object, the same size as a card, and call it Hitbox.

The events are :


I would create 4 temporary variables that would store the coordinates of the two cards (first 2 variables are the x and y coordinates of the first card, and the last 2 variables are the x and y coordinates of the second card) when you the program senses the card colliding on the other card. Now that you have the two scene variables, the program will move the first card to the coordinates stored in the scene variable created for the other card. After that is finished, the temporary variables get set to 0. Hope this helps.

I’ve managed to do some sort of “spin-off” of your solution - mine is much more complex (not as clean as your) because it had to be edited to fit in my code and to match my other stuff’s coding…

It works, but sometimes it breaks and the cards won’t switch positions with eachother any more.
For example, if I drag a card ontop of the other, they switch, and if I then choose the same card again and drop it on the other same card again - it breaks…

Some code snippets from my project to maybe get someone to understand what’s happening:

When a card gets grabbed/dragged ^

If a card gets dropped on an occupied card-slot ^

If a card gets dropped on a vacant card-slot ^

When a placeholder is emptied of a card / the placeholder gets vacant ^

Please don’t hesitate to ask for more information/context if you think you need it. I wasn’t really sure what to share for you to understand.

Thanks a lot for your help, in advance.

That uses the cards’ current position, not the original position of the dragged card. But otherwise, that idea works fine with card on card using 2 object groups each with all the cards in them (I can’t see how you can do it with one object group).

But how would you do it so that it caters for both scenarios - one scenario with dropping a card on top of another, and the other scenario with dropping a card when it’s not over another card (remember the card needs to return to it’s original position)?

I’m genuinely interested because I don’t think it’s that simple, but would love to see your working solution & code.

Where do you turn on the draggable behaviour? You turn it off when the card has been dropped, but haven’t included or shown the events that turn it back on.

Where do you turn on the draggable behaviour? You turn it off when the card has been dropped, but haven’t included or shown the events that turn it back on.

My bad, I have updated my earlier post to include it.

So you set the draggable behaviour off in one event, and then in the first subevent of that event turn it back on? Why?

I had one thought about that earlier on but now it doesn’t make sense to me either.
I have removed that now.
But that isn’t causing the problem so it’s irrelevant for now.

I should also point out that the variable “firstTime” isn’t relevant either, that’s something that gets coordinates for the card that you touch first - that isn’t something that causes the problem…

The code snippets you’ve provided suggests otherwise - the events you have shown only place the dragged card back to it’s original position. There’s no swapping going on.

I looks like you’re providing the snippets of code that you think are relevant or where the problem lies, whereas the actual issue could be elsewhere in the code. Could you share a larger screen shot of the events?

I’ve removed the behavior-activations you talked about, and it works fine without them.
They weren’t doing anything, I have tested that. But let me give you a screenshot of everything so I don’t miss anything :smiley: !

Second image shows all the code for every placeholder that a card can slot to.
The code is only for the first placeholder, but for every other - the only thing that is different is the variable “placeholder_card1” and the coordinates in “Change the position of Cards…”. (Forgive me for this mess of redundant code, but I haven’t had the time to write a code that works for every placeholder instead of one-by-one - but let’s not focus on that, shall we?).

To give you context:
Cards can be slotted into the hand like this:

If you drop the card over a placeholder (inside the frame in the picture above), then the card slots into the nearest placeholder (the placeholder_cardN that you’re hovering on), see picture below.

The hand looks like this in editing:
Every slot is a different placeholder_cardN.

Thanks for your help and patience, in advance.

Wait, you have a different placeholder object for each placeholder? So you have 12 placeholder objects? And you have events for every card and every placeholder?

I think you can do some serious refactoring here, and reduce your events a hell of a lot. This will also greatly minimise the potential for bugs, like the one you are experiencing. I know you said not to focus on it, but compact code is a lot easier to debug than a mass of events

I’m going to be away from the computer for the next 1/2 day, but I can help reduce the events after that (if no one else does so first).

Would you mind sharing your whole project? PM me with a link if you’re not keen to do it in this thread. It’d make the whole process a lot faster.

This issue is solved.
The fix was to link the cards with the placeholder that the card snaps to and work on layers. I’m not that good at explaining it, but that was the fix.

Thank you all for your help.