1
\$\begingroup\$

This is my code:

extends CharacterBody2D


@export var SPEED = 300.0
@export var maxHP = 100
@export var curHP = 100

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var state_machine = $AnimationTree.get("parameters/playback")

func _ready():
    pass


func _physics_process(delta):
    if not is_on_floor():
        velocity.y += gravity * delta
    
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
    
    if velocity.length() > 0:
        state_machine.travel("run")
    else:
        state_machine.travel("idle")
        
    
    if Input.is_action_pressed("ui_left"):
        $Sprite2D.set("flip_h", true)
    if Input.is_action_pressed("ui_right"):
        $Sprite2D.set("flip_h", false)
    
    
    move_and_slide()
    
    if Input.is_action_just_pressed("attack"):
        _shoot()

func _losehp():
    curHP -= 10

func _die():
    if curHP == 0:
        pass

func _shoot():
    if Input.is_action_just_pressed("attack"):
        pass

I get errors on the line:

var state_machine = $AnimationTree.get("parameters/playback")

These are the errors:

enter image description here

CharacterBody2D.gd:9 @ @implicit_new(): Node not found: "AnimationTree" (relative to "CharacterBody2d").

enter image description here

Cannot call method get on a null value.

the tree: enter image description here

please help:(

\$\endgroup\$
5
  • \$\begingroup\$ Please use formatted text for error messages rather than screenshots/images. Right clicking an error brings up a context menu with the option to copy the error & the debugger has a square looking icon for copying. \$\endgroup\$
    – Pikalek
    Commented Dec 11, 2023 at 1:29
  • 1
    \$\begingroup\$ As for the error: What does the scene tree look like? (note: the scene tree is one of the things in Godot where it makes sense to include a screenshot/image as it includes a lot of icons). Based on where you making the function call, you probably need to be using the @onready annotation, but we can't know for sure because you haven't included any information / description about your scene(s) or what you're trying to do. \$\endgroup\$
    – Pikalek
    Commented Dec 11, 2023 at 1:50
  • \$\begingroup\$ @Pikalek there's another error when i changed the script to theraot's . here's the error: W 0:00:00:0822 _transition_to_next_recursive: AnimationNodeStateMachinePlayback: parameters/playback aborts the transition by detecting one or more looped transitions in the same frame to prevent to infinity loop. You may need to check the transition settings. <C++ Source> scene/animation/animation_node_state_machine.cpp:928 @ _transition_to_next_recursive() \$\endgroup\$ Commented Dec 11, 2023 at 11:39
  • \$\begingroup\$ but ill fix it. \$\endgroup\$ Commented Dec 11, 2023 at 11:47
  • \$\begingroup\$ At a glance, I don't see the what causes the error you posted in the above comment... Yet, searching it took me here: github.com/godotengine/godot/issues/82935 - see if it helps. \$\endgroup\$
    – Theraot
    Commented Dec 11, 2023 at 19:10

1 Answer 1

2
\$\begingroup\$

In this line:

var state_machine = $AnimationTree.get("parameters/playback")

The expression $AnimationTree is equivalent to get_node("AnimationTree"), which is trying to access the scene tree.

However, the aforementioned line will run before the CharacterBody2D is in the scene tree. To solve this, you can use @onready:

@onready var state_machine = $AnimationTree.get("parameters/playback")

Then, assuming the the AnimationTree is there (you didn't share how the scene tree looks like in your project), it should work.

\$\endgroup\$
1
  • \$\begingroup\$ it worked! there are no errors but the animations seem not to play for some reason but i think i coded it wrong. but thanks! \$\endgroup\$ Commented Dec 11, 2023 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .