Godot Version
4.3.stable
Question
I am having an issue where my “run” animation is only playing on one of three instantiated scenes.
A lot of what I am doing features instantiation, because I am trying to create a game with some procedural generation.
In the level scene, I instantiate an instance of the player scene, which has a character handler which instantiates three copies of the character scene.
Player Scene Tree:
Character Handler Instantiating Three Characters Code:
#character preloads
const PHCHAR = preload("res://entities/player/characters/placeholder/ph_character.tscn")
#character variables
var char1: Node2D
var char2: Node2D
var char3: Node2D
#position variables
@onready var posA = $ActivePosition
@onready var posR = $RightPosition
@onready var posL = $LeftPosition
func _ready():
char1 = PHCHAR.instantiate()
char1.player = $".."
char1.global_position = posA.global_position
char1.printstring = "1"
add_child(char1)
char2 = PHCHAR.instantiate()
char2.player = $".."
char2.global_position = posR.global_position
char2.printstring = "2"
add_child(char2)
char3 = PHCHAR.instantiate()
char3.player = $".."
char3.global_position = posL.global_position
char3.printstring = "3"
add_child(char3)
Each character scenes has a sprite for the lower body (with an animation player child) and a sprite for the upper body (with an animation player child).
Character Scene Tree:
In the _process function of the character scene, it checks to see if the player (defined upon instantiation) is moving, and, if the player is moving, the character should play the “active” animation. If the player is not moving, the character should play the “idle” animation. In the _process function there is also some debug code that should print out the printstring assigned in the instantiation code shown above when the player is moving. That aspect works (it prints out 1, 2, and 3 in the output) but the animation only plays in the first instantiated character.
_process Function of Character Script:
func _process(_delta):
if player.velocity.length() < 10:
lb_animation.play("LB_idle")
else:
if lb_animation.current_animation != "LB_ph_active":
print(printstring)
lb_animation.play("LB_ph_active")
This only works in the first instantiated character scene. If I comment out the first instantiated character, the animation system now works in the second instantiated character (which is now the first, since the first no longer instantiates).
The weird thing is that the characters also have an “action handler” which plays an animation for the upper body after being triggered by a timer/function. This animation system works in all three instantiated characters.
I must be doing something wrong, so I am wondering if you all had any ideas.
(Also, I am new to this forum, so please let me know if I have posted this in error.)
Thank you.