Skip to content
Ashton Meuser edited this page Jun 4, 2023 · 15 revisions

What is Godot Wasm?

Godot Wasm embeds a WebAssembly (Wasm) runtime in your Godot project. It allows you to interact with Wasm modules from GDScript. This is a powerful paradigm that allows developers to enhance their Godot projects with fast, secure, single-binary-multi-platform extensions.

What is WebAssembly?

WebAssembly (Wasm) is a binary format that runs in a sandboxed virtual machine. It provides a portable target for many programming languages. That means that you can compile a Wasm module once from a language of your choice e.g. Rust, Go, C++ and run it on any platform!

Additionally, WebAssembly's sandboxed model means that you can run untrusted binaries without fear that they can corrupt your machine or access files which they shouldn't!

What is WASI?

WASI (WebAssembly System Interface) provides a POSIX-like interface definition that allows WebAssembly modules to interact with the machine on which they're being run (the host). Without WASI, WebAssembly modules have no way to access simple host features available outside of the Wasm VM like printing to to STDOUT, file access, cryptographicly-secure random number generation, system clock time, etc.

Godot Wasm implements a basic subset of these interfaces to extend the abilities of Wasm modules running in Godot. However, to promote security, not all interfaces are provided by Godot Wasm be default. If you need to support a new WASI behaviour, you're free to do so via the imports you provide to a Wasm module when instantiating via Godot Wasm's Wasm.instantiate() method.

Why Would I Use Wasm in Godot?

There are many reasons why you'd want the ability to run Wasm in Godot and interact with it via GDScript!

  • Use (almost) any language! You can create a WebAssembly module from any compatible language e.g. Rust, Go, AssemblyScript. This provides the ability to use existing libraries for these languages.
  • Safe & sandboxed. Securely run untrusted mods. Because WebAssembly is sandboxed, you can run Wasm modules from untruisted sources without risking your users' safety. This opens the door for zero-trust mods and plugins for your Godot project.
  • Compile once, run everywhere. Wasm is a portable binary format meaning there's no need to compile binaries for Windows x86, Windows x64, macOS ARM, macOS x86, Linux, etc. separately.
  • Raw speed! WebAssembly runs incredibly fast. This is particularly apparent when compared to Godot's high-level GDScript. Take a look at some benchmarks for a comparison.

How Do I Build a Wasm Module?

There are many ways in which to build a Wasm modules and many programming languages from which to compile. See Awesome Wasm Langs for an idea of the breadth of Wasm support.

Mozilla provides a great guide on getting started building Wasm modules. For some concrete examples of building a module from a variety of languages, see here.

Do I Export My Project?

Godot Wasm can be exported to Windows, macOS, and Linux.

Once Godot Wasm has been installed, you can export your Godot project as you typically might. For macOS exports, you'll have to disable library validation in Project → Export → Options. Finally, if you're including Wasm binaries in your project, you'll need to mark these for inclusion in your export by adding *.wasm in Project → Export → Resources.

What Runtime Does Godot Wasm Use?

Godot Wasm uses Wasmer as the underlying WebAssembly runtime. Godot Wasm interfaces with Wasmer via the Wasm C API meaning that swapping out the runtime for another that supports this API e.g. Wasmtime is theoretically possible, although untested.

Why Doesn't Godot Wasm Support Web Exports?

Currently, Wasmer, the runtime used by Godot Wasm, is not able to be exported to run in the web. There are issues tracking support for HTML5/web exports. See #15 and #18.

Clone this wiki locally