From c768e0dfb28303bc579855f740681f6de8344604 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Fri, 28 Nov 2025 16:48:16 +0000 Subject: [PATCH 1/2] Find coin nodes by their group Previously, game_logic.gd walked recursively over all nodes in the tree, counting those which are an instance of the Coin script. A more idiomatic way to do this is to place coins into a group, and then count how many nodes are in that group. This will also make it easier to have some coins that don't count towards the goal, by having those coins not be in this group. --- components/coin/coin.tscn | 2 +- project.godot | 1 + scripts/rules_goals/game_logic.gd | 11 +---------- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/components/coin/coin.tscn b/components/coin/coin.tscn index 74cd040..21c4b06 100644 --- a/components/coin/coin.tscn +++ b/components/coin/coin.tscn @@ -6,7 +6,7 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_5w0o8"] radius = 46.72 -[node name="Coin" type="Area2D"] +[node name="Coin" type="Area2D" groups=["coins"]] collision_layer = 2 script = ExtResource("1_4bks0") diff --git a/project.godot b/project.godot index 2c0a272..873ecb5 100644 --- a/project.godot +++ b/project.godot @@ -32,6 +32,7 @@ window/stretch/mode="canvas_items" [global_group] players="" +coins="Coins that must be collected to win the level" [input] diff --git a/scripts/rules_goals/game_logic.gd b/scripts/rules_goals/game_logic.gd index 9f6287b..0d07002 100644 --- a/scripts/rules_goals/game_logic.gd +++ b/scripts/rules_goals/game_logic.gd @@ -44,13 +44,6 @@ func _set_lives(new_lives): Global.lives = lives -func _get_all_coins(node, accumulator = []): - if node is Coin: - accumulator.append(node) - for child in node.get_children(): - _get_all_coins(child, accumulator) - - # Called when the node enters the scene tree for the first time. func _ready(): if Engine.is_editor_hint(): @@ -65,9 +58,7 @@ func _ready(): if win_by_collecting_coins: Global.coin_collected.connect(_on_coin_collected) if coins_to_win == 0: - var coins = [] - _get_all_coins(get_parent(), coins) - coins_to_win = coins.size() + coins_to_win = get_tree().get_node_count_in_group(&"coins") if win_by_reaching_flag: Global.flag_raised.connect(_on_flag_raised) From 97d1a24d5fca737f46983812c57026a1660b6ca9 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Fri, 28 Nov 2025 17:05:15 +0000 Subject: [PATCH 2/2] game_logic: Remove slider hack for coins_to_win The bug this was taking advantage of was fixed in Godot 4.4. The property is displayed with a spin control despite this hack. Godot 4.6 will add a new "prefer_slider" hint . Remove the weird 0.9-coin step, and replace the HACK comment with a TODO for when we update to Godot 4.6. --- scripts/rules_goals/game_logic.gd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/rules_goals/game_logic.gd b/scripts/rules_goals/game_logic.gd index 0d07002..b129bad 100644 --- a/scripts/rules_goals/game_logic.gd +++ b/scripts/rules_goals/game_logic.gd @@ -7,12 +7,12 @@ extends Node ## Should you win the game by collecting coins? @export var win_by_collecting_coins: bool = false -# HACK: the step needs to be 0.9 for displaying a slider. +# TODO: When the game is updated to Godot 4.6, add prefer_slider hint ## How many coins to collect for winning? ## If zero, all the coins must be collected.[br] ## [b]Note:[/b] if you set this to a number bigger than the actual coins, ## the game won't be winnable. -@export_range(0, 100, 0.9, "or_greater") var coins_to_win: int = 0 +@export_range(0, 100, 1, "or_greater") var coins_to_win: int = 0 ## Should you win the game by reaching a flag?[br] ## If the option to win by collecting coins is also set, then it will only be