Created
April 13, 2026 15:34
-
-
Save WolfgangSenff/cdad3289b0943acfa748f59ce026b21c to your computer and use it in GitHub Desktop.
Riverpod in Godot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @tool | |
| extends Node | |
| # This is just a singleton that represents my game data in the game. Everything goes into this, and everything comes out of | |
| # it. The fun part? Anything in it becomes a type to which you can connect to the changed signal. | |
| var _game_state : Dictionary = {} | |
| func _get(property): | |
| if not _game_state.has(property): | |
| _game_state[property] = DataChangeListener.new() | |
| return _game_state[property] | |
| func _set(property, value): | |
| if not _game_state.has(property): | |
| _game_state[property] = DataChangeListener.new() | |
| _game_state[property].value = value | |
| return true | |
| func listen(property: String) -> DataChangeListener: | |
| if not _game_state.has(property): | |
| _game_state[property] = DataChangeListener.new() | |
| return _game_state[property] | |
| class DataChangeListener extends RefCounted: | |
| signal changed(new_value: Variant) | |
| var value: | |
| set(v): | |
| if value != v: | |
| value = v | |
| changed.emit(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Label # Could easily target any type, so long as the func connected to the changed signal takes in the correct value(s)/type(s) | |
| class_name TrackingLabel | |
| @warning_ignore("onready_with_export") | |
| @export @onready var variable_name: String | |
| # Would not be hard to make this a separate function that takes the variable name and the callback, but this felt more Godot-y | |
| func _ready() -> void: | |
| GameData[variable_name].changed.connect(func(value): text = str(value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment