Getting a line to draw between an initial point and mouse cursor

Godot Version

4

Question

I created a drag and drop function that displays a label with inches. However I want it to also draw a line from original position. However it states initial_pos is nil and not a vector.
My code is below, I thought initial_pos would be a vector since it displays as one when I use it in print.


var is_dragging = false
var mouse_offset 
var delay = 5
var initial_pos
var inches
@export var Ruler: RichTextLabel

func _physics_process(delta):
	if is_dragging:
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", get_global_mouse_position() - mouse_offset, delay * delta)
		inches = initial_pos.distance_to(get_global_mouse_position())/100
		Ruler.text = "%din" % inches
		tween.tween_property(Ruler, "position", get_global_mouse_position()+mouse_offset, 0)
		print(initial_pos)
func _draw():
	draw_line(initial_pos, get_global_mouse_position(), Color.RED, 4.0)
	

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			if get_rect().has_point(to_local(event.position)):
				is_dragging = true
				mouse_offset = event.position - position
				initial_pos = self.position
				Ruler.show()
		elif event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
			is_dragging = false
			Ruler.hide()

You have not declared initial_pos to have a type, so it will be assigned the type of the value used for the first assignment. By the time _draw is called, no assignment has taken place. That means initial_pos has neither a value nor a type at that time.

Thank you, but do you now how I would put the initial line always at the initial position of the sprite? I got it to redraw the line and only draw it when Im dragging the object, but the intial line moves with the object. Furthermore, its not on the object, instead being placed in the bottom right corner of the screen.

Try checking for is_dragging in _draw().

It checks, however the line that is drawn isn’t located on the initial point and doesn’t draw to my cursor. Furthermore, the initial point of the drawn line doesn’t remain on the initial point.

Your initial position is using local coordinates, but it needs to be global. The code you posted here looks like it should end the line at your mouse.

I tried converting all my positions to global positions, but it doesn’t redraw starting from initial point still. Now its just a static line that moves with my cursor. I attached my code below, what am I missing?


var is_dragging = false
var mouse_offset 
var delay = 5
var initial_pos : Vector2
var _point2 : Vector2
var inches : float
var width : int=10
var color : Color = Color.RED
@export var Ruler: RichTextLabel


func _physics_process(delta):
	if is_dragging:
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", get_global_mouse_position() - mouse_offset, delay * delta)
		inches = initial_pos.distance_to(get_global_mouse_position())/100
		Ruler.text = "%din" % inches
		tween.tween_property(Ruler, "position", self.global_position+mouse_offset, 0)
		print(initial_pos)
		
func _draw() -> void:
	if is_dragging == true:
		draw_line(Vector2.ZERO, initial_pos, color, width)
func _process(_delta):
	var mouse_position = get_viewport().get_mouse_position()
	if mouse_position != _point2:
		_point2 = mouse_position
		queue_redraw()

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			if get_rect().has_point(to_local(event.global_position)):
				is_dragging = true
				mouse_offset = event.global_position - global_position
				initial_pos = self.global_position
				Ruler.show()
		elif event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
			is_dragging = false
			Ruler.hide()

Try making initial_pos=get_global_mouse_position(), and drawing your line with draw_line(initial_pos,get_global_mouse_position(), color, width)

So I decided to redo my code and have a new problem. The line now draws correctly, however it remains on the screen even when I stop dragging. Furthermore, when I stop dragging, the sprite will remain in place until I click another place. Then it will immediately move to that location.
Heres the new code


var is_dragging = false
var initial_pos : Vector2
var _point2 : Vector2
var inches : float
var width : int=10
var color : Color = Color.RED
@export var Ruler: RichTextLabel



func _draw():
	if is_dragging == true:
		draw_line(to_local(initial_pos), to_local(_point2), color, width, true)
	
func _physics_process(delta):
	if Input.is_action_pressed("click"):
		if get_rect().has_point(to_local(self.position)):
			Ruler.text = "%din" % inches
			Ruler.position = self.position * 1.1
			Ruler.show()
			_point2 = self.position
			self.position = get_global_mouse_position()
			inches = initial_pos.distance_to(get_global_mouse_position())/100
			print(initial_pos)
			queue_redraw()
		else:
			is_dragging = false
			Ruler.hide()
			

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			if get_rect().has_point(to_local(event.position)):
				is_dragging = true
				initial_pos = self.position