Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singletons such as RenderingServer should be accessible in MODULE_INITIALIZATION_LEVEL_SCENE #1180

Open
Zylann opened this issue Jul 16, 2023 · 1 comment
Labels
bug This has been identified as a bug topic:gdextension This relates to the new Godot 4 extension implementation

Comments

@Zylann
Copy link
Collaborator

Zylann commented Jul 16, 2023

Godot version

4.1

godot-cpp version

4.1

System information

Windows 10 64 bits NVIDIA GeForce GTX 1060

Issue description

RenderingServer::get_singleton returns null in cases where it should not.

  • If you have code in your extension library initializer that tries to access RenderingServer, it will crash because that singleton will be null, even in initialization levels that should definitely work in modules (like MODULE_INITIALIZATION_LEVEL_SCENE).

  • Similarly, if you have code in a class constructor (node, resource...) that tries to access RenderingServer, it will also crash the editor on startup, because EditorHelp creates temporary instances of every class in ClassDB to access the default values of their properties (mentionned in There should be a way to register classes without exposing them (usually editor plugin internals) #1179).

  • In fact, if you dare calling RenderingServer::get_singleton() and it returns null, it will keep returning null forever, because in GodotCpp the wrapper caches the value once in a static variable and never runs again:

    "\tstatic GDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton(_gde_class_name._native_ptr());"

This is also the case with other servers such as PhysicsServer.

In all the mentionned cases, the server singleton actually exists.
Apparently the cause during initialization is that singletons in GDExtension are accessed similarly to scripts, which is terrible because that API is initialized way too late and completely disregards MODULE_INITIALIZATION_ levels.
This is very confusing and impractical. GDExtension needs the same access API modules have.

I notably work on a big module project, which can also compile as a GDExtension, so finding workarounds gets more complicated.

Steps to reproduce

Access RenderingServer::get_singleton() inside your library initializer, in MODULE_INITIALIZATION_LEVEL_SCENE, or access it inside the constructor of a custom class.

Minimal reproduction project

N.A

Alternative (theory)

This came up in a meeting: in modules, Godot is currently mixing up registration (declaring that X is there, but not executing anything) and initialization in modules. And by extension... extensions too 😁 In an ideal world, these two steps should be very separate.

In my module, I don't actually need to initialize singletons in MODULE_INITIALIZATION_LEVEL_SCENE, I just do it there because in the present state of things, I can do it there. But in GDExtension there is nowhere I can do it which won't cause the present issue.

Though regardless of this idea, there would still be a need to be able to order each step, because registration can depend on other things (base classes?), and initialization can also depend on others (accessing a core singleton when initializing a custom singleton).

@Zylann Zylann changed the title Initialization of singletons such as RenderingServer needs to be fixed Access to singletons such as RenderingServer needs to be fixed Jul 16, 2023
@Zylann Zylann changed the title Access to singletons such as RenderingServer needs to be fixed Singletons such as RenderingServer should be accessible in MODULE_INITIALIZATION_LEVEL_SCENE Jul 17, 2023
@Calinou Calinou added bug This has been identified as a bug topic:gdextension This relates to the new Godot 4 extension implementation labels Jul 17, 2023
Zylann added a commit to Zylann/godot_voxel that referenced this issue Jul 17, 2023
- Don't access EditorNode indirectly from EditorPlugin constructors,
see godotengine/godot-cpp#1179
- Don't access RenderingServer directly from any registered constructor,
see godotengine/godot-cpp#1180
- Register a few missing classes

A plethora of hidden issues remain. Terrains crash as soon as they
start generating stuff.
@Splizard
Copy link

Also tracked here: godotengine/godot#64975

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This has been identified as a bug topic:gdextension This relates to the new Godot 4 extension implementation
Projects
None yet
Development

No branches or pull requests

3 participants