Previously spawned instance's sprite change into the newest instance's sprite

Godot Version

Godot_v4.5-stable_win64

Question

I plan on having variants to the cats that spawn and I’m trying to do it through changing the values on the animation player for each that spawn. Unfortunately for every new instance that spawns, the sprite of the previously spawned instance is also changed to the newest spawn. Is there a way to prevent this thank you

tscene of the cat:

func _ready() -> void:
	var variant = randi_range(0,7)
	
	var animation1 = $AnimationPlayer.get_animation("Cat1")
	var track1 =  animation1.find_track("Sprite2D:frame",0)
	animation1.track_set_key_value(track1,0,variant)
	animation1.track_set_key_value(track1,1,variant+8)
	
	var animation2 = $AnimationPlayer.get_animation("jump")
	var track2 = animation2.find_track("Sprite2D:frame",6)
	animation2.bezier_track_set_key_value(track2,0,variant+16.0)
	

tscene of the summon positions

func _on_main_summon() -> void:
	summonblock = randi_range(1,3)
	spawnentity = randi_range(1,2)
	match summonblock:
		1: summonpos = $Area2D.global_position
		2: summonpos = $Area2D2.global_position
		3: summonpos = $Area2D3.global_position
	
	if spawnentity == 2:
		var catspawn = cat.instantiate()
		get_tree().current_scene.add_child(catspawn)
		catspawn.global_position = summonpos
		
	else:
		var obstacle = obstacle1.instantiate()
		get_tree().current_scene.add_child(obstacle)
		obstacle.global_position = summonpos

Animations are shared resources, editing one animation track will change all other objects using that animation library.

Instead of changing the animations dynamically you can change the sprite’s texture, if you do not want to split the cat textures in another program you can use a new Atlas texture resource within Godot.

1 Like

i guess there’s no getting around it then.. i was trying to be creative with how it would handle variants, thanks for the explanation! i’ll try using atlases now

i managed to save some effort by turning on region and pushing the x value forward to the next column for every new variant!

var variants = (randi_range(0,7) * 243)
func _ready() -> void:
	$Sprite2D.set_region_rect(Rect2(variants,0.0,243.0,993.0))
1 Like

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