ashtonmeuser / godot-wasm

Interact with WebAssembly modules from Godot
https://github.com/ashtonmeuser/godot-wasm/wiki
MIT License
193 stars 11 forks source link

V-Sekai UGC System with MissingResource and MissingNode #43

Closed fire closed 1 year ago

fire commented 1 year ago

I am looking for help trying to define the godot wasm interface for MissingNode, and MissingResource.

Godot wasm needs to handle downstream nodes like Node3D, Control and Node2D.

My goal is to create a generic interface for handling incomplete data and providing a generic interface for behavior, ensuring that the UGC system remains flexible and adaptable to various types of content.

The V-Sekai UGC System is designed to provide a seamless experience for users to create, interact with, and share custom game objects and resources. By leveraging the power of Godot WebAssembly (godot-wasm), this system can efficiently process and manage user-generated content (UGC) within the virtual world.

MissingResource and MissingNode play crucial roles in handling incomplete data and providing a generic interface for behavior, ensuring that the UGC system remains flexible and adaptable to various types of content.

Security Note

Set a script will execute gdscript.

Sequence Diagram

sequenceDiagram
    title: V-Sekai UGC System with MissingResource and MissingNode
    participant User as User
    participant V-Sekai as V-Sekai
    participant godot-wasm as Godot WebAssembly
    participant UGC as User-Generated Content

    User->>V-Sekai: Create custom game objects and resources
    V-Sekai->>godot-wasm: Process custom content in engine
    godot-wasm->>UGC: Store custom content
    Note over UGC: MissingResource handles<br/>incomplete data
    Note over UGC: MissingNode serves as a<br/>generic interface for behavior

    User->>V-Sekai: Interact with custom content
    V-Sekai->>godot-wasm: Request custom content from engine
    godot-wasm->>UGC: Retrieve custom content
    godot-wasm-->>V-Sekai: Return custom content to V-Sekai
    V-Sekai-->>User: Display custom content in the virtual world

    User->>V-Sekai: Share custom content with others
    V-Sekai->>UGC: Distribute shared content to other players
fire commented 1 year ago

Here's the updated mermaid.js diagram representing the relationship between PackedScene, MissingNode, Wasm Binary, and Embedded Script with the change that the Wasm Binary is executed to read:

sequenceDiagram
participant A as PackedScene
participant B as MissingNode
participant C as Wasm Binary
participant D as Executed to read
participant E as Custom Class Name
participant F as Data inside (Embedded Script)

A->>B: Passes MissingNode
B->>C: Passes Wasm Binary
C->>D: Executes to read
D->>E: Reads Custom Class Name
D->>F: Reads Data inside (Embedded Script)
ashtonmeuser commented 1 year ago

Hey @fire I think that this is firmly outside of the scope of Godot Wasm issues and is probably a better fit for discussions. However, I'd love to try to understand your use case a little more and help you with your implementation as much as possible.

I think I'm missing some context for the graphs you've included. If you could help me understand how you're hoping to use Wasm modules, who owns them, etc., I'd be able to give better feedback. Are you interested in the security aspect as modules are to be uploaded by users? Are you more interested in using Wasm for heavy compute as an alternative to GDScript? Once loaded, what functions do the Wasm modules perform (high level)?

fire commented 1 year ago

The UGC WASM modules, uploaded by users for universal access, are attached to a MissingNode as a script, providing property data and behavior. The clever implementation involves MissingResource for resources and a dictionary of Variants for node properties.

fire commented 1 year ago

Behavior is within the dictionary in this design. Utilizing Godot's standard set_script property, WASM Godot only needs to implement dependencies for MissingNode and MissingResource, excluding components like Node3D.

fire commented 1 year ago

Here's a reply to your questions.

Wasm in V-Sekai Proposal

V-Sekai utilizes WebAssembly (Wasm) to enhance performance, offering an alternative to GDScript.

Ownership

Security

User-uploaded Wasm modules undergo a two-step process for safety and unauthorized access prevention:

  1. Validation: Ensure module structure and integrity adhere to security standards.
  2. Sandboxing: Confine modules in a secure environment, restricting system resource access and mitigating potential harm.

Purpose

Wasm serves multiple purposes in V-Sekai:

Functionality

Wasm modules facilitate advanced functions in virtual worlds and avatar-based social games, such as:

Implementation

The UGC (User-Generated Content) WASM modules, uploaded by users for universal access, are attached to a MissingNode as a script, providing property data and behavior. The clever implementation involves MissingResource for resources and a dictionary of Variants for node properties. Behavior is within the dictionary in this design. Utilizing Godot's standard set_script property, WASM Godot only needs to implement dependencies for MissingNode and MissingResource, excluding components like Node3D.

AI Assistance

This article was assisted by AI.

fire commented 1 year ago

I remade the article https://v-sekai.github.io/manuals/decisions/20230611-wasm-in-v-sekai.html.

ashtonmeuser commented 1 year ago

Thanks for the info. I think I understand your use case a little clearer. Let's start with the simpler case of MissingNode. You still certainly have a better sense of what you need so let me know your thoughts on the following.

  1. A PackedScene encounters a MissingNode and attempts to extract its class name and properties.
  2. A GDScript (or C++) shim that's a universal wrapper around the Wasm module (and directly uses Godot Wasm) allows translation of some higher-level types e.g. strings, arrays, dictionaries. This is the type that should be instantiated by the PackedScene.
  3. The wrapper node, when queried for its properties, should forward this query onto the Wasm module thus allowing UGC to define arbitrary node properties.

Does this sound roughly correct? If you have a clearer understanding, could you create a simple POC that uses this flow but simply defines properties in either GDScript/C++? With that, I could add the Wasm translation layer.

fire commented 1 year ago

We need to create a simple Godot Engine PackedScene with one node, called MissingNode.

This node will be initialized by a GDScript script (representing Godot-WASM). Let's call this missing_node.gd

ashtonmeuser commented 1 year ago

Awesome if you could draft up a POC Godot project with exactly that, that would be helpful!

In the actual implementation (post-POC), you'd replace hello_world.gd with logic contained in the UGC Wasm module, correct?

fire commented 1 year ago

I think this is what I got for a wasm_test.tscn.

[gd_scene load_steps=2 format=3 uid="uid://muen6x7gxbjo"]

[sub_resource type="GDScript" id="GDScript_wmpej"]
script/source = "extends Node

func _enter_tree():
    print(\"_enter_tree()\" % [])
"

[node name="Node3D" type="MissingNode"]
original_class = "Node3D"
script = SubResource("GDScript_wmpej")

It prints _enter_tree() when the scene is executed.

WASM Game Project.zip

fire commented 1 year ago

Here's another scene. We assume we know nothing about Node3D in godot gdscript here.

[gd_scene load_steps=2 format=3 uid="uid://bk1cybq0p5ekv"]

[sub_resource type="GDScript" id="GDScript_jtge8"]
script/source = "extends Marker3D

func _enter_tree():
    print(\"_enter_tree\")
    print(\"Add position\")
    var current_global_transform: Transform3D = get(\"global_transform\")
    current_global_transform.origin = current_global_transform.origin + Vector3(0, 1, 0)
    set(\"global_transform\", current_global_transform)
    print(get("global_transform"))
"

[node name="Marker3D" type="MissingNode"]
original_class = "Marker3D"
script = SubResource("GDScript_jtge8")
fire commented 1 year ago

tl;dr

MissingNode / MissingResource behaviour is a standard feature of Godot 4. It's been turned on and shipped in production.

I was using its properties with the fact that wasm can be degraded to use only Node and Resource methods to limit the godot-wasm implementation surface area.

fire commented 1 year ago

After discussion we ended up with this conclusion.

The proposed solution uses WebAssembly (Wasm) to enhance performance and functionality in V-Sekai through User-Generated Content (UGC) Wasm modules. These modules are attached to an existing base node as a script, enabling them to provide property data and behavior for nodes in the virtual world or game.

I think the original discussion went off into MissingNode and MissingResource