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. extends Node
  2. class_name StateMachine
  3.  
  4. var state = null
  5. var previous_state = null
  6. var states = {}
  7.  
  8. onready var parent = get_parent()
  9.  
  10. func _physics_process(delta):
  11. if state != null:
  12. _state_logic(delta)
  13. var transition = _get_transition(delta)
  14. if transition != null:
  15. set_state(transition)
  16.  
  17. #Follow 4 functions need to be pasted into new script and applied accordingly; See EnemySM.gd for examples
  18. #State Logic is where you apply logic when parent enters a certain state
  19. func _state_logic(delta):
  20. pass
  21.  
  22. #Get Transition applies when transitions should occur and applies new states
  23. func _get_transition(delta):
  24. return null
  25.  
  26. # Enter State is called when a state is entered, mainly used for animations, sounds, or attacks
  27. func _enter_state(new_state, old_state):
  28. pass
  29.  
  30. # Exit State is called when a state is exited, good for the same usage as enter state
  31. func _exit_state(old_state, new_state):
  32. pass
  33.  
  34. func set_state(new_state):
  35. previous_state = state
  36. state = new_state
  37.  
  38. if previous_state != null:
  39. _exit_state(previous_state, new_state)
  40. if new_state != null:
  41. _enter_state(new_state, previous_state)
  42.  
  43. func add_state(state_name):
  44. states[state_name] = states.size()
Advertisement
RAW Paste Data Copied
Advertisement