How to only allow animation to play only once

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

Hey,

I have set up a basic animation, where the animation will automatically move the camera and load a new scene once the animation has finished, but when the player goes back into the same scene then the animation plays again. Where as when the player goes back into the same scene, then I don’t want the animation to play at all.

extends Camera2D

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

func _ready( ):
    # Connects the AnimationPlayer with the signal "animation_finished" to the camera ('self' here)
    anim.connect("animation_finished", self, "_on_AnimationPlayer_animation_finished")

func _on_AnimationPlayer_animation_finished(animation):
    # Check the name of the animation, which is finished.
    # In your case the camera moving animation..
    if animation == "camera_auto_move":
        get_tree().change_scene("scenes/missions/prologue/prologue-hallway.tscn")

Edited to format your code (use code region tool, or indent the code block)

Where are you playing that animation in the first place? Perhaps you should simply not play it.

Zylann | 2019-03-11 13:46

The animation is playing on the very first scene

Kyle J144 | 2019-03-11 14:25

Yes but what makes it play?
If you are doing play(anim) from a script, perhaps you could add an if not Globals.first_scene_played_already so that it plays only the first time?

Zylann | 2019-03-11 18:14

The code is above, so where woul I add it :confused:

Kyle J144 | 2019-03-11 19:35

Is the current animation set to [stop] in your animation player? If you didnt play anything, perhaps it’s because it is set as current.

Zylann | 2019-03-11 20:02

yeah the animation is set to stop as shown in the image supplied

Animation

Kyle J144 | 2019-03-11 21:07

Sooo… it’s set to [stop], and your code doesn’t call play(), so currently it never plays, actually.

Anyways, if you use change_scene() and you don’t want the animation to play the next time the player goes back to this scene, you could save a boolean in an auto-load: Singletons (AutoLoad) — Godot Engine (3.0) documentation in English

Create a singleton called “Globals” for example, and put this inside:

extends Node

var animation_has_played = false

And in your scene, do something like:

# Put this wherever you start the animation
if not Globals.animation_has_played:
	$AnimationPlayer.play("your_animation")
	Globals.animation_has_played = true

Zylann | 2019-03-11 23:28

I get the following error:

0:00:00:0600 - Condition ’ _debug_parse_err_line >= 0 ’ is true. returned: __null

Type:Error
Description:
Time: 0:00:00:0600
C Error: Condition ’ _debug_parse_err_line >= 0 ’ is true. returned: __null
C Source: modules/gdscript/gdscript_editor.cpp:287
C Function: debug_get_stack_level_instance

and this is my code:

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 Globals.animation_has_played:
	$AnimationPlayer.play("your_animation")
	Globals.animation_has_played = true
	# Connects the AnimationPlayer with the signal "animation_finished" to the camera ('self' here)
	anim.connect("animation_finished", self, "_on_AnimationPlayer_animation_finished")

func _on_AnimationPlayer_animation_finished(animation):
# Check the name of the animation, which is finished.
# In your case the camera moving animation..
if animation == "camera_auto_move":
    get_tree().change_scene("scenes/missions/prologue/prologue-hallway.tscn")

Kyle J144 | 2019-03-12 00:38

Check the editor’s debugger. Did you name the singleton correctly? In Godot 3.0.6 there is such an error in the system console if you name it incorrectly (i.e maybe you named it globals but wrote it Globals in the script), but the editor should tell you what’s wrong.

Zylann | 2019-03-12 01:54

On the debugger it has got the following:

Parser Error: Identifier not found: Globals

Kyle J144 | 2019-03-12 13:07

That means you either mispelled the autoload, or you didnt add it in project settings.

Zylann | 2019-03-13 22:31