From a593be82739ba5adfc3edec1119e2a3c7318df7f Mon Sep 17 00:00:00 2001 From: kziolkowski Date: Sat, 18 Oct 2025 17:15:35 +0200 Subject: [PATCH] :memo: added documentation and improved event scope --- Babushka.csproj | 1 + project.godot | 2 +- resources/events/{test.tres => test2.tres} | 8 +-- scenes/Babushka_scene_startMenu.tscn | 22 +++---- scripts/CSharp/Events/EventCaller.cs | 13 ---- scripts/CSharp/Events/EventListener.cs | 28 --------- scripts/CSharp/Events/EventResource.cs | 44 ------------- .../CSharp/Low Code/Events/EventListener.cs | 62 +++++++++++++++++++ .../Events/EventListener.cs.uid | 0 scripts/CSharp/Low Code/Events/EventRaiser.cs | 35 +++++++++++ .../Events/EventRaiser.cs.uid} | 0 .../CSharp/Low Code/Events/EventResource.cs | 62 +++++++++++++++++++ .../Events/EventResource.cs.uid | 0 13 files changed, 175 insertions(+), 102 deletions(-) rename resources/events/{test.tres => test2.tres} (54%) delete mode 100644 scripts/CSharp/Events/EventCaller.cs delete mode 100644 scripts/CSharp/Events/EventListener.cs delete mode 100644 scripts/CSharp/Events/EventResource.cs create mode 100644 scripts/CSharp/Low Code/Events/EventListener.cs rename scripts/CSharp/{ => Low Code}/Events/EventListener.cs.uid (100%) create mode 100644 scripts/CSharp/Low Code/Events/EventRaiser.cs rename scripts/CSharp/{Events/EventCaller.cs.uid => Low Code/Events/EventRaiser.cs.uid} (100%) create mode 100644 scripts/CSharp/Low Code/Events/EventResource.cs rename scripts/CSharp/{ => Low Code}/Events/EventResource.cs.uid (100%) diff --git a/Babushka.csproj b/Babushka.csproj index 76acd77..8d3587e 100644 --- a/Babushka.csproj +++ b/Babushka.csproj @@ -6,5 +6,6 @@ + \ No newline at end of file diff --git a/project.godot b/project.godot index c7180b7..ff69202 100644 --- a/project.godot +++ b/project.godot @@ -144,7 +144,6 @@ directories/tres_directory={ "default_bus_layout": "res://audio/default_bus_layout.tres", "default_stylebox": "res://addons/dialogic/Modules/DefaultLayoutParts/Layer_SpeakerPortraitTextbox/default_stylebox.tres", "default_vn_style": "res://addons/dialogic/Modules/DefaultLayoutParts/Style_VN_Default/default_vn_style.tres", -"events/test": "res://resources/events/test.tres", "farming_equipment_glossary": "res://dialog/farming_equipment_glossary.tres", "hoe": "res://resources/items/hoe.tres", "inventory_interactable_outline": "res://art/materials/inventory_interactable_outline.tres", @@ -163,6 +162,7 @@ directories/tres_directory={ "test/test_01": "res://resources/quests/test/test_01.tres", "test/test_02": "res://resources/quests/test/test_02.tres", "test/test_03": "res://resources/quests/test/test_03.tres", +"test2": "res://resources/events/test2.tres", "textbubble_style": "res://addons/dialogic/Modules/DefaultLayoutParts/Style_TextBubbles/textbubble_style.tres", "tomato": "res://resources/items/tomato.tres", "tomato_seed": "res://resources/items/tomato_seed.tres", diff --git a/resources/events/test.tres b/resources/events/test2.tres similarity index 54% rename from resources/events/test.tres rename to resources/events/test2.tres index a01c70d..14b8ba4 100644 --- a/resources/events/test.tres +++ b/resources/events/test2.tres @@ -1,10 +1,8 @@ -[gd_resource type="Resource" script_class="EventResource" load_steps=2 format=3 uid="uid://beed6ikqf46vk"] +[gd_resource type="Resource" script_class="EventResource" load_steps=2 format=3 uid="uid://bgfxakxxfmoxs"] -[ext_resource type="Script" uid="uid://ci3t5mvnopntg" path="res://scripts/CSharp/Events/EventResource.cs" id="1_6u8ge"] +[ext_resource type="Script" uid="uid://ci3t5mvnopntg" path="res://scripts/CSharp/Low Code/Events/EventResource.cs" id="1_jm77s"] [resource] -resource_name = "kafjhkj" -script = ExtResource("1_6u8ge") -Name = "kafjhkj" +script = ExtResource("1_jm77s") _showLog = true metadata/_custom_type_script = "uid://ci3t5mvnopntg" diff --git a/scenes/Babushka_scene_startMenu.tscn b/scenes/Babushka_scene_startMenu.tscn index 43f1fc7..55101a6 100644 --- a/scenes/Babushka_scene_startMenu.tscn +++ b/scenes/Babushka_scene_startMenu.tscn @@ -1,11 +1,11 @@ [gd_scene load_steps=7 format=3 uid="uid://c6wnoif01ltld"] [ext_resource type="Script" uid="uid://cssdu8viimwm6" path="res://scripts/CSharp/Common/SceneTransition.cs" id="1_fj2fh"] -[ext_resource type="Script" uid="uid://b5dotkx17gvxg" path="res://scripts/CSharp/Events/EventCaller.cs" id="3_beyjr"] +[ext_resource type="Script" uid="uid://b5dotkx17gvxg" path="res://scripts/CSharp/Low Code/Events/EventRaiser.cs" id="3_5a78e"] [ext_resource type="Texture2D" uid="uid://c7atj6ohlmir3" path="res://art/ui/StartScreen/titlescreen.png" id="3_r0y6o"] -[ext_resource type="Script" uid="uid://bc6uaaxsx5k5p" path="res://scripts/CSharp/Events/EventListener.cs" id="4_beyjr"] +[ext_resource type="Resource" uid="uid://bgfxakxxfmoxs" path="res://resources/events/test2.tres" id="4_5a78e"] [ext_resource type="Texture2D" uid="uid://du612t3xytly3" path="res://art/ui/StartScreen/babushkalog_white.png" id="4_dl0t1"] -[ext_resource type="Resource" uid="uid://beed6ikqf46vk" path="res://resources/events/test.tres" id="5_5a78e"] +[ext_resource type="Script" uid="uid://bc6uaaxsx5k5p" path="res://scripts/CSharp/Low Code/Events/EventListener.cs" id="6_5a78e"] [node name="BabushkaSceneStartMenu" type="Node2D"] script = ExtResource("1_fj2fh") @@ -55,7 +55,7 @@ toggle_mode = true text = "Quit " -[node name="Quit2" type="Button" parent="CanvasLayer/TextureRect"] +[node name="Test" type="Button" parent="CanvasLayer/TextureRect"] custom_minimum_size = Vector2(100, 30) layout_mode = 1 anchors_preset = 4 @@ -70,8 +70,8 @@ scale = Vector2(2, 2) toggle_mode = true text = "Test " -script = ExtResource("3_beyjr") -eventResource = ExtResource("5_5a78e") +script = ExtResource("3_5a78e") +_eventResources = Array[Object]([ExtResource("4_5a78e")]) [node name="Text" type="TextureRect" parent="CanvasLayer/TextureRect"] layout_mode = 1 @@ -83,11 +83,11 @@ scale = Vector2(0.5, 0.5) texture = ExtResource("4_dl0t1") stretch_mode = 3 -[node name="Node" type="Node" parent="."] -script = ExtResource("4_beyjr") -_eventResource = ExtResource("5_5a78e") -_showLog = true +[node name="EventListener" type="Node" parent="."] +script = ExtResource("6_5a78e") +_eventResources = Array[Object]([ExtResource("4_5a78e")]) [connection signal="pressed" from="CanvasLayer/TextureRect/Start" to="." method="LoadScene"] [connection signal="pressed" from="CanvasLayer/TextureRect/Quit" to="." method="Quit"] -[connection signal="pressed" from="CanvasLayer/TextureRect/Quit2" to="CanvasLayer/TextureRect/Quit2" method="RaiseEvent"] +[connection signal="pressed" from="CanvasLayer/TextureRect/Test" to="CanvasLayer/TextureRect/Test" method="RaiseEvents"] +[connection signal="EventRaised" from="EventListener" to="CanvasLayer/TextureRect/Test" method="hide"] diff --git a/scripts/CSharp/Events/EventCaller.cs b/scripts/CSharp/Events/EventCaller.cs deleted file mode 100644 index c6171f2..0000000 --- a/scripts/CSharp/Events/EventCaller.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Godot; - -namespace Babushka.scripts.CSharp.Events; - -public partial class EventCaller : Node -{ - [Export] EventResource eventResource; - - public void RaiseEvent() - { - eventResource.Raise(); - } -} \ No newline at end of file diff --git a/scripts/CSharp/Events/EventListener.cs b/scripts/CSharp/Events/EventListener.cs deleted file mode 100644 index 54a6307..0000000 --- a/scripts/CSharp/Events/EventListener.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Godot; - -namespace Babushka.scripts.CSharp.Events; - -public partial class EventListener : Node -{ - [Export] private EventResource _eventResource; - [Export] private bool _showLog; - - [Signal] public delegate void EventRaisedEventHandler(); - - public override void _EnterTree() - { - _eventResource.RegisterListener(this); - } - - public override void _ExitTree() - { - _eventResource.UnregisterListener(this); - } - - public void RaiseEvent() - { - if(_showLog) - GD.Print("Event Raised on: " + Name); - EmitSignal(SignalName.EventRaised); - } -} \ No newline at end of file diff --git a/scripts/CSharp/Events/EventResource.cs b/scripts/CSharp/Events/EventResource.cs deleted file mode 100644 index fe4771f..0000000 --- a/scripts/CSharp/Events/EventResource.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Collections.Generic; -using Godot; - -namespace Babushka.scripts.CSharp.Events; - -[GlobalClass] [Tool] -public partial class EventResource : Resource -{ - [Export] private string Name - { - get { return ResourceName; } - set { ResourceName = value; } - } - - [Export] private bool _showLog; - [ExportToolButton("Raise")] Callable _raiseAction => Callable.From(Raise); - - private List _eventListeners = new (); - - public void RegisterListener(EventListener listener) - { - if(_showLog) - GD.Print("Registering listener " + listener); - _eventListeners.Add(listener); - } - - public void UnregisterListener(EventListener listener) - { - if(_showLog) - GD.Print("Unregistering listener " + listener); - _eventListeners.Remove(listener); - } - - public void Raise() - { - if(_showLog) - GD.Print("Raising event: " + Name); - - foreach (var eventListener in _eventListeners) - { - eventListener.RaiseEvent(); - } - } -} \ No newline at end of file diff --git a/scripts/CSharp/Low Code/Events/EventListener.cs b/scripts/CSharp/Low Code/Events/EventListener.cs new file mode 100644 index 0000000..2bf3693 --- /dev/null +++ b/scripts/CSharp/Low Code/Events/EventListener.cs @@ -0,0 +1,62 @@ +using System; +using Godot; +using Godot.Collections; + +namespace Babushka.scripts.CSharp.Low_Code.Events; + +/// +/// Listens to an as long as this node is in the tree (i.e., during playtime). +/// +public partial class EventListener : Node +{ + /// + /// The event resource to listen to. + /// + [Export] private Array _eventResources; + + /// + /// Log to console when this event is being raised. + /// + [Export] private bool _showLog; + + /// + /// The signal that is triggered when this listener is called by one of the . + /// + [Signal] public delegate void EventRaisedEventHandler(); + + + /// + /// Subscribes to all s present in the array. + /// + /// + public override void _EnterTree() + { + foreach (var eventRes in _eventResources) + { + eventRes.RegisterListener(this); + } + } + + /// + /// Unsubscribes from all s present in the array. + /// + /// + public override void _ExitTree() + { + foreach (var eventRes in _eventResources) + { + eventRes.UnregisterListener(this); + } + } + + /// + /// Called by a s from the array. + /// Propagates the event by emitting signal. + /// + public void Invoke() + { + if(_showLog) + GD.Print("Event Raised on: " + Name); + EmitSignal(SignalName.EventRaised); + } +} \ No newline at end of file diff --git a/scripts/CSharp/Events/EventListener.cs.uid b/scripts/CSharp/Low Code/Events/EventListener.cs.uid similarity index 100% rename from scripts/CSharp/Events/EventListener.cs.uid rename to scripts/CSharp/Low Code/Events/EventListener.cs.uid diff --git a/scripts/CSharp/Low Code/Events/EventRaiser.cs b/scripts/CSharp/Low Code/Events/EventRaiser.cs new file mode 100644 index 0000000..2bebce3 --- /dev/null +++ b/scripts/CSharp/Low Code/Events/EventRaiser.cs @@ -0,0 +1,35 @@ +using Godot; +using Godot.Collections; + +namespace Babushka.scripts.CSharp.Low_Code.Events; + +/// +/// Raises one or more s from the scene. +/// +public partial class EventRaiser : Node +{ + /// + /// The s to call + /// + [Export] Array _eventResources; + + /// + /// Raises all s present in . + /// + public void RaiseEvents() + { + foreach (var eventRes in _eventResources) + { + eventRes.Raise(); + } + } + + /// + /// Raises at index. + /// + /// + public void RaiseEvent(int index) + { + _eventResources[index].Raise(); + } +} \ No newline at end of file diff --git a/scripts/CSharp/Events/EventCaller.cs.uid b/scripts/CSharp/Low Code/Events/EventRaiser.cs.uid similarity index 100% rename from scripts/CSharp/Events/EventCaller.cs.uid rename to scripts/CSharp/Low Code/Events/EventRaiser.cs.uid diff --git a/scripts/CSharp/Low Code/Events/EventResource.cs b/scripts/CSharp/Low Code/Events/EventResource.cs new file mode 100644 index 0000000..1560c00 --- /dev/null +++ b/scripts/CSharp/Low Code/Events/EventResource.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using Godot; + +namespace Babushka.scripts.CSharp.Low_Code.Events; + +/// +/// Represents an event in the project space. +/// Can be called by and subscribed to by . +/// +[GlobalClass] [Tool] +public partial class EventResource : Resource +{ + /// + /// Log into console when this event resource is adding or removing listeners, and when it's raised. + /// + [Export] private bool _showLog; + + /// + /// Raise-Button Call with Editor Export for easier debugging. + /// Beware: This will only work with custom event listeners that register at edit-time. + /// Standard register and deregister during playtime. + /// + [ExportToolButton("Raise")] Callable _raiseAction => Callable.From(Raise); + + private List _eventListeners = new (); + + /// + /// Adds an EventListener to the calling list for this event. + /// + /// + public void RegisterListener(EventListener listener) + { + if(_showLog) + GD.Print("Registering listener " + listener); + _eventListeners.Add(listener); + } + + /// + /// Removes an Eventlistener from the calling list for this event. + /// + /// + public void UnregisterListener(EventListener listener) + { + if(_showLog) + GD.Print("Unregistering listener " + listener); + _eventListeners.Remove(listener); + } + + /// + /// Raises this event on all current listeners. + /// + public void Raise() + { + if(_showLog) + GD.Print("Raising event: " + ResourcePath.GetFile().TrimSuffix(".tres")); + + foreach (var eventListener in _eventListeners) + { + eventListener.Invoke(); + } + } +} \ No newline at end of file diff --git a/scripts/CSharp/Events/EventResource.cs.uid b/scripts/CSharp/Low Code/Events/EventResource.cs.uid similarity index 100% rename from scripts/CSharp/Events/EventResource.cs.uid rename to scripts/CSharp/Low Code/Events/EventResource.cs.uid