From eaeb57e2d76a39f10daf009fe13426fad0f9d81f Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Fri, 1 Mar 2024 11:02:32 +0100 Subject: [PATCH] Allow `LimboState::dispatch` to be called from inside the hierarchy. Improves usability and removes requirement to call this method on the root state (see #49). --- hsm/limbo_hsm.cpp | 6 +++--- hsm/limbo_hsm.h | 2 +- hsm/limbo_state.cpp | 6 +++++- hsm/limbo_state.h | 8 ++++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/hsm/limbo_hsm.cpp b/hsm/limbo_hsm.cpp index 37a190f..54e95f2 100644 --- a/hsm/limbo_hsm.cpp +++ b/hsm/limbo_hsm.cpp @@ -127,17 +127,17 @@ void LimboHSM::set_initial_state(LimboState *p_state) { initial_state = Object::cast_to(p_state); } -bool LimboHSM::dispatch(const String &p_event, const Variant &p_cargo) { +bool LimboHSM::_dispatch(const String &p_event, const Variant &p_cargo) { ERR_FAIL_COND_V(p_event.is_empty(), false); bool event_consumed = false; if (active_state) { - event_consumed = active_state->dispatch(p_event, p_cargo); + event_consumed = active_state->_dispatch(p_event, p_cargo); } if (!event_consumed) { - event_consumed = LimboState::dispatch(p_event, p_cargo); + event_consumed = LimboState::_dispatch(p_event, p_cargo); } if (!event_consumed && active_state) { diff --git a/hsm/limbo_hsm.h b/hsm/limbo_hsm.h index 8be57a6..f92857c 100644 --- a/hsm/limbo_hsm.h +++ b/hsm/limbo_hsm.h @@ -43,6 +43,7 @@ protected: void _notification(int p_what); virtual void _initialize(Node *p_agent, const Ref &p_blackboard) override; + virtual bool _dispatch(const String &p_event, const Variant &p_cargo = Variant()) override; virtual void _enter() override; virtual void _exit() override; @@ -62,7 +63,6 @@ public: LimboState *get_initial_state() const { return initial_state; } virtual void initialize(Node *p_agent, const Ref &p_parent_scope = nullptr); - virtual bool dispatch(const String &p_event, const Variant &p_cargo = Variant()) override; void update(double p_delta); void add_transition(LimboState *p_from_state, LimboState *p_to_state, const String &p_event); diff --git a/hsm/limbo_state.cpp b/hsm/limbo_state.cpp index bf75f49..6e00396 100644 --- a/hsm/limbo_state.cpp +++ b/hsm/limbo_state.cpp @@ -80,7 +80,7 @@ void LimboState::_initialize(Node *p_agent, const Ref &p_blackboard) _setup(); } -bool LimboState::dispatch(const String &p_event, const Variant &p_cargo) { +bool LimboState::_dispatch(const String &p_event, const Variant &p_cargo) { ERR_FAIL_COND_V(p_event.is_empty(), false); if (handlers.size() > 0 && handlers.has(p_event)) { Variant ret; @@ -126,6 +126,10 @@ void LimboState::add_event_handler(const String &p_event, const Callable &p_hand handlers.insert(p_event, p_handler); } +bool LimboState::dispatch(const String &p_event, const Variant &p_cargo) { + return get_root()->_dispatch(p_event, p_cargo); +} + LimboState *LimboState::call_on_enter(const Callable &p_callable) { ERR_FAIL_COND_V(!p_callable.is_valid(), this); connect(LimboStringNames::get_singleton()->entered, p_callable); diff --git a/hsm/limbo_state.h b/hsm/limbo_state.h index 5f337a1..291be8c 100644 --- a/hsm/limbo_state.h +++ b/hsm/limbo_state.h @@ -54,6 +54,7 @@ protected: void _notification(int p_what); virtual void _initialize(Node *p_agent, const Ref &p_blackboard); + virtual bool _dispatch(const String &p_event, const Variant &p_cargo = Variant()); virtual void _setup(); virtual void _enter(); @@ -67,8 +68,6 @@ protected: GDVIRTUAL1(_update, double); #endif // LIMBOAI_MODULE - void add_event_handler(const String &p_event, const Callable &p_handler); - public: void set_blackboard_plan(const Ref p_plan) { blackboard_plan = p_plan; } Ref get_blackboard_plan() const { return blackboard_plan; } @@ -78,13 +77,14 @@ public: Node *get_agent() const { return agent; } void set_agent(Node *p_agent) { agent = p_agent; } - virtual bool dispatch(const String &p_event, const Variant &p_cargo = Variant()); - LimboState *named(String p_name); LimboState *call_on_enter(const Callable &p_callable); LimboState *call_on_exit(const Callable &p_callable); LimboState *call_on_update(const Callable &p_callable); + void add_event_handler(const String &p_event, const Callable &p_handler); + bool dispatch(const String &p_event, const Variant &p_cargo = Variant()); + _FORCE_INLINE_ String event_finished() const { return LW_NAME(EVENT_FINISHED); } LimboState *get_root() const; bool is_active() const { return active; }