0 votes

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 = getnode("Tween")
onready var player = get
parent().get_node("Player")

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

const RESTTIME = 1.0
var rest
timer = REST_TIME

const PREPARETIME = 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 gotostate(newstate):
state = new
state
if newstate == State.REST:
rest
timer = RESTTIME
elif new
state == State.PREPARE:
preparetimer = PREPARETIME
targetpos = player.position
elif new
state == State.ATTACK:
moveto(targetpos)

Add code here

func is_idle():

func moveto(targetpos):
TweenNode.interpolateproperty(self, "position", position, targetpos, 1,

Tween.TRANSELASTIC,
Tween.EASE
OUT, 0)
TweenNode.start()

func onTweentweencompleted(object, key):
go
to_state(State.REST)

Godot version Godot 3.4 stable (Basically the latest version with support for C#)
in Engine by (12 points)

1 Answer

0 votes

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.

by (105 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.