How to apply function on specific elements of a group

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mazen

So i’ve previously asked a question about how to calculate the distance between a player and an object, the object was a softbody node, community help was amazing, but the answers didn’t work, i just want to point out do NOT use soft body for this kind of tasks, it will always be at position (0,0,0) no matter where u place it, thats why the answers i was given didn’t work, idk if it’s a bug in godot or something else, anyway now i’m using a rigidbody3D nd everything is working fine, however i want to create different rigidbodies all under a group called “object”, nd if the distance between an element of the group nd player is less than 4 the function collect gets applied on it (i’m collecting the elements in a Listitem node), i know about get_tree.get_group(“group name”, “function”) but i just wanna apply that only on the elements that r far than the player by less than 4, is the apprach i’m trying to achieve correct? if not whats the right approach nd how to achieve it

:bust_in_silhouette: Reply From: Wakatta

You may have to create a group manager for this

but in simpler terms, add/remove the nodes of interest to another group or loop the group for the affected

example

func _process(delta):

    for obj in get_tree().get_group("object"):
        if player.global_transform.origin.distance_to(obj.global_transform.origin) < 4:
            obj.function()
    

@wakatta ty, i’ve tried to achieve similar approach, but now there’s another bug i’m confused about, i’ve ended up asking another question lol

Mazen | 2023-01-27 14:26