Skip to content

Examples

Ashton Meuser edited this page Mar 7, 2024 · 9 revisions

Overview

Examples are provided for both creating and consuming/using a Wasm module.

To run an example project, you'll need to:

  1. Clone or download the repo.
  2. Open the example project in Godot.
  3. Install or build the Godot Wasm addon (see Installation).

Running Examples on Windows

Because Unix symbolic links aren't reliable in Windows, you may have to copy the root-level addons directory into the example directory (replacing the existing symbolic link file). Alternatively, create an equivalent Windows shortcut.

Wasm Consume (Godot)

Screenshot 2023-09-15 at 12 54 00 PM

A simple example of loading a Wasm module, displaying the structure of its imports and exports, calling its exported functions, and providing GDScript callbacks via import functions. Some computationally-expensive benchmarks e.g. prime sieve in GDScript and Wasm can be compared. The Wasm module used is that generated by the Wasm Create example. All logic for this Godot project exists in Main.gd.

Wasm Create (AssemblyScript)

An example AssemblyScript project which creates a simple Wasm module with the following exported entities.

Entity Type Description
global_const Global i64 Global constant
global_var Global f64 Global mutable
memory_value Global i64 Used to store first eight bytes of memory to demonstrate memory manipulation
update_memory Function () → void Updates the value of memory_value
add Function (i64, i64) → i64 Adds two integers
fibonacci Function (i64) → i64 Return Fibonacci number at the position provided
sieve Function (i64) → i32 Return largest prime number up to the limit provided

From the example directory (examples/wasm-create), install or update Node modules npm i. Run npm run build to build the Wasm module. This will create and populate a build directory containing the Wasm module, example.wasm, amongst other build artifacts. If you have the Wasmer CLI installed, run wasmer inspect build/example.wasm to view the Wasm module imports and exports.

Note that WebAssembly is a large topic and thoroughly documenting the creation of Wasm modules is beyond the scope of this project. AssemblyScript is just one of many ways to create a Wasm module.

Wasm Visualizations

Screen.Recording.2023-09-15.at.1.04.47.PM_compressed.1.mp4

An example of displaying graphics generated by Wasm modules. Graphics are read directly from the Wasm module memory using the StreamPeer interface.

The source for the modules used in this example can be found here.

Wasm Test

Integration tests for Godot Wasm. This is a good source for exploring the Godot Wasm API and possible usage patterns.

The source code for WebAssembly modules used in integration tests is available here.

The project can be run either in the Godot editor or via command line with GODOT_EXECUTABLE --headless --debug --path . (replacing GODOT_EXECUTABLE with Godot executable for your platform.

Doom Port

Porting Doom to Godot in 34 Lines of GDScript

An exploration of running and rendering an existing WebAssembly Doom port within Godot using the Godot Wasm addon. See Porting Doom to Godot in 34 Lines of GDScript.