Draw a line from point to point

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

How to draw line from point to point like this

:bust_in_silhouette: Reply From: Dooowy.

1.) Create a Node with global variables

2.) Each Sprite node should have a way to detect if the OS’s cursor is on top. You can do this by finding the Sprite’s position with Vector2.

Or

Vise versa, Have a single Node find all the nodes Positions

Example

using Godot;
using System;
using System.Collections.Generic;

public class cs_Overseer : Node
{
    private string SelectedNode = null;
    public string selectedNode {get; set;}

    public float MouseX {get; set;}

    public float MouseY {get; set;}

    private Sprite NodeOnePos {get; set;}
    private Sprite NodeTwoPos {get; set;}
    private Sprite NodeThreePos {get; set;}
    private Sprite NodeFourPos {get; set;}
    private Sprite NodeFivePos {get; set;}

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _handleNodesPos();
    }

    public override void _UnhandledInput(InputEvent @event)
    {
        if(@event is InputEvent)
        {
            _HandleLeftclick(@event);
        }
    }

    private void _HandleLeftclick(object @event)
    {
        if( @event is InputEventMouseButton MouseValue)
        {
            float x = MouseValue.Position.x;
            float y = MouseValue.Position.x;
            _SetPosition(x,y);
        }
    }
    private void _SetPosition(float x, float y)
    {
        MouseX = x;
        MouseY = y;
    }
  // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(float delta)
    {
        _handleNodesPos();
        _IsInLocation(MouseX,MouseY);
    }
    private void _handleNodesPos()
    {
        Sprite NodeOnePos = (Sprite)GetNode("pathToNode1");
        Sprite NodeTwoPos = (Sprite)GetNode("pathToNode2");
        Sprite NodeThreePos = (Sprite)GetNode("pathToNode3");
        Sprite NodeFourPos = (Sprite)GetNode("pathToNode4");
        Sprite NodeFivePos = (Sprite)GetNode("pathToNode5");
        List<Sprite> Nodes = new List<Sprite>();
        Nodes.Add(NodeOnePos);
        Nodes.Add(NodeTwoPos);
        Nodes.Add(NodeThreePos);
        Nodes.Add(NodeFourPos);
        Nodes.Add(NodeFivePos);
        foreach (var Entry in Nodes)
        {
            _PosSetter(Entry.Position,Entry);
        }
    }
    private void _PosSetter(Vector2 Position, Sprite Entry)
    {
        if(Entry == NodeOnePos)
        {
            NodeOnePos.Position = Entry.Position;
        }
        else if(Entry == NodeTwoPos)
        {
            NodeTwoPos.Position = Entry.Position;
        }
        else if(Entry == NodeThreePos)
        {
            NodeThreePos.Position = Entry.Position;
        }
        else if(Entry == NodeFourPos)
        {
            NodeFourPos.Position = Entry.Position;
        }
        else if(Entry == NodeFivePos)
        {
            NodeFivePos.Position = Entry.Position;
        }
    }

    private bool _IsInLocation(float x, float y)
    {
        float offset = 20;
        Sprite NodeOnePos = (Sprite)GetNode("pathToNode1");
        Sprite NodeTwoPos = (Sprite)GetNode("pathToNode2");
        Sprite NodeThreePos = (Sprite)GetNode("pathToNode3");
        Sprite NodeFourPos = (Sprite)GetNode("pathToNode4");
        Sprite NodeFivePos = (Sprite)GetNode("pathToNode5");
        List<Sprite> Nodes = new List<Sprite>();
        Nodes.Add(NodeOnePos);
        Nodes.Add(NodeTwoPos);
        Nodes.Add(NodeThreePos);
        Nodes.Add(NodeFourPos);
        Nodes.Add(NodeFivePos);
        foreach (var Entry in Nodes)
        {
            if(x > Entry.Position.x && x < Entry.Position.x - offset && y > Entry.Position.y && y < Entry.Position.y - offset)
            {
                return true;
            }
        }
        return false;
    }
}

The example above uses offset for a square 16x16 32x32 etc.
Edit it to your liking

Thanks for the answer.
But how to draw the lines when selecting the objects like in the image

I need to select random objects .

THE HELIX | 2019-06-19 15:06