0 votes

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

in Engine by (15 points)
edited by

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?

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

1 Answer

+1 vote
Best answer

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.

by (1,825 points)
selected by

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 :)

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

Glad it helped :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories