Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | vicguedez | |
Old Version | Published before Godot 3 was released. |
Hello guys, I’m new to Godot and game developing in general. So I’m trying to integrate state machine pattern into my player, by now the script looks like this:
#Player
extends KinematicBody2D
const STATE_IDLE = 0
onready var State = PlayerIdleState.new(self)
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
State.update(delta)
func set_state(new_state):
State.exit()
if new_state == STATE_IDLE:
State = PlayerIdleState.new(self)
func get_state():
if State extends PlayerIdleState:
return STATE_IDLE
# ------------------------------------------------------------------
class PlayerState:
var Player
func move_player(speed, direction):
Player.move(direction.normalized() * speed)
# ------------------------------------------------------------------
class PlayerIdleState extends PlayerState:
var speed = 2
var speed_diagonal_ratio = 1.4
var current_animation = ""
func movePlayer():
var motion_speed = speed
var moving_up = Input.is_action_pressed("player_move_up")
var moving_right = Input.is_action_pressed("player_move_right")
var moving_down = Input.is_action_pressed("player_move_down")
var moving_left = Input.is_action_pressed("player_move_left")
if ((moving_left or moving_right) and (moving_down or moving_up)):
motion_speed = speed / speed_diagonal_ratio
if moving_up :
.move_player(motion_speed, Vector2(0, -1))
elif moving_right:
.move_player(motion_speed, Vector2(1, 0))
elif moving_down:
.move_player(motion_speed, Vector2(0, 1))
Player.get_node("AnimationPlayer").play("WalkDown")
elif moving_left:
move_player(motion_speed, Vector2(-1, 0))
func _init(player):
Player = player
func update(delta):
movePlayer()
func input(event):
pass
func exit():
pass
I’m having a problem, If I tap my move down key (“S”) it goes down a bit and then plays the animation walking down. If I keep pressed the move down key for lets say 3 seconds, it moves down all that time but doesnt play the animation until it stops moving. Why is that?
Furthermore how can I improve this script? I want to have more states, its a survival game so I will have states like fighting, searching through stuff and etc…