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

Module support #17

Merged
merged 8 commits into from
Apr 22, 2023
Merged

Module support #17

merged 8 commits into from
Apr 22, 2023

Conversation

ashtonmeuser
Copy link
Owner

@ashtonmeuser ashtonmeuser commented Apr 20, 2023

Adapted the project to compile as either an addon or module. Closes #7. Does not actually allow for web/HTML5 exports due to Wasmer not being able to be compiled to Wasm via Emscripten (which is what happens internally when compiling Godot export templates). More details in #18).

Surprisingly few compromises and defines were required. An overview of the quirks introduced:

  1. GODOT_MODULE define determines how Godot sources are included. To make things easier, all Godot source includes should go in defs.h. While a little awkward, I think this is an okay pattern for the (currently) very limited imports used. If this proves problematic, could use the same pattern in each individual source file. Example:
    #ifdef GODOT_MODULE // Godot includes when building module
      #include "scene/2d/sprite.h"
    #else // Godot addon includes
      #include <Godot.hpp>
      #include <Sprite.hpp>
    #endif
    
  2. Method are registered via _bind_methods in Godot and _register_methods. REGISTRATION_METHOD is used to abstract this difference. Inside the method, there's a check on the GODOT_MODULE define to actually register the methods differently. Example:
    void MyClass::REGISTRATION_METHOD() {
      #ifdef GODOT_MODULE
        ClassDB::bind_method(D_METHOD("my_method"), &MyClass::my_method);
      #else
        register_method("my_method", &MyClass::my_method);
      #endif
    }
    
  3. NS define used to define the godot:: namespace that is required for addon and blank (::) namespace for module.
  4. godot_error (used in GDNative) is defined as an alias for Error for modules.
  5. Errors used by the addon are defined explicitly using the same name without the godot_ prefix. This aligns error names with Godot proper (3.x and 4) and GDExtension. For example, Godot and GDExtension use OK while GDNative uses GODOT_OK. Now an OK define exists to align GDNative with the others.
  6. Superclass of StreamPeerWasm is now set by a define (either StreamPeer or StreamPeerGDNative). StreamPeerGDNative interface is all defines. This is hideous and will hopefully remain the ugliest part the addon/module agnosticism.

To build the engine with the Godot Wasm module, add the entire project in godot/modules/wasm and run scons platform=MY_PLATFORM.

CC @Trey2k

GODOT_MODULE define determines how Godot sources are included
All Godot sources should be included in defs.h
REGISTRATION_METHOD defines the method name used to register methods and properties
Method registration must be defined twice based on GODOT_MODULE
NS define used for godot namespace required by anonymous namespaces when compiling as addon
Define godot_error as alias of Error for module
Errors used for Godot 3.x addon are defined (enables max compatibility with module and Godot 4)
Godot library source file (addon specific) skipped if GODOT_MODULE present
StreamPeer superclass defined depending on context
StreamPeerGDNative interface defined for addon (likely to be ugliest part of module agnosticism)
@Trey2k
Copy link
Contributor

Trey2k commented Apr 20, 2023

I think godot-library.cpp is probably rendered unnecessary by register_types.cpp, they both do pretty much the same thing. When working on the gd4 PR this was the route I went with.

@ashtonmeuser
Copy link
Owner Author

I think godot-library.cpp is probably rendered unnecessary by register_types.cpp

This file is wrapped if an ifdef that precludes it when used as a module. However, just to be safe, I excluded the file from the source glob as well.

@ashtonmeuser ashtonmeuser marked this pull request as ready for review April 22, 2023 01:29
@ashtonmeuser ashtonmeuser merged commit 1642e7d into master Apr 22, 2023
@ashtonmeuser ashtonmeuser deleted the module branch May 1, 2023 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Godot module support
2 participants