0 votes

Hello! I am currently trying to create a system in which the player can choose between multiple bodies within an area 2D. I am currently using the get_overlapping_bodies() function to do this, and was wondering how it determines the order of the list of bodies.

For insight, this is the code I am using right now -I am currently assigning an ID to each of the bodies, so I can select based on the value of a counter which changes with keyboard input-

func assignIDs():
        var ID = 0
        for bodies in Area.get_overlapping_bodies():
            bodies.ID = ID
            ID += 1
        maxID = ID

func select():

    owner.movement_velocity = Vector2.ZERO

    canHit = whipRange.get_overlapping_bodies()

    if Input.is_action_just_pressed("Right"):
        targetBody += 1
    elif Input.is_action_just_pressed("Left"):
        targetBody -= 1

    targetBody = clamp(targetBody, 0 , maxID)

    for bodies in canHit:
        if targetBody == bodies.ID:
            bodies.modulate = Color(1,0,0,1) #Visual aid to show which body is selected
            bodies.modulate = Color(1,1,1,1)

I am currently using these two snippets of code (first is in a separate function, unimportant right now) to select a body out of the list, but would like to know how the function determines the order so I can have it be more polished in the final product :)


Godot version 3.5.1
in Engine by (50 points)

1 Answer

0 votes

I believe the first body to get into area gets listed first in overlapping_bodies, so it all depends on a time of collision detection.

by (8,099 points)
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.