Requiring Other Scripts

Godot Version

4.1.3.stable.official

Question

Hi! I’m used to object-oriented languages and am just now getting to Godot.

I have one script, for example SmoothMotion.gd:

var targetPos = position;
@export_range(0.0, 1.0, 0.05) var moveLerp : float = 0.1;

func _process(delta):
	position = lerp(position, targetPos, moveLerp);

and would like to use it in conjunction with a few other scripts attached to the same node that might modify targetPos.

Is there a way to require SmoothMotion.gd also be attached to the node if I use one or more of the others?
I know I could extend the script, but then what if I want to require multiple? And what happens if two scripts extend it and want to modify the same variable?
I’ve seen a few uses of the preload function, and it seems like the solution could be in there, but I’m having trouble understanding if that creates a new instance of the class or allows me to modify the node’s instance of it (and therefore the variables held there).

Let me know if my question’s unclear or anything, thanks!

Isn’t YourClassName.new() is just enough?

2 Likes

You can currently extend only one class, meaning GDScript can be based on inheritance only. There is a plan to implement Traits (similar to C# Interfaces), but we don’t know when it’s gonna come.
Check out my GDScript snippet that allows to work with component type safe classes and reference them in your scripts.

If you have any issues implementing it in your project - let me know

1 Like

Lol extremely simple thank you