0 votes

If the root node is KinematicBody2D, and the character has a child node stats storing all the variable root node use, like speed. After the KinematicBody2D node enter the scence tree and get stats 's speed, if the speed changed in child node stats , dose it have to send signal to inform the KinematicBody2D node to change its speed variable so the KinematicBody2D node can move in a different speed?
Thanks!

in Engine by (74 points)

2 Answers

0 votes
Best answer

Hello hsjaaa,

You could do it with signals yes.
To accomplish this, you should only ever modify the node's stats (among which, speed) with a dedicated method like "set_speed(value)". You could emit the signal from there.
So something like that :

func set_speed(newSpeed : float) -> void:
    speed = newSpeed
    emit_signal("speed_changed", newSpeed)

Something like that should work. Of course you should do a line like that somewhere earlier in the code :

nodeThatContainsTheStats.connect("speed_changed", theKinematic2Dnode, "methodThatGetsCalledByTheSignal")

Hope this helps

by (305 points)
selected by

Thanks, it's helpful!

+1 vote

No.
I think a signal would be unnecessary if you use it like this from the root node:

extends KinematicBody2D

func _physics_process(delta):
    move_and_slide($stats.speed)
by (3,959 points)

I tried this, it works, but is this good practice? Since it call child node every frame.

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.

Categories