load an use cpp extension during runtime

Godot Version

4.6.2

Question

Hello all,
I need to load a extension + pck during runtime.
The extension is not available at point of time the app starts.

The extension in cpp which works perfectly fine if it is available at startup in the first place.

I am using

GDExtensionManager.load_extension("res://my_extension.gdextension")

to load the extension.
This works and the return status is LOAD_STATUS_OK
For further checks I see my extension in the returned list of:

GDExtensionManager.get_loaded_extensions()

To check if the classes of the extension are available I check

ClassDB.get_class_list()

to contain my classes of the extension.

Additionally I check if it is possible to instantiate a object of it

ClassDB.can_instantiate("MyExtensionClass")

All this seems to be correct and the classes should be useable.

After the extension got loaded I load a additional pck and use a gdscript out of it which uses the class of the extension.

load("res://from_pck_loaded_script.gd")

I have the following observation:
I can do

ClassDB.instantiate("MyExtensionClass")

But I cannot do

extends MyExtensionClass
#or 
MyExtensionClass.new()

and that’s the issue. It stops the app directly.

Also the following causes issue

var test:MyExtensionClass 
test = ClassDB.instantiate("MyExtensionClass")

This produces a error during runtime but the app continues to run

write_assign_with_conversion: Compiler bug: unresolved assign.

I would like to use the extension classes without any limitations during development.

Can someone explain why it doesn’t work as expected and how to overcome it?

Thank you very much in advance

If you are loading classes at runtime then there is no possible way for the compiler to know about them at compile time. You will have to fallback to an Object or Variant type.

1 Like

Hi Gertkeno and others,

thanks for your reply.

What exactly do you mean by “compile time” in that case?
Do you mean the GDScript interpretation/tokenization?

The script which is using the classes of the extension is loaded after the extension itself.
It cannot be interpreted before the extension got loaded.

Here is a overview of the order I am doing at the moment.

1.) Start Godot App
2.) load extension with extension manager
3.) load the pck which contains the script in step 4.)
4.) load the gdscript which uses the class of the extension

What is missing between step 2.) and 4.) that the script interpreter can understand the classes of the extension?

How and at what point of time does the script interpreter being setup with the known classes?

Thanks a lot.