Kaynağa Gözat

:wrench: Automatically destroy container when every prompt is discarded

DricomDragon 2 hafta önce
ebeveyn
işleme
4f15643cb2

+ 1 - 5
godot/component/ui/input_prompts/move/move_container.tscn

@@ -1,6 +1,6 @@
 [gd_scene load_steps=15 format=3 uid="uid://y4gc5uinkcqj"]
 
-[ext_resource type="Script" path="res://component/ui/input_prompts/move/move_prompt.gd" id="1_b1cf6"]
+[ext_resource type="Script" path="res://component/ui/input_prompts/prompt_container.gd" id="1_b1cf6"]
 [ext_resource type="Texture2D" uid="uid://cah6r1q5x2lte" path="res://addons/input_prompts/icons/generic/left_stick_left.png" id="1_lp1l5"]
 [ext_resource type="Script" path="res://addons/input_prompts/action_prompt/action_prompt.gd" id="2_hmylp"]
 [ext_resource type="Texture2D" uid="uid://c03r3n0nfrjck" path="res://addons/input_prompts/icons/generic/left_stick_right.png" id="3_l042n"]
@@ -94,10 +94,6 @@ icon = 0
 events = Array[InputEvent]([SubResource("InputEventKey_3sn2y"), SubResource("InputEventJoypadMotion_kmldo")])
 
 [connection signal="pressed" from="MoveLeftActionPrompt" to="MoveLeftActionPrompt" method="queue_free"]
-[connection signal="tree_exited" from="MoveLeftActionPrompt" to="." method="_on_child_exited_tree"]
 [connection signal="pressed" from="MoveRightActionPrompt" to="MoveRightActionPrompt" method="queue_free"]
-[connection signal="tree_exited" from="MoveRightActionPrompt" to="." method="_on_child_exited_tree"]
 [connection signal="pressed" from="MoveForwardActionPrompt" to="MoveForwardActionPrompt" method="queue_free"]
-[connection signal="tree_exited" from="MoveForwardActionPrompt" to="." method="_on_child_exited_tree"]
 [connection signal="pressed" from="MoveBackActionPrompt" to="MoveBackActionPrompt" method="queue_free"]
-[connection signal="tree_exited" from="MoveBackActionPrompt" to="." method="_on_child_exited_tree"]

+ 0 - 14
godot/component/ui/input_prompts/move/move_prompt.gd

@@ -1,14 +0,0 @@
-extends Control
-## Show basic movements
-
-
-func remove_if_no_more_prompt():
-	print(get_children().size(), " remaining children")
-	if get_children().size() <= 1:
-		# Only label remaining
-		print("Destruction !")
-		queue_free()
-
-
-func _on_child_exited_tree() -> void:
-	remove_if_no_more_prompt()

+ 27 - 0
godot/component/ui/input_prompts/prompt_container.gd

@@ -0,0 +1,27 @@
+class_name PromptContainer
+extends Control
+## Free itself when no prompt remaining
+
+
+var _nb_prompts: int
+
+
+func _ready() -> void:
+	_nb_prompts = 0
+	for child in get_children():
+		if child is InputPrompt:
+			child.tree_exited.connect(_on_child_exited_tree)
+			_nb_prompts += 1
+
+
+func remove_if_no_more_prompt():
+	_nb_prompts -= 1
+	print(_nb_prompts, " remaining prompts")
+	if _nb_prompts <= 0:
+		# Only label remaining
+		print("Destruction !")
+		queue_free()
+
+
+func _on_child_exited_tree() -> void:
+	remove_if_no_more_prompt()