Godot Version
4.3.stable.mono
Question
` Hi,
I’m making a game that involves several minigames, and one of them is a very basic game where when you click on a target, the target gets destroyed. The amount of targets in the game is a random number between 1 and 4 targets, and all of them are loaded in when the game is first loaded, so they are not in the scene tree from the very beginning.
I have everything else set up and it works fine; the reticle, the random amount and positions of the targets, the collisions for the targets, the functionality to destroy the target with queue_free when it is clicked on and the counter to show how many targets have been destroyed
The one thing I can’t get to work is that when I click on one target, no matter which one it is, all of them get destroyed at the same time.
I’ve tried putting the targets inside of a seperate array, a group for the targets, even using ‘self’ on a specific instance gets me the same result
Theres a few moving parts to this minigame, so I’ll just post the code for the 3 main components; the mouse cursor (named “Reticle”), the target itself, and the gamemode.
Mouse Cursor/Reticle:
extends Node2D
@onready var reticle_collision: ShapeCast2D = $ColorRect/ShapeCast2D
signal reticle_hit()
signal target_hit
func _process(delta: float) -> void:
position = get_viewport().get_mouse_position()
if reticle_collision.is_colliding():
reticle_hit.emit()
print("Target Hit! " + str(reticle_collision.get_collider(0)))
func _input(event: InputEvent) -> void:
if event.is_action_pressed("Use", false):
$ColorRect/ShapeCast2D.enabled = true
$ColorRect.modulate = Color.RED
print("Gun Firing")
if event.is_action_released("Use"):
$ColorRect/ShapeCast2D.enabled = false
$ColorRect.modulate = Color.WHITE
print("Gun Stopped")
Targets:
extends Node2D
@onready var Target = $TargetColour
signal target_hit(hit_object)
var myself
func _ready() -> void:
myself = self
func _on_reticle_reticle_hit() -> void:
Target.modulate = Color.GREEN_YELLOW
$TargetColour/Area2D.monitorable = false
target_hit.emit(myself)
Game Mode/“ShootMain”:
extends Node2D
@onready var ScoreDisplay = $scoretest
@onready var ShootGame = $"."
var target_spawn = preload("res://Scenes/Minigames/Shoot/target.tscn")
var target_instance = target_spawn.instantiate()
var score = 0
var starting_targets
var targets_remaining
func _ready() -> void:
starting_targets = randi_range(1, 4)
print("STARTING TARGETS: " + str(starting_targets))
ScoreDisplay.text = str(score)
for n in starting_targets:
target_instance = target_spawn.instantiate()
var count = n
target_instance.name = "Target_" + str(count)
var instance_position_x = randi_range(100, 1000)
var instance_position_y = randi_range(100, 550)
add_child(target_instance)
target_instance.add_to_group("Targets")
$Reticle.reticle_hit.connect(target_instance._on_reticle_reticle_hit)
target_instance.target_hit.connect(ShootGame._on_target_instance_target_hit)
target_instance.position = Vector2(instance_position_x, instance_position_y)
targets_remaining = get_tree().get_node_count_in_group("Targets")
print("Current Targets: " + str(get_tree().get_nodes_in_group("Targets")))
func _on_target_instance_target_hit(hit_object) -> void:
hit_object.queue_free()
score += 1
ScoreDisplay.text = str(score)
print("Target Destroyed!")
print("Targets Remaining: " + str(targets_remaining))
Heres the scene tree for reference: