Reputation: 51
Any insight onto why this code doesn't work?
When I right click, the game crashes and gives the error: "Invalid call. Nonexistent function 'play' in base 'Array'".
func _ready():
anim_Play = get_tree().get_nodes_in_group("AnimationPlayer")
func_input(event):
if Input.is_action_pressed("aim"):
anim_Play.play("AimSights")
Upvotes: 0
Views: 919
Reputation: 3500
get_nodes_in_group(group)
returns an Array
of nodes that are both in the SceneTree
and in group group
.
Let's say there is one AnimationPlayer node in the group "AnimationPlayer". We'll fetch it like:
var anim_player = get_tree().get_nodes_in_group("AnimationPlayer")[0]
Notice the [0]
. That is called an accessor. We access the array at element 0
. Now, we can call play:
anim_player.play("AimSights")
Do note: it is an error to access a non-existent element of an array.
This seems like an inappropriate use of groups. I recommend you use a node path, like svarog suggested, if the animation player is in the same scene as the script.
Additionally, it will help to read or google about some fundamental programming concepts: specifically Objects and Arrays.
Lastly, read over the scenes and nodes page from Godot's documentation: https://docs.godotengine.org/en/3.1/getting_started/step_by_step/scenes_and_nodes.html
The whole getting started guide on the Godot's documentation is an invaluable resource for learning Godot. It will help you greatly and it's not too long of a read.
Good luck!
Upvotes: 0
Reputation: 9837
I guess from your code that you are trying to get a reference to your AnimationPlayer
node, it fails and you get an Array instead.
It happens because you are using get_nodes_in_group
(which returns an Array of nodes in a group), instead of get_node
, which returns a node.
Invalid call. Nonexistent function 'play' in base 'Array
Means your are trying the call the play
method (found in AnimationPlayer) from an Array object, that does not exist.
You would get AnimationPlayer
like
var anim_Play = get_node("./path/to/your/AnimationPlayer")
Upvotes: 0