2D RTS Selection Rect - Issues with Code

Godot Version

4.3 STABLE

Question

I’m trying to make a troop selection mechanism for my 2d rts. I have the following code so far, it works most of the time but sometimes it get’s jittery and acts weird, where the rect just freezes as it was and like doesn’t resize. Is there something wrong with the code or is it just my laptop?

extends Node2D

var selection_rect: Panel

var dragging = false
var init_pos = Vector2()
var curr_pos = Vector2()

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.is_pressed():
				print("PRESSED AT: ", event.position)
				
				dragging = true
				init_pos = get_global_mouse_position()
				
				selection_rect = Panel.new()
				add_child(selection_rect)
				
			if event.is_released():
				print("RELEASED AT: ", event.position)
				
				dragging = false
				selection_rect.queue_free()
				
	if event is InputEventMouseMotion:
		if dragging:
			var curr_pos = get_global_mouse_position()
			
			var begin = Vector2(minf(init_pos.x, curr_pos.x), minf(init_pos.y, curr_pos.y))
			var end = Vector2(maxf(init_pos.x, curr_pos.x), maxf(init_pos.y, curr_pos.y))
			
			selection_rect.set_begin(begin)
			selection_rect.set_end(end)

Does this happen when you drag your mouse over other ui-nodes?

it happens when i change the position of the mouse. say i clicked at (200, 200) and i move the mouse to (300, 300) its fine, nothing happens but if i move to say (100,100) or (200, 100) it starts glitching.

And there are no control-nodes that could block the input-signals?

1 Like

Ohhh… i’m using a Panel as the selection_rect and i think that’s what’s blocking the mouse input. Yea that’s the problem.

Thanks you!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.