0 votes

i was trying to apply actions on specific elements of a group named "object", i have two rigidbodies in the group, i used for loop to iterate through group elements nd if distance between element and player is less than 4 actions will be applied, strangely not all actions gets applied, here is the code

extends Spatial
var axis
var kin
func _ready():
    var rig1=$rig1
    var rig2=$rig2
    kin=$KinematicBody
    axis=get_tree().get_nodes_in_group("object")

func _physics_process(delta):
    for elem in axis:
        if (kin.translation.distance_to(elem.translation)<4):
            $Control.visible=true
            if Input.is_action_just_pressed("ui_accept"):
                $ItemList.add_item("tool",load("res://b65186fd210e34e35d0763332262993c.jpg"))
        else:
            $Control.visible=false

the visible property only changes with the last rigid body in the group, when i'm near the first element the button doesn't appear, what confuses me is that the nested if works with both of them, when i'm near first element it collects it nd when i'm near second element it collects it as well, so the code is calculating the distance between each one of them nd the player but i don't get why the button icon doesn't become visible with both, it only becomes visible when distance between last element of group object nd the player is less than 4, does anyone know why that might be?

Godot version 3.5.1
in Engine by (59 points)
edited by

Aren't you shutting off the button in the else?
For example if axis[0] < 4 == true the button gets turned on (visible).
But then the loop continues and the next element axis[1] > 4 shuts the button off.
Conversely if the axis[0] > 4 the button gets shut off and axis[1] < 4 the button gets turned back on.
This would make it seems as though only the last elem is working properly.

Please log in or register to answer this question.

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.