Skip to content

Instantly share code, notes, and snippets.

@bcsmith2846
Last active July 15, 2025 05:59
Show Gist options
  • Save bcsmith2846/c0523e2d4d2f2a3f4bded0880b0abc33 to your computer and use it in GitHub Desktop.
Save bcsmith2846/c0523e2d4d2f2a3f4bded0880b0abc33 to your computer and use it in GitHub Desktop.
extends Node2D
class ObservableProperty extends Node:
var _value: Variant
signal _value_changed(old_value, new_value)
func _set(property: StringName, value: Variant) -> bool:
if property == "value":
var old_value = _value
self._value = value
self._value_changed.emit(old_value, _value)
return true
return false
class GameProperties extends Node:
var properties = {
"speed": null,
"distance": null
}
func _set(property: StringName, value: Variant) -> bool:
if property in properties:
if properties[property] == null:
properties[property] = ObservableProperty.new()
if value is Callable:
properties[property]._value_changed.connect(value)
return true
else:
properties[property].value = value
return true
return false
func _get(property: StringName) -> Variant:
if property in properties:
return properties[property]._value
return null
func on_speed_changed(old_value, new_value):
print("Old speed value: %s\nNew speed value: %s" % [old_value, new_value])
func on_distance_changed(old_value, new_value):
print("Old distance value: %s\nNew distance value: %s" % [old_value, new_value])
func _init():
var props = GameProperties.new()
props.speed = on_speed_changed
props.speed = 1.0
print()
props.speed = 1.5
print()
print("Speed: %s" % props.speed)
print()
print("props.properties['speed'] is ObservableProperty? %s" % props.properties['speed'] is ObservableProperty)
print("props.speed is ObservableProperty? %s" % props.speed is ObservableProperty)
print("props.speed is float? %s" % props.speed is float)
print()
props.distance = 500
props.distance = on_distance_changed
props.distance = 100
print()
props.distance = 1000
print()
print("Distance: %s\n" % props.distance)
print("props.properties['distance'] is ObservableProperty? %s" % props.properties['distance'] is ObservableProperty)
print("props.distance is ObservableProperty? %s" % props.distance is ObservableProperty)
print("props.distance is int? %s" % props.distance is int)
print()
print("All properties: %s" % ", ".join(props.properties.keys()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment