0 votes

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

in Engine by (53 points)

3 Answers

+3 votes
Best answer
by (7,868 points)
selected by
+1 vote

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

# 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
by (381 points)

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.

+1 vote

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.

by (3,875 points)

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 :)

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 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 webmaster@godotengine.org with your username.