Trying to swap instances

I think it’s a good idea not to go with draggable behavior. For one thing there will be less complexity in making sure your players don’t drag candy to inappropriate places all over the game screen. They are really only going 1 block over. They don’t need to actually have to pick up a piece of candy and physically carry it to the box they want it in. It’s right there.

I’m working on a project that involves swapping places too and at this stage I think changing positions with a simple tween would be better, unless I decide to go with a multi animation single object later. But since I am using different game objects for each swappable item right now I think our swapping is similar so far. But I put all my swappable items in a group and just call the group instead of the individual swappable items. I use the Swipe Gesture extension to detect when swipes end. I also use the Pinch Gesturing extension as a simple and lazy way to tell when the player has more than one touch on the screen so I can prevent swaps from happening then (prevents the incorrect object picking that can occur with multiple touches). I make positioning blocks so I know what pieces to swap and where.

Here are the events I’m using so far. Just ignore the notes, I don’t get a lot of time to work on my projects so I like to keep notes so I don’t spend all my time wondering what I was doing or why on earth I’m doing it this way and not that way. So hopefully you can find some inspiration or ideas.

Summary

This is just a placement image so you can better see where the 3 main event groups are.

This main event group is selecting the piece to swap

This one is selecting where to swap it (using swipe)

And this is just some extra utility where I delete all blocks and center if there is more than 1 touch on screen, and also delete blocks immediately after creation if they are not on the game board, so the player can not swipe pieces off the board

1 Like

Wow wee comforting to know that there are other game developers out there willing to help me out. Games are a great way to keep people on your website. I would love to make games to put on company websites.

I took a quick look at your code but it is late at night so I will have to take a closer look tomorrow.

Thanks for the input.

1 Like

Well, I don’t even know if it will help. It’s just swapping positions, I haven’t created the rules for detecting if a swap is valid or not. But it might give you some ideas anyway.

1 Like

I tried the cursor/touch is on event instead of dragging the instances. It kind of works…but it has the same troubles as the dragging code, it leave a blank space sometimes…and you can drag the instance all over the screen.

I’ll try something else.

Going to try a suggestion from Drona. How do i change an object into another object? - #2 by Drona

The easiest way to do that would be to store the coordinates of object A, delete it and create object B at the stored position of object A.

Another way would be to have different animations (with your object A and B) for the same sprite and change the animation you need in the events.

Tried the delete/create thingy. Still not working.

Do your candy objects have draggable behavior? I can’t think of anything else that would let them drag all over the screen.

If I delete this behavior I can not drag the instances.

Well if you want to have them draggable then keep it. You can try using the “was just dropped” condition on your candy to know when the player drops it on the rainbow.

1 Like

I have been stumped. I can not find a solution. Is it me or the app?

Ahaha… I suspect that it is something about “object picking”. A concept with the app which at first seems easy, but actually has many subtelties. If you which to investigates this possibily, there is in the expression editor a way to know how many objects of a specific type are currently selected.

Can you post your whole project somewhere ? I might have time to have a look this friday.

1 Like

Trying something different. I will post if it works. I find a lot of concepts difficult to understand, like the sub-events, the 2 different collisions and the force with direction…

Almost works, :smiley: :smiley:
will post soon

1 Like

Here’s a working solution of swapping 2 objects:

FlowerSwap


Scene variable setup:

image


Events:


Scene setup:

image

Note, each GridCell and Flower object have the origin moved to the centre, The editor grid cells are 40 pixels apart, hence the distance <= 80 check in the events.


FlowerGroup is an object group with all the Flower objects in it:

image

1 Like

I can understand that…

If it helps :

  • The eventsheet is read every frame (60 times per seconds), from top to bottom.
  • An event (a line) is made of 2 parts :
    • the left part, the condition(s),
    • and the right part, the action.
  • If the condition(s) is true, then the action is executed.
    But the condition does something else very important, which is Object picking ! Consider it like a filter : only the objects which have been picked (for which the condition is true) are available to the actions on the right side.

For exemple, if you check the collision between the PurpleCoin and the GreenCoin, on the right side, the actions will be applied to the Purple et Green coins which collide.

  • Now come the subevents :
    A subevent is only tested if the condition of the event it is attached to is true.
    AND it is only applied to the objects which have been picked in the event it is attached to.

For exemple : a subevent attached to the one described previously, will only perform the condition on the Purple and Green coins which collide (which had been picked in the previous event).

  • And this is a huge difference with an new event, created below another but which is not a subevent ! Contrary to a subevent, this new event would be tested independently of the previous one (whether the conditions of the previous one are meet or not), and it will take into account every objects (the object picking from previous event is reset).

Said differently : a condition also filters the objects available for the actions. A subevent adds a new filter to the previous one, while a new event (which is not a subevent) resets the filter.

I hope that it makes sense :sweat_smile: It is a key concept in Gdevelop, which is easy to miss when starting. And which very different from most other programming tools.

1 Like

Hi, I moved this topic to the correct section of the forum “Gdevelop general / How do I…?”

1 Like

Wow wee I have to try it out, tomorrow. Thanks for all your help.

This seems to work better, but still stops working sometimes???

I thought the first global variable EndX and EndY would record the first position of purplecandy, but it follows purplecandy and returns the position of where purplecandy ends up. The the second global variable StartX and StartY records the first position of purplecandy???