Skip to content

Commit

Permalink
Merge pull request #55 from limbonaut/hsm-dispatch-improvement
Browse files Browse the repository at this point in the history
Allow `LimboState::dispatch` to be called from inside the hierarchy.
  • Loading branch information
limbonaut authored Mar 1, 2024
2 parents 1aa64f8 + eaeb57e commit 543f2bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions hsm/limbo_hsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion hsm/limbo_hsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class LimboHSM : public LimboState {
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;
Expand All @@ -62,7 +63,6 @@ class LimboHSM : public LimboState {
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);
Expand Down
6 changes: 5 additions & 1 deletion hsm/limbo_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions hsm/limbo_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LimboState : public Node {
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();
Expand All @@ -67,8 +68,6 @@ class LimboState : public Node {
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; }
Expand All @@ -78,13 +77,14 @@ class LimboState : public Node {
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; }
Expand Down

0 comments on commit 543f2bb

Please sign in to comment.