Advertisement
Guest User

Untitled

a guest
Apr 8th, 2022
10
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Extend the State Machine script
  2. extends "res://StateMachine.gd"
  3.  
  4. # Ready function required to apply first state as well as add state types applicable to specific node
  5. func _ready():
  6. # Add new state types for specific node as such
  7. add_state("sentry")
  8. add_state("pursue")
  9. # Call deferred will allow setting the default state during idle time, so the first state will not be null
  10. call_deferred("set_state", states.sentry)
  11.  
  12. #State Logic is where you apply logic when parent enters a certain state, this is where we initiate parent AI functions
  13. func _state_logic(delta):
  14. # Initiate functions in parent script as such
  15. if state == states.sentry:
  16. parent._sentry()
  17. if state == states.pursue:
  18. parent._navigate()
  19.  
  20. #Get Transition applies when transitions should occur and applies new states
  21. func _get_transition(delta):
  22. # Match statements are like switch statements, which is the most effective method in this situation
  23. match state:
  24. states.sentry:
  25. if parent.in_attack_range:
  26. return states.pursue
  27. states.pursue:
  28. if !parent.in_attack_range:
  29. return states.sentry
  30.  
  31. # Enter State is called when a state is entered, mainly used for animations, sounds, or attacks
  32. # This example does not have any animations, replace pass statements with parent.animation_player.player("animation")
  33. # Once again match (switch) statements are best for this
  34. func _enter_state(new_state, old_state):
  35. match new_state:
  36. states.sentry:
  37. print("Entered sentry")
  38. parent._clear_path()
  39. states.pursue:
  40. print("Entered pursuit")
  41. parent._generate_path()
  42.  
  43. # Exit State is called when a state is exited, good for the same usage as enter state
  44. func _exit_state(old_state, new_state):
  45. pass
Advertisement
RAW Paste Data Copied
Advertisement