Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var states = {}
- var state # current state
- func _ready():
- states["one"] = StateOne.new()
- states["two"] = StateTwo.new()
- for s in states:
- states[s].connect("change_state_plz", self, "change_state")
- # set the initial state
- state = states["one"]
- set_process(true)
- func _process(delta):
- state._process(delta)
- func change_state(next_state):
- state = states[next_state]
- # maybe call a `unload` and `load` function on the states to show them that they are active now
- class StateOne:
- signal change_state_plz(next_state)
- func _process(delta):
- print("DD")
- pass
- class StateTwo:
- signal change_state_plz(next_state)
- func _process(delta):
- print("qq")
- pass
- Not tested
- but that's how I'd do it
- You can then do emit_signal("name of next state")anywhere in a state to switch to a different state
- since you can swap out state the call state._process(delta) will always call the function in the currently active state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement