How do I check if the player is moving with the is_idle function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Brizzuh

I’m a beginner with Godot, and I’m trying to use an enemy behavior script that I found on youtube to give behavior to my enemy. However, I cannot figure out how to check if the player is not moving with the is_idle function.

extends Node2D

var direction = Vector2()
var target_pos = Vector2()

onready var TweenNode = get_node(“Tween”)
onready var player = get_parent().get_node(“Player”)

enum State {IDLE, PREPARE, ATTACK, REST}
var state = State.IDLE

const REST_TIME = 1.0
var rest_timer = REST_TIME

const PREPARE_TIME = 1.2
var prepare_timer = PREPARE_TIME

func _ready():
set_process(true)

func _process(delta):
# When not resting, LOOK at player
if state in [State.IDLE, State.REST]:
direction = (player.get_position() - get_position().normalized())

Error is here:

if state == State.IDLE and player.is_idle():
	go_to_state(State.PREPARE)
elif state == State.PREPARE:
	prepare_timer -= delta
	if prepare_timer < 0:
		go_to_state(State.ATTACK) 
elif state == State.REST:
	rest_timer -= delta
	if rest_timer < 0:
		go_to_state(State.IDLE)

func go_to_state(new_state):
state = new_state
if new_state == State.REST:
rest_timer = REST_TIME
elif new_state == State.PREPARE:
prepare_timer = PREPARE_TIME
target_pos = player.position
elif new_state == State.ATTACK:
move_to(target_pos)

#Add code here
func is_idle():

func move_to(target_pos):
TweenNode.interpolate_property(self, “position”, position, target_pos, 1,

Tween.TRANS_ELASTIC,
Tween.EASE_OUT, 0)
TweenNode.start()

func on_Tween_tween_completed(object, key):
go_to_state(State.REST)

:bust_in_silhouette: Reply From: Arkinum

Not sure how if there is a better answer, but I would just make a singleton and make a variable that contains a reference to the players node velocity variable or state.

On the players move script I would update the singleton with the players velocity or state. Then just have the enemy access the singleton and use the information there to find out if the player has velocity or state is idle.

I’m kind of a newb programmer so take this advice as is.