0

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")
DALEmuh
  • 51
  • 1
  • 5
  • Forgot to add that I do have an 'anim_Play' variable equal to null. – DALEmuh Oct 21 '19 at 04:51
  • 1
    "does not work" is not helpful; there are many ways something doesn't work: It plays the wrong animation, it throws an exception, nothing happens, it plays half the animation, etc. Define your intent with the code and actual behaviour. – Fredrik Schön Oct 21 '19 at 13:39
  • When I right click it crashes the game and gives the error: "Invalid call. Nonexistent function 'play' in base 'Array'. – DALEmuh Oct 21 '19 at 17:05
  • I recommend checking the node structure created when game is running, then traverse the tree appropriately in code to find "AnimationPlayer". Seems like what you are getting back from `get_tree().get_nodes_in_group("AnimationPlayer")` is not the actual `AnimationPlayer` since it can't find `play()`. – Dayan Oct 25 '19 at 13:20

2 Answers2

0

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")
svarog
  • 9,477
  • 4
  • 61
  • 77
  • I still get the error "Node not found: AnimationPlayer" and even get a new error: "Attempt to call function 'play' in base 'null instance' on a null instance." – DALEmuh Oct 22 '19 at 17:19
  • did you rename it? you can use the editor to look it up, e.g. `var anim = $path/to/player` – svarog Oct 23 '19 at 09:51
0

Response to your question

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.

Recommendation

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!

hola
  • 3,150
  • 13
  • 21