How to stop instances of one scene from doing the same thing?

Godot Version

4.2.2

Question

I’m currently trying to make a score counter that goes up gradually (every 0.1 seconds). But the issue is that the numbers of the digits don’t go up separately, but instead all together. I turned on Local to Scene for the numbers in the editor, but that didn’t solve the issue.

Relevant code

Code for the number counter (changes the sprite up depending on the number):

extends Node

@export var w_size = 90
@export var h_size = 120

@onready var texture_rect = $MarginContainer/TextureRect

signal counting_finished
	
func number_roll(new_number : int):
	for i in range(new_number + 1):
		var timer = get_tree().create_timer(0.1)
		await timer.timeout
		
		change_number(i)
		
	counting_finished.emit()

func change_number(number : int):
	if number >= 10:
		number = number % 10
	texture_rect.texture.set_region(Rect2(w_size * number, 0, w_size, h_size))

Code controlling the number display (the numbers are instances of the same scene):

i = 1
	for key in sorted_dictionary:
		var container = $VBoxContainer.get_child(i).get_child(1)
		print(sorted_dictionary[key] % 10, int(sorted_dictionary[key] / 10))
		
		container.get_child(1).number_roll(sorted_dictionary[key] % 10)
		
		await container.get_child(1).counting_finished
		
		container.get_child(0).number_roll(int(sorted_dictionary[key] / 10))
		
		i += 1

Would be grateful for any help!

I’m guessing you are using an AtlasTexture. They are Resources and Resources are shared by default. You’ll need to make them unique:

  • Right click over it in the inspector and click on Make Unique
  • Enable Local to Scene in the Resource. This will make unique the resource when instantiating the scene (if the resource is shared between nodes in the same scene the copies won’t be made unique)
  • Calling Resource.duplicate() in code. For example in the _ready() function.
2 Likes

Yes, thank you! I thought the problem might be that, but I made the script local to scene instead of the AtlasTexture. Thank you!

1 Like

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