Connecting 2 objects with a line and being able to drag the objects and have the line follow the point

How do I…

I’m trying to make a wire connecting system between 2 objects but the objects are draggable. I want to be able to make a line connecting 2 objects and the line still being connected to them when one of them is moved like this


And I don’t want the line to be draggable I just want it to follow the points of the objects

Hi,
for that you just need the shape painter-object. In the menu for the shape painter-object you have to make sure that the box ‘Clear the rendered image between each frame’ is checked, otherwise your line will not follow your objects.

But is there a way to make it so when you click on the point of Object A the line follows your mouse until it collides with Object B’s point?

Yes, you can draw the line to your mouse pointer like so:

And for making it collide with the object B point?

Here is a quick example. You need boolean object variables which will be toggled in case you click/release the mouse button on the object that you want to connect.

This is hardcoded and you have to find a system that allows you to connect dynamically two or more objects. This will lead you into the wide and “exciting” world of object picking Object Picking [GDevelop wiki].
The implementation depends on the way you want the user to select and connect the objects.

(Edit: I just added the boolean for the mouse draw that was missing in the previous example)

1 Like