Skip to content

Getting Started

Ashton Meuser edited this page Sep 17, 2024 · 13 revisions

Installation

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).

GDExtension/GDNative Addon

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.

  1. Download a ZIP of Godot Wasm from the repository.
  2. In Godot's Asset Library tab, click Import and select the downloaded ZIP file. Follow prompts to complete installation of the addon.

Godot Module

Installation as a Godot module requires recompilation of the Godot engine.

  1. Clone or download the Godot engine following this guide.
  2. Download the Godot Wasm source via the releases page or Code → Download ZIP on GitHub.
  3. Include the entire Godot Wasm directory within the godot/modules directory.
  4. 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).

Usage

Once installed in a Godot project, the Godot Wasm class can be accessed via Wasm. Here's a simple usage example.

  1. Create a Wasm module or use the example module.
  2. Add the Wasm module to your Godot project.
  3. In GDScript, instantiate the Wasm class via var wasm = Wasm.new().
  4. Load your Wasm module bytecode as follows replacing YOUR_WASM_MODULE_PATH with the path to your Wasm module e.g. example.wasm. The Wasm.load() method accepts a PoolByteArray and a dictionary defining Wasm module imports. All imports should be satisfied and may differ with each Wasm module.
    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()
    For Godot 3.x, file access works a little differently.
    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()
  5. Access global constants and mutables exported by the Wasm module via wasm.global("YOUR_GLOBAL_NAME") replacing YOUR_GLOBAL_NAME with the name of an exported Wasm module global.
  6. 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.
  7. Call a function exported by your Wasm module via wasm.function("YOUR_FUNCTION_NAME", args) replacing YOUR_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.

Clone this wiki locally