How to change variable in nodes in group ?

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

I am adding all mobs to the “enemy” group and I want to change their speed. How can I do it ???

:bust_in_silhouette: Reply From: Oen44

Get node that holds all enemies, iterate through children, get variable that holds their speed and change it.

# Get node that holds spawned enemies
# SomeNode is parent of node SpawnedEnemies, it's a path
var spawnedEnemies = get_node("SomeNode/SpawnedEnemies");
for enemy in spawnedEnemies.get_children(): # iterate through children
    enemy.speed_variable = 5; # set speed to 5

I think this person’s asking for a way to change a variable in nodes in a group. Which would imply that they are most likely not going to be all nicely packed in a parent node. Nice try though.

SIsilicon | 2018-09-17 21:05

:bust_in_silhouette: Reply From: SIsilicon

You can change variables in a group of nodes just like how you would with any other nodes.

The first step is to access the enemy nodes in the group they’re in.

var enemies = get_tree().get_nodes_in_group("your group name")

Then, in a for-loop, set each of the enemies speed like how you would with one of them.

var new_speed = 20.0
for(enemy in enemies):
    enemy.speed = new_speed

Edit: BTW, this code would be found in maybe like the root node, but that’s up to you as the programmer.

Oh, I’m working with Godot since yesterday so my bad, didn’t know that something like “nodes in group” is a thing. Good to know.
P.S. Could you look at my comment on your answer about A*? Thanks :slight_smile:

Oen44 | 2018-09-17 21:13

:bust_in_silhouette: Reply From: eons

Use get_tree().set_group.

See SceneTree — Godot Engine (3.0) documentation in English