0
\$\begingroup\$

The code from where I based my script.

The code I have has the these 2 problems:

  • The visual area for the player to see what unit are going to be selected doesn't work as intended, if I start dragging from left to right, and down, the selection area is drawn inverted. If I try to do the same but inverting the movement the selection area is drawn correctly.

  • The selection area and the units I want to select are not selected, or just selects just one, might have something to do with the problem above.

  • Something I have tried is the code alone in another scene, this baffles me because it works as intended, it doesn't have any one of the problem above!

The code:

extends Node2D

var dragging = false  # are we currently dragging?
var selected_units = []  # array of selected units
var drag_start = Vector2.ZERO  # location where the drag begian
var select_rect = RectangleShape2D.new()
const blue =  Color(0.59, 0.86, 1.0,0.4)
var drag_end  = Vector2.ZERO 
var selected = 0


func _unhandled_input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
        # When the mouse button is pressed, then the dragging starts
        if event.pressed:
            if selected_units.size() == 0:
                dragging = true
                drag_start = event.position
            else:
                for unit in selected_units:
                    unit.collider._set_selected(false)
                    selected_units = []
            dragging = true
            drag_start = event.position
            
        elif dragging:
            dragging = false
            drag_end = event.position
            update()
            drag_end = event.position
            select_rect.extents = (drag_end - drag_start) / 2
            var space = get_world_2d().direct_space_state
            var query = Physics2DShapeQueryParameters.new()
            query.collide_with_bodies = true
            query.set_shape(select_rect)
            query.transform = Transform2D(0, (drag_end + drag_start)/2)
            selected_units = space.intersect_shape(query)
            selected = selected_units.size() 
            for unit in selected_units:
                unit.collider._set_selected(true)
                
    if event is InputEventMouseMotion and dragging:
        update()

func _draw():
    if dragging:
        draw_rect(Rect2(drag_start, get_global_mouse_position() - drag_start),
                blue, true)

Not sure why is that happening, now I think this is the line where the problem lies:

query.transform = Transform2D(0, (drag_end + drag_start)/2)

Since it's not inverting the area where we are trying to get the units selected. not sure if did I missed something to configure elsewhere? why my code fails when it's in the main scene of my game but not on the test scene?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I found what was the issue with the code. It turns out that event.position gives back local space coordinates instead of the global coordinates. Using get_global_mouse_position() makes the code work as intended.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.