How to switch spaces of 2 things after picking them up

How do I…

I’m making a merge game in a top down style, that has a grid, where you have to work with the limited space, one grid has space only for one thing and i’d like to know, how:
Once you have things summoned on (for example) spaces 1C and 2G and you pick up the one from space 1C and put it in space 2G, the two switch spaces?
all items are draggable
It’s made by having invisible squares over the whole screen, and if you drop something, it finds the closest square to the cursor and aligns itself with it
thank you so much

I would do it with 2 variables let’s call them OriginPosX and Y and checking if tile is occupied

Imagine if tile is free you have its opacity at 255 (this could be done with additional variable for each tile called Occupied and checking if its true of false but i say opacity is enough here for means to check)
When something is on tile you change its opacity to 254
This way you simply have checker if something was on tile or not

Now when you pick up object you set OriginPosX and OriginPosY vars to X and Y position of that tile from which object was picked up

NOW
When you drop it on occupied tile
Object that was on that occupied tile you simply move to OriginPosX and Y vars while item you just dropped would go to that occupied tile

And that would be pretty much all

If you have no means for checking if something was picked or not
Then that would involve another variable

If you would have problems switching places for picked object and object on occupied tile
You would add 2 more variables to temporally store which object needs to go away from occupied tile and which is currently picked

And you would reset them to 0 when nothing is picked

1 Like

thank you so much, I’ll try it out!

I’m very sorry, but could you provide a few screenshots? I’m quite new and have been trying to do it for about 2 hours now
Thank you so much, so sorry for the inconvenience


Problem is my knowledge about inventory is limited
Or let’s face it next to zero
I know how to do parts of it i DID NOT ever made one and i do not know how to combine all my knowledge into making one

Basic idea is
You need some variable to check if ANY object is being dragged so you do not drag other objects by holding LMB or touch and moving over them when you are dragging some object
Then you need to shift some variable or opacity of object you are dragging to mark it as being dragged

Then there is slot manipulation variable
Like you need to check if something is being dragged so you check state of variable i mention at beginning
And if nothing is being dragged and object is in collision with slot
You set variables of all objects to NOT being dragged
And variable of slot to occupied

So now with that you can check when you click on object
Is it in collision with slot or not
Then you can check if such object is in collision with some other slot or not
And is that slot occupied or not

And so go on
I would suggest you check this out Gdevelop 5: BEST INVENTORY EVER! by LevelUpWithAlex

And watch this https://www.youtube.com/@LevelUpWithAlex/search?query=inventory

1 Like