Multiple Instance of a Scene with animation show the same result

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By karg

So i have this code who loop thru my 2 instanced scene(wich is one scene instanced twice so 2 identical instance) . I have an animation player in a sprite showing the result of the variable ion a 5 stars rating so 50= 2.5 stars and 100 = 5 stars. But the 2 animation are always showing the same result wich is the last instance going thru the loop( resulting in 50 so showing 2.5 stars)

CODE:

extends Node


export var categorie_scores : Dictionary = {
	"efficiency": 100,
	"durability": 50
}

#Function
func _ready():
	var i = 0
	for category in categorie_scores.keys():
		var progress : Node = self.get_child(i)
		var score : int = categorie_scores[category]
		var _succ = progress.setCoeff(score)
		progress.startAnimation()
		print("%s -> %s : %d" % [progress.name, category, score])
		i += 1

i tried to debug but everything seems to work fine…
Here is what the print shows in the console:

5
FiveStarProgress -> efficiency : 100
2.5
FiveStarProgress2 -> durability : 50

So my question i guess is how can i make two identical instance work . ive tried with the instance() function two and same result( with a different code than this one of course ) the 2 animations are always showing the same result instead of showing the 2 result it should be.

Thanks a lot in advancefor the help!

startAnimation function and SetCoeff Function:

extends Sprite


#var
var coeff
var duration : float = -1
var number_anim_playing : int = 1

# Called when the node enters the scene tree for the first time.
func _ready():
	pass


func setCoeff(value : int) -> bool :
	if value < 0 or value > 100:
		print("coeff should be between 0 and 100")
		return false
	
	duration = ((value * 5) / 100.0)
	return true


func startAnimation():
	if duration == -1:
		print("you need to set the coeff first")
		return
		
	$AnimationPlayer.play("emptyToFull", -1, 1)
	$AnimationPlayer.get_animation('emptyToFull').length = duration

ps: hope the explanation is not to hard to understand

By the looks of it, you don’t seem to be passing any arguments to the startAnimation() function. Can you post the code that drives the animation?

johnygames | 2020-08-16 22:59

Hey thanks for your comment. i edited the post to add the animation function. Hope u can help !

karg | 2020-08-16 23:17

:bust_in_silhouette: Reply From: johnygames

You use one animation resource for both of your animations and the animation’s length parameter controls how many stars the AnimationPlayer will display, am I right? Digging into this I found this issue on github where it is stated that animations are shared resources in Godot. The way I understand it, this practically means that changing an animation’s value in one instance automatically changes this value in all instances. In other words, You can’t have it play the same animation differently, or at least that is what I figured. I also tried a contrived example and it seems to hold true: With two identical instances, when I changed the length of the first instance, the second changed too.

You have to find another way to display the animation, maybe without using AnimationPlayer at all. I do not know what your visuals look like, but I would create an array of star sprites and arrange them one next to the other according to score.

If you definitely have to use your method and have one animation for all cases, then
I guess you could make use of the duplicate function. Duplicate the scene and add it as a child to your main scene via code.

Thanks. I’ve search everywhere but i didnt se this article and it help me a lot to understand everything in the background so many thanks :slight_smile:

I made it work with duplicate() like they were saying in the link u sent.

karg | 2020-08-17 23:09

Glad it helped :slight_smile:

johnygames | 2020-08-18 01:18