-
Notifications
You must be signed in to change notification settings - Fork 12
Getting Started
Godot Wasm supports installation via GDExtension/GDNative addon or module. Installing as an addon is far faster and simpler and requires merely including the asset in your Godot project while installing as a module requires recompilation of the Godot engine. Installing as a module involves recompilation of Godot but allows distributing your exported project as a single binary (no dynamic libraries).
Installation in a Godot project can be done entirely through Godot's built-in Asset Library. Recompilation of the engine is not required. Using the Asset Library tab within the Godot editor, search for "Godot Wasm", and follow the prompts to install. You can also explore the Godot Wasm asset page (or Godot 3.x asset) in a browser. Note that the Asset Library has an approval process that can take several days and may therefore be a version or two behind.
Alternatively, you can download the repository as a ZIP file and import manualy via the same Asset Library tab within the Godot editor.
- Download a ZIP of Godot Wasm from the repository.
- In Godot's Asset Library tab, click Import and select the downloaded ZIP file. Follow prompts to complete installation of the addon.
Installation as a Godot module requires recompilation of the Godot engine.
- Clone or download the Godot engine following this guide.
- Download the Godot Wasm source via the releases page or Code → Download ZIP on GitHub.
- Include the entire Godot Wasm directory within the godot/modules directory.
- Rename the Godot Wasm directory to wasm. All project files e.g. SCsub should now be in godot/modules/wasm.
Recompile the Godot engine following this guide. More information on custom Godot modules can be found in this guide.
Compiling the web/HTML5 export template is not yet supported (see #18).
Once installed in a Godot project, the Godot Wasm class can be accessed via Wasm
. Here's a simple usage example.
- Create a Wasm module or use the example module.
- Add the Wasm module to your Godot project.
- In GDScript, instantiate the
Wasm
class viavar wasm = Wasm.new()
. - Load your Wasm module bytecode as follows replacing
YOUR_WASM_MODULE_PATH
with the path to your Wasm module e.g. example.wasm. TheWasm.load()
method accepts a PoolByteArray and a dictionary defining Wasm module imports. All imports should be satisfied and may differ with each Wasm module.For Godot 3.x, file access works a little differently.var file = FileAccess.open("res://YOUR_WASM_MODULE_PATH", FileAccess.READ) var buffer = file.get_buffer(file.get_length()) var imports = { "functions": { "index.callback": [self, "callback"] } } # Set imports according to Wasm module wasm.load(buffer, imports) file.close()
var file = File.new() file.open("res://YOUR_WASM_MODULE_PATH", File.READ) var buffer = file.get_buffer(file.get_len()) var imports = { "functions": { "index.callback": [self, "callback"] } } # Set imports according to Wasm module wasm.load(buffer, imports) file.close()
- Access global constants and mutables exported by the Wasm module via
wasm.global("YOUR_GLOBAL_NAME")
replacingYOUR_GLOBAL_NAME
with the name of an exported Wasm module global. - Define an array containing the arguments to be supplied to your exported Wasm module function via
var args = [1, 2]
. Ensure the number of arguments and argument types match those expected by the exported Wasm module function. - Call a function exported by your Wasm module via
wasm.function("YOUR_FUNCTION_NAME", args)
replacingYOUR_FUNCTION_NAME
with the name of the exported Wasm module function.
For more practical examples, see Examples. Detailed documentation on module imports and exports as well as module memory are also provided.