How do I make an object attach to the mouse cursor without using 'Draggable'?

Hello, it has been long since I have used Gdevelop. So I am gonna start again with something very basic. :sweat_smile:

How do I make an object attach to the mouse cursor without using ‘Draggable’?

I have tried a few things but it does not work the way I expect, because I am probably missing something crucial here.

Required:

~When we click on an object, it should follow the mouse cursor (stick to it).

~When we click again, it should detach the object and place it wherever the mouse position was at the moment of detach.

I must mention that, I have tried ‘Draggable’ behavior, but it does not fulfill my exact requirement. In draggable behavior one must hold the mouse button to drag the object. On the other hand, I need the object to stick to the mouse cursor by just clicking once. Then detach it by clicking once again.

~What have I tried so far?

This approach results exactly as if we were using ‘Draggable’ behavior and therefore not useful for my case.

In other words - How do I make the object follow mouse cursor and updates every frame when the mouse is moved around until detached?

Quite simply, when you click on the object, toggle an object variable DragMeAround, and use that variable as condition to position the object on the cursor’s position.

Then use that variable again as condition to disable itself if the object is clicked again.

In the first event, DragMeAround variable is initially set to true, when the game runs, the Circle object sticks to mouse while moving - no issues there.

When I click ‘once’ the Circle object gets detached from the mouse as expected - no issues here.

I added a third event coz I need to pick up the Circle Object again. So I go like this

When run, the Circle object keeps attached, but then it stays attached after clicking the mouse. I have a feeling that I may be missing something very basic here, but cannot figure it out. Have been trying various other combinations. One such try goes like this:

When run, as DragMeAround is initially false, I click the Circle once. It snaps to the mouse position but does not stick. Although strangely when the mouse button is held and dragged on the object, the Circle gets dragged along. So back to initial problem.

As far as my understanding of Gdevelop event system goes, I think that second event mouse click and third event mouse click is happening at the same time and the true/false condition is met simultaneously.

Any ideas?

First, you need to use the trigger once condition wherever events don’t need to be repeated.
Second, you’re right, it looks like a typical case of false-true-false.
You need to make sure this can’t happen. More information here:
https://wiki.gdevelop.io/gdevelop5/tutorials/how-to-make-togglable-states-with-variables#how_to_toggle_states_using_variables

1 Like

Ok…

Gruk’s reply made me realized how little I know applying logic in general, as well as Gdevelop. So first I had to get my fundamentals right. It works in different ways when it comes to certain patterns. And I understood that it was crucial to know those differences.

Big thanks to Gruk :clap:t3:as I wasn’t aware of things like ‘toggle the boolean’. Heck I didn’t even know the scope of ‘Trigger Once’. Well, now I am beginning to understand these basics and applying them to my practice-logics.

Although it still took me a while to figure this one out but here is the solution just in case anyone else is interested. Took me like forever to come up with the last event-line :sweat_smile: but then it was Eureka moment.

1 Like