Merge pull request #55 from limbonaut/hsm-dispatch-improvement

Allow `LimboState::dispatch` to be called from inside the hierarchy.
This commit is contained in:
Serhii Snitsaruk 2024-03-01 11:56:38 +01:00 committed by GitHub
commit 543f2bb32c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 9 deletions

View File

@ -127,17 +127,17 @@ void LimboHSM::set_initial_state(LimboState *p_state) {
initial_state = Object::cast_to<LimboState>(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) {

View File

@ -43,6 +43,7 @@ protected:
void _notification(int p_what);
virtual void _initialize(Node *p_agent, const Ref<Blackboard> &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<Blackboard> &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);

View File

@ -80,7 +80,7 @@ void LimboState::_initialize(Node *p_agent, const Ref<Blackboard> &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);

View File

@ -54,6 +54,7 @@ protected:
void _notification(int p_what);
virtual void _initialize(Node *p_agent, const Ref<Blackboard> &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<BlackboardPlan> p_plan) { blackboard_plan = p_plan; }
Ref<BlackboardPlan> 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; }