0 votes

I have a bunch of images placed on the screen. When the user performs a mouse-click, is there an easy way to tell what image resource is 'near' the 'event.position Vector2'? If so, is there a way to modify how far away 'near' is?

Godot version v3.5.1.stable.official [6fed1ffa3]
in Engine by (88 points)

1 Answer

+1 vote
Best answer

One way to do it is to loop through all the images and calculate the distance between them and the mouse then have all that are near aka within x distance added to a list.

func get_nearby(near: float = 64.0) -> Array:
    var images: Array = $Images.get_children()
    var gmpos: Vector2 = get_global_mouse_position()
    var nearby: Array = Array()
    for i in images:
       if gmpos.distance_to(i.global_position) <= near:
    return nearby

If you want the distance to be exclusive then change <= to <

by (659 points)
selected by

How would you do that with duplicated image resources?

# setting node name and placing resource on screen
temp_node = get_node(location_info.path + location_info.filename)
var my_copy = temp_node.duplicate()
my_copy.visible = true
my_copy.set_position(Vector2(xpos, ypos))
my_copy.rect_scale = Vector2(asset_result[0]['scale'],asset_result[0]['scale'])

# tracking arrays

The way I was trying to do it was as I duplicated and placed the resource I was saving that value in an array, the cycling through those values on a mouse click event, see below.

func _input(event):
    # Mouse in viewport coordinates.
    if event is InputEventMouseButton:
        # testing for any elements in resource tracker
        if (resource_position_array.size() > 0):
            # looping through position array
            for loop in range(0,resource_position_array.size(),3):
                # setting position values
                var xstart = (resource_position_array[loop][0]-20)
                var xfinish = (resource_position_array[loop][0]+20)
                var ystart = (resource_position_array[loop][1]-20)
                var yfinish = (resource_position_array[loop][1]+20)

                # looking for any resource at that x,y range
                if (event.position.x > xstart and event.position.x < xfinish):
                    if (event.position.y > ystart and event.position.y < yfinish):
                        alert("Resource Found","Resource Found")

However, this was not working. When I clicked on the middle of the graphic, the event.position was different from the xy in the array. I like your idea better, I'm just not sure how you would cycle through duplicated resources like that.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.