Context objects - Properties

2025-02-07

Properties are gdscript variables - they are class members. An example property of a Camera3D is fov.

Custom classes also have properties:

extends Node
class_name example_class

signal _webview_update(property_names: Array[String])
signal _webview_property_changed(property_name: String)

signal random_signal(some_parameter: String)

var cool_variable = 1337

func test_func(some_arg: int, another_arg: Array):
    print("test_func called with args: ", some_arg, " ", another_arg)

gdscript:

example_obj = example_class.new()
$WebView.set_context_node("example_obj", example_obj)

In the above example, the only (custom) property is cool_variable.

Javascript

In javascript, accessing, and changing property values should go through .value:

window.addEventListener("godot.ready", event => {
  // access
  console.log("cool_variable value: ", $g.example_obj.cool_variable.value);

  // write
  $g.example_obj.cool_variable.value = 42;
});

To detect updates from gdscript:

$g.example_obj.cool_variable.on("changed", (value) => {
  console.log("new value: ", value);
});

GDScript -> Javascript

To update a property, use the _webview_update signal. It takes a list of strings you'd like to update; the property names. In our case the name is cool_variable:

# update it
example_obj.cool_variable = 123
# commit change
example_obj.emit("_webview_update", ["cool_variable"])

In gdscript you can monitor when a property was updated from javascript, via the _webview_property_changed signal.

example_obj.connect("_webview_property_changed", _on_webview_property_changed)

func _on_webview_property_changed(property_name: String):
    pass

Supported types & Performance

See Context objects - Limitations