PH4z
PH4z

Reputation: 11

Global accessor State Machine Handler move_and_slide problem

im currently having a problem with a statemachine that i am coding, the problem that is occurring is that when i add move_and_slide to the state of movement, the if condition at the last of the code block is getting the error:

the method " move_and_slide isn't declared in the current class

    extends Node2D

    class_name State

    var state_machine = null
    var player_velocity = Vector2()
    var player_speed = 200

    enum playerStates{IDLE, WALK, RUN, USE, SHOOT}
    enum ItemStates{ITEM_ON_GROUND, IS_EQUIPED}

   func _ready():
        if Global.player_isIdle == State.state_machine == playerStates.IDLE:
            pass
        if Global.player_isWalking == State.state_machine == playerStates.WALK:
            player_velocity = Global.player_velocity
    func handle_input(_event: InputEvent):
        if player_velocity != player_velocity.ZERO:
            if Global.player_isWalking == State.state_machine == playerStates.WALK:
                pass
            if Global.player_isRunning == State.state_machine == playerStates.RUN:
               if Input.is_action_pressed("sprint"):
                    player_speed = 300
            Global.player_velocity = Global.player_velocity.normalized() * player_speed
            Global.player_velocity = move_and_slide(Global.player_velocity)
    func _physics_process(_delta):
        pass

Upvotes: 1

Views: 132

Answers (2)

NEXE
NEXE

Reputation: 49

extends Node2D
class_name State

your class is extending Node2D but move_and_slide is from KinematicBody2D Try:

extends KinematicBody2D
class_name State

and also use move_and_slide in _physics_update function. Also, don't forget to change Player Node to KinematicBody2D

and if your Class is a child of Player node then do like this:

extends Node2D

class_name State

    var state_machine = null
    var player_velocity = Vector2()
    var player_speed = 200

    var parent = get_parent() <<<<<<<<<<<<<<<< 

    enum playerStates{IDLE, WALK, RUN, USE, SHOOT}
    enum ItemStates{ITEM_ON_GROUND, IS_EQUIPED}

   func _ready():
        if Global.player_isIdle == State.state_machine == playerStates.IDLE:
            pass
        if Global.player_isWalking == State.state_machine == playerStates.WALK:
            player_velocity = Global.player_velocity
    
func handle_input(_event: InputEvent):
        if player_velocity != player_velocity.ZERO:
            if Global.player_isWalking == State.state_machine == playerStates.WALK:
                pass
            if Global.player_isRunning == State.state_machine == playerStates.RUN:
               if Input.is_action_pressed("sprint"):
                    player_speed = 300
            Global.player_velocity = Global.player_velocity.normalized() * player_speed
            
    
func _physics_process(_delta):
    Global.player_velocity = parent.move_and_slide(Global.player_velocity) <<<<<<<<<<

Upvotes: 1

Theraot
Theraot

Reputation: 40295

There is a method called move_and_slide but it belongs to KinematicBody2D (or its 3D counterpart).

However, you are writing code in a Node2D. As the error message tell you, there isn't a move_and_slide for Node2D.

Since there isn't a move_and_slide in Node2D, you could write your own method called move_and_slide that does whatever you want. However, I believe you want to use the one form KinematicBody2D.

So change the Node from Node2D to KinematicBody2D (and update the script, at the top, where it says extends Node2D to say KinematicBody2D). In the Godot editor you can find an option to change the type of a Node called "Change Type", it is in the contextual menu of the Node in Scene.

Upvotes: 1

Related Questions