Play animation One

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


I have got a basic scene with an animation and when the animation is finished, then it loads a new scene and when you load back in to the animated scene, the the animation starts again, where as i don’t want it too. I have been trying solution and researching regarding this for just over 2 weeks. I have tried singletons, animation states and even trying with simple code.

Her is my code below:

extends Camera2D

var animation_has_played = false

# Get a reference to the AnimationPlayer, which is a child of the camera.
onready var anim = $AnimationPlayer

func _ready( ):
if not animation_has_played:
	animation_has_played = true
	remove_child(AnimationPlayer, Camera2D)

func _on_AnimationPlayer_animation_finished(camera_auto_move):
if animation_has_played:
	if animation_has_played:

My scene tree is below:

Scene Tree

Any help appreciated and thanks in advance

:bust_in_silhouette: Reply From: exe2k

As i see in your code above the variable animation_has_played will NEVER work as you expect because since u go back to the scene u (RE)LOAD this scene again, and the scripts says every single time the scene is loaded:

var animation_has_played = false

Using singletons is the right way. Create a new script without attaching it to any node. Name it like (example) and move this var into

var animation_has_played = false

go Project settings - autoload and add this script there. Then set global var to TRUE after animation has played. U can do it in the script of the scene\level. Like this:

Global.animation_has_played = true

and of course the first line of your _ready() fn in the scene with animation must be

func _ready( ):
    if not Global.animation_has_played:
           #do smth

and just for sure, untick “autoplay animation” in animationplayer.

When i go to create the script it asks me to inherit a node.

Also is it just one line that i add to, which is the following:

var animation_has_played = false

Kyle J144 | 2019-03-17 12:27

Yes, just that one line. Basically with this line in Global you make this var available through the whole game. And the var will exists and keep the value until you exit the game.

When i go to create the script it asks me to inherit a node.

That’s ok, it must inherit a node. In script editor just go file new, give it a name and save.

extends Node

var animation_has_played = false

Be aware to avoid redeclare this var again in other scripts.

exe2k | 2019-03-17 16:43