how to add CUSTOM node via GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By metin
:warning: Old Version Published before Godot 3 was released.

When I create a node via GDScript it’s easy, I just call for example

var test = MeshInstance.new()

But now I created a Editor Addon for a custom node called “Projectile” following this manual and I can add it via the Editor but not via Script. When I enter

var test = Projectile.new()

I get an Error

Parser Error: Identifier not found: Projectile

Is that what I’m trying to do even possible? Or do I have to use C++ for that?

:bust_in_silhouette: Reply From: Zylann

I think it’s not possible, but the workaround is easy:

var test = preload("res://addons/youraddon/projectile.gd").new()

You can also define it as a const to make it more readable (nice thing to note is this pattern looks like a Python import):

const Projectile = preload("res://addons/youraddon/projectile.gd")

Then you can use it like this in the rest of your script:

var test = Projectile.new()

Thanks, it does exactly what I needed.

metin | 2017-09-02 15:48