|Topic was automatically imported from the old Question2Answer platform.
I am a newcomer on Godot Engine (less than a week using Godot) and
and I’m learning how to better organize the project.
My game has a common behavior among objects that is follow another object and I don’t want repeat the same code for each different object. So I created a new scene with a generic Node called FollowBehavior and attached a script. The script look like this:
func init(_object, _target):
object = _object
target = _target
# MY LOGIC HERE
I added FollowBehavior as a child node in some objects and in their respective scripts I did this:
# Object with follow behavior
Is there a better alternative for this? Is it the “best” way?
I’ve been struggling with the exact same problem. I’ve done it using this node method, but also this way:
a_behaviour = my_behaviour.new()
I found that this way avoids some null object errors that can happen because of the order in which the nodes get created and the engine trying to initialize variables on objects that aren’t created yet.
The downside is that you can’t use the editor modify exported properties of the behaviour.
NaughtyGnosiophile | 2018-07-28 10:22