Q Science
Q Science

Reputation: 111

Can a GDScript class not extend anything?/Is there a class that every class must extend?

I'm making a GDScript class that shouldn't need to extend Node2D. It doesn't have an associated scene, and exists so that other classes in my project can interface with it, but will never be displayed directly by the engine. Can I simply not have the extends keyword at the top of the file?

Upvotes: 1

Views: 2768

Answers (1)

hola
hola

Reputation: 3500

You can extend Object or any of its descendants, not just Node.

I believe you can omit the extends statement and it will default to inheriting Reference, but you should be explicit and write extends Reference.

Read the docs on Object then Reference then Resource and see which one will work best for you.

You can then instance your new script like:

const MyScript = preload(‘path/to/my_script.gd’)
var my_script := MyScript.new()

Or if you have given your script a class_name you can omit the preload into constant step.

Upvotes: 3

Related Questions