#Need help with my enemy

1 messages · Page 1 of 1 (latest)

fallow pier
#

Okay so like
My problem is that:

  1. The enemy does not attack when it is in range
  2. Weirdly, the enemy DOES attack, but only sometimes and very randomly
  3. The enemy also attacks while there is a exact distance between ???? Like wtf lmao
  4. The enemy gets back up after death state and continues to change states, even though i don't even have an update_death_state function to change the state.

base enemy is my parent and simple_enemy inherits from the base_enemy

raw sigil
#

One problem is the state switch isn't fully comitted too, the run animation can play afterwards and whatever super call. It should be either the last statement or an early return.

    if player_distance <= attack_threshold:
        enter_state(ENEMY_STATES.ATTACK)
        return
raw sigil
# fallow pier Okay so like My problem is that: 1) The enemy does not attack when it is in ra...

And in enter_attack_state() the change there also isn't fully committed, so it'll await whatever animation is playing and force a movement state. Which will then re-enter attack, which will force a movement and pile up in a big loop

if attack == 0:
    enter_state(ENEMY_STATES.MOVEMENT)
    return # Without this, v===== it's gonna do that too
...
await anim_player.animation_finished
enter_state(ENEMY_STATES.MOVEMENT)
fallow pier
#

Holy shit that worked. The enemy is actually attacking now. Thanks so much 😭

There's still the problem of the death state not staying dead, the enemy keeps getting up lol

fallow pier
raw sigil
#

Await puts the method on pause until a condition (animation_finished) happens. If something happens since that time, you'd better check otherwise it's gonna continue with what it was doing!

await anim_player.animation_finished
if _current_state != ENEMY_STATES_MOVEMENT:
  print("oops")
  return
fallow pier
#

I think the problem might be in co-routines, adding await seems to have messed with a lot of stuff
was looking into more conventional methods of FSM's and even there I got the co-routine error until I freed the enemy from the queue

fallow pier
raw sigil
# fallow pier Yeahhh, that's what I thought. Ugh, so annoying. Ig that just means more work fo...

There are a number of existing plugins for state machines (I use
https://derkork.github.io/godot-statecharts/).

If you still want to roll your own, consider the state as an object design:

class_name MyState


func _on_enter():
  pass

func _on_exit():
  pass

func _on_process(delta: float):
 pass

...
var _current_state: MyState

func change_state(state: MyState):
  if _current_state != state:
    _current_state._on_exit()
    _current_state = state
    _current_state._on_enter()

func _process(delta: float):
  _current_state._on_process(delta)