GithubHelp home page GithubHelp logo

devsystem / godot-wasm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dheatly23/godot-wasm

0.0 0.0 0.0 298 KB

WebAssembly binding for Godot

License: Apache License 2.0

Rust 88.05% GDScript 11.88% WebAssembly 0.07%

godot-wasm's Introduction

godot-wasm

WebAssembly binding for Godot, with rich feature set.

Introduction

Hello there! Thanks for checking out this library ๐Ÿ™. I published this as my future portfolio on my coding adventure. This is my hobby project, developed over 6 months now. It has gone through a lot of changes. And with Godot 4 on horizon, i might port it to the next version (if and when rust bindings is finalized).

For the Godot 3 version, check out branch gdnative. There is also a gdextension branch that tracks interim porting to GDExtension.

โš  WARNING! Very Beta! โš 

This repository is changing rapidly. And since i don't really like semver, things might break unexpectedly. Just follow the latest commit and you're probably safe.

Documentation is the source code itself. If you want to document, great! But i'm just really lazy and perhaps old-school.

Features

  • Easily run any WASM module.
  • Supports WAT compilation.
  • Imports any (custom) Godot methods into WASM.
  • Easy access to linear memory, per-element basis or bulk array operation.
  • Catch and throw runtime error/traps with signal.
  • Epoch-based limiter to stop bad-behaving module.
  • Memory limiter to prevent exhaustion.
  • Experimental API for direct Godot object manipulation.

Building

To build the addon:

  1. Clone this repository
  2. Install cargo-make (see installation guide here)
  3. Run cargo make deploy
  4. Copy addon in out/addons/godot_wasm to your project

Using the Library

After adding it to your Godot project, there are 2 classes added by the library:

  • WasmModule : Contains the compiled WebAssembly module.
  • WasmInstance : Contains the instantiated module.

Due to limitation of godot-rust, you must call initialize after creating new object. Here is a snippet of example code:

const WAT = """
(module
  (func $add (export "add") (param i64 i64) (result i64)
    local.get 0
    local.get 1
    i64.add
  )
)
"""

func _ready():
  # initialize() returns itself if succeed and null otherwise
  # WARNING! DO NOT USE UNINITIALIZED/FAILED MODULE OBJECTS
  var module = WasmModule.new().initialize(
    "test", # Name of module (not really used)
    WAT, # Module string (accepts PoolByteArray or String)
    {} # Imports to other module
  )

  # Create instance from module
  var instance = WasmInstance.new().initialize(
    module, # Module object
    {} # Host imports
    # Configuration (optional)
  )
  # Convenience method
  # var instance = module.instantiate({})

  # Call to WASM
  print(instance.call_wasm("add", [1, 2]))

  # There are many more methods of WasmInstance, including:
  # - Trapping (signal_error/signal_error_cancel)
  # - Epoch (reset_epoch)
  # - Memory (too many to list here)
  # See it's source code (src/wasm_instance.rs) for list.

With the addon, there are many more helper scripts too:

  • WasmHelper : Autoload that contains many helper functions to load WebAssembly code.
  • WasmFile/WasmFileLoader : Importer that automatically imports WASM/WAT file. It also lazily compile and cache module.

Potential Uses

There are many uses of running WebAssembly code in Godot. If you are looking for inspiration or just confused about the purpose of this package, here are some prompts:

Language-independent* programming game

Many programming language now supports compiling to WebAssembly. And with many programming type game out there, it would be awesome to transfer your skill at your favourite programming language into the game. Bonus, if somehow your program has bugs, it won't corrupt or crash the game.

*Right now, very few programming language can emit standalone WASM.

Competitive robot/AI game

Isn't that obvious enough? Tied to previous one, a really great use is some sort of competitive multiplayer AI vs AI game. With sandboxing of WebAssembly, no code can do any harm to participant/judge.

Custom userscript

Instead of making your own scripting language to integrate into your game, why not consider sandboxing it within WebAssembly?

Modding framework

WebAssembly can replace DLL/SO as a way to mod your game. Using it as easy as exposing your API as imports. Plus, sandboxing makes any mod automatically be safe from doing malicious things.

Server-sent Mod

With mods there will always problem with multiplayer. Imagine having to install random code just to join your favorite server. And don't forget to juggle mods for different servers. Well no more, the server could just send you the mods and assets necessary to join. It's automatic, painless, and of course, safe. Think of browsers, where you load untrusted website code safely.

godot-wasm's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.