Skip to content

Instantly share code, notes, and snippets.

@schweigert
Created November 7, 2025 14:05
Show Gist options
  • Select an option

  • Save schweigert/63f5ea1bfc60964323c6ed0a0f7322b9 to your computer and use it in GitHub Desktop.

Select an option

Save schweigert/63f5ea1bfc60964323c6ed0a0f7322b9 to your computer and use it in GitHub Desktop.
class_name IntReactable extends Reactable
@export var value: int:
set(other_value):
if value != other_value:
value = other_value
react()
extends Node
@export var valor: Other
func _ready() -> void:
valor = Other.new()
valor.value1 = IntReactable.new()
valor.value2 = IntReactable.new()
valor.reacted.connect(self.reage)
valor.value1.value = 10
valor.value2.value = 50
func reage(source: Other, cause: Reactable) -> void:
print(source.value1.value)
print(source.value2.value)
print(cause == source.value1)
print(cause == source.value2)
class_name Other extends Reactable
@export var value1: IntReactable:
set(other_value):
if value1 != other_value:
depends_on(other_value)
release(value1)
value1 = other_value
react()
@export var value2: IntReactable:
set(other_value):
if value2 != other_value:
depends_on(other_value)
release(value2)
value2 = other_value
react()
@abstract class_name Reactable extends Resource
signal reacted(source: Reactable, cause: Reactable)
@export var dependencies: Array[Reactable] = []
func _init() -> void: for dependency in dependencies: dependency.reacted.connect(self._propagate)
func depends_on(dependency: Reactable) -> void:
dependencies.append(dependency)
dependency.reacted.connect(self._propagate)
func release(dependency: Reactable) -> void:
if dependency in dependencies:
dependencies.erase(dependency)
dependency.reacted.disconnect(self._propagate)
func react(cause: Reactable = null) -> void: reacted.emit(self, cause if cause != null else self)
func _propagate(_source: Reactable, cause: Reactable) -> void: react(cause)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment