![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Solfatare |
Hello! It’s my first question here, glad to start my journey with you!
I just finished the Dodge the Creeps tutorial, and while i mostly understood, there is this part of the code that bugs me:
func _on_MobTimer_timeout():
# Choose a random location on Path2D.
$MobPath/MobSpawnLocation.set_offset(randi())
# Create a Mob instance and add it to the scene.
var mob = Mob.instance()
add_child(mob)
# Set the mob's direction perpendicular to the path direction.
var direction = $MobPath/MobSpawnLocation.rotation + PI / 2
# Set the mob's position to a random location.
mob.position = $MobPath/MobSpawnLocation.position
# Add some randomness to the direction.
direction += rand_range(-PI / 4, PI / 4)
mob.rotation = direction
# Set the velocity (speed & direction).
mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
mob.linear_velocity = mob.linear_velocity.rotated(direction)
The part is mob.linear_velocity.
In the tutorial, they make itso the root node from the Mob Scene is the RigidBody, which contains a linear velocity variable.
In my version, the RigidBody is the child of a Node2D node, which does not contain a linear velocity variable.
The hierarchy looks like that:
MobNode
RigidBody
I want to edit a var in the RigidBody, without a direct access to it.
I tried something like replacing
mob.linear_velocity = mob.linear_velocity.rotated(direction)
with
$RigidBody.linear_velocity = mob.linear_velocity.rotated(direction)
or
get_node("/root/mob/rigidbody").linear...
What would the syntax be?
The Mob scene is not directly instanced in the main scene. If i understand it correctly, it’s the script that creates an instance by referencing another scene.
If someone could clarify this to me, and explain me the way of the get_nodes, I’d be so glad. Thanks!