Accidental duplicates that aren't in hierarchy

I have a game that supposed to decrease a peg count when a spinning top hits them. My pegs weren’t contributing to the counter when they collided, and I thought it was because only one peg had the body_entered signal going to the spinner. I didn’t know how to add the signal to the rest of the pegs without creating a new function every time. I also didn’t know if there was a way to do it in the peg body sub tree scene because the spinner isn’t in that scene so there was no option to connect it.

So, what I did was delete all the pegs except the one that had the signal and duplicated it. But that just created two versions of everything, even the spinner. I only have 9 pegs in the hierarchy but there’s 18 when I run the game. If I delete one in the hierarchy, the duplicate still shows up. The ones in the hierarchy will reset when I start a new game, but the other duplicates will stay down. I’m not sure how this happened or how to get rid of it.

When you instantiate your pegs you can connect their collision signal to your peg count script:

var peg_parent : Node2D
var peg_scene : PackedScene

func instantiate_peg():
   var peg = peg_scene.instantiate()

   peg_parent.add_child(peg)
   peg.connect("my_collision_signal", Callable(self, "on_peg_collision")

func on_peg_collision():
   print("peg collision!")

peg script:

signal my_collision_signal

func _on_body_entered(body):
   # assuming your spinning top is in a group called "spinning_top"
   if body.is_in_group("spinning_top"):
      my_collision_signal.emit()

If you want to place your pegs by hand in your scene you could get the reference to your pegs by an exported array:

@export var pegs : Array[Node2D]

func connect_all_peg_signals:
   for peg in pegs:
      peg.connect("my_collision_signal", Callable(self, "on_peg_collision")

I hope that answers your question.

So I have the pegs that I put in the scene manually and then also instances of each peg and the spinner? How do I get rid of the duplicates instances so I just have the ones in the hierarchy? If they aren’t the hierarchy how do I delete them?

I am a little confused. So this is what you are trying to achieve:

  • Instantiating pegs & placing pegs manually in the scene
  • one spinning top in the scene
  • if spinning top collides with a peg: decrease peg count

?

In that case you can just use the on_body_entered signal of the spinning top and connect it to a function on its own script (assuming the legs are in a group called “peg”):

var peg_count : int = 9

func _on_body_entered(body):
   # check if collided with peg
   if body.is_in_group("peg"):
      # delete the peg if collided
      body.queue_free()

      peg_count -= 1

This would be a way easier solution than what I posted before

When I first added the pegs I placed them manually. There was only one of each, so nine in the scene. My question was what happened that caused there to be two of everything and how to fix it. If the instantiated pegs and manually added pegs are different, I only want the manually added pegs. But I’m not sure what happened in the first place to cause the duplications so I don’t know how to delete them. I’m new to Godot so I’m still trying to figure out my way around. Here’s a function I have in my spinner script:

func _on_peg_body_body_entered(body: Node) → void:
if body.name.contains(“peg”):
bump_play.play()
if body.standing == true:
body.standing = false
peg_count -= 1
show_count.text = “Moves: %d Best: %d Pegs left: %d” %[moves_number, best_score, peg_count]

Is there any other script? Can you show your scene hierarchy?

I have a peg body script and a spinner body script. The peg body only really has a variable standing that’s checked in the spinner script and the sound effect. Would it be better to show the whole spinner script too? How do I upload it on here so it looks better?

You can put ``` in front and after your script to keep its format.
Showing your spinner script would be great.

here’s the spinner script. It’s a little messy I think.

extends RigidBody3D

@onready var push_play: AudioStreamPlayer3D = $"../pushPlay"
@onready var bump_play: AudioStreamPlayer3D = $"../bumpPlay"
var spin := 20
var rand
@onready var show_count: RichTextLabel = $"../showCount"
var peg_count
var moves_number
var best_score = 100
@onready var timer_label: RichTextLabel = $"../Timer"

var timer = 30


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	rand = RandomNumberGenerator.new()
	peg_count = 9
	moves_number = 0


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if Input.is_action_just_pressed("ui_accept"):
		roll_spin()
	if Input.is_key_pressed(KEY_N):
		owner.get_tree().reload_current_scene()
	if timer > 0:
		timer -= delta
		timer_label.text = "Time: %d" %[timer]
	else:
		owner.get_tree().reload_current_scene()
	
func roll_spin():
	push_play.play()
	var force = Vector3(rand.randf_range(-spin, spin), 0, 
						rand.randf_range(-spin, spin))
	apply_impulse(force)
	moves_number += 1
	show_count.text = "Moves: %d Best: %d Pegs left: %d" %[moves_number, best_score, peg_count]


func _on_new_game_button_pressed() -> void:
	owner.get_tree().reload_current_scene()


func _on_peg_body_body_entered(body: Node) -> void:
	if body.name.contains("peg"):
		bump_play.play()
		if body.standing == true:
			body.standing = false
			peg_count -= 1
			print(body.standing)
			show_count.text = "Moves: %d Best: %d Pegs left: %d" %[moves_number, best_score, peg_count]

Since there is no instantiate function used in your scripts there shouldn’t be any duplications :face_with_raised_eyebrow:

Yeah I have no idea where the duplicates came from. Everything was fine until I duplicated the one peg that had the signal on it and replaced all the other pegs with the duplicates. But I deleted the originals and there’s also the problem of how it affected the spinner itself as well, since I never duplicated the spinner. Maybe it’s a bug but I hope not because that means it might be fixable. I don’t want to have to redo everything, especially because my assignment is already late now :confused: I appreciate you trying to help though.

So just to clarify when you start your game there are double the pegs that were there before you startet the game. If that’s the case I have no idea how to fix it. Sorry ):

Yes, that’s correct. Thanks for trying!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.