Merge pull request #242 from limbonaut/fix-hsm-reparenting-saga-episode-three
Fix re-parenting an agent interrupts its state machine
This commit is contained in:
commit
f90c48eb81
|
@ -265,22 +265,46 @@ void LimboHSM::_validate_property(PropertyInfo &p_property) const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LimboHSM::_exit_if_not_inside_tree() {
|
||||||
|
if (is_active() && !is_inside_tree()) {
|
||||||
|
_exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LimboHSM::_notification(int p_what) {
|
void LimboHSM::_notification(int p_what) {
|
||||||
switch (p_what) {
|
switch (p_what) {
|
||||||
case NOTIFICATION_POST_ENTER_TREE: {
|
case NOTIFICATION_POST_ENTER_TREE: {
|
||||||
if (was_active && is_root()) {
|
if (was_active && is_root()) {
|
||||||
// Re-activate the root HSM if it was previously active.
|
// Re-activate the root HSM if it was previously active.
|
||||||
// Typically, this happens when the node is re-entered scene repeatedly (e.g., re-parenting, pooling).
|
// Typically, this happens when the node is re-entered scene repeatedly (such as with object pooling).
|
||||||
set_active(true);
|
set_active(true);
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case NOTIFICATION_EXIT_TREE: {
|
case NOTIFICATION_EXIT_TREE: {
|
||||||
if (is_root()) {
|
if (is_root()) {
|
||||||
// Remember active status for re-parenting and exit state machine
|
// Exit the state machine if the root HSM is no longer in the scene tree (except when being reparented).
|
||||||
// to release resources and signal connections if active.
|
// This ensures that resources and signal connections are released if active.
|
||||||
was_active = active;
|
was_active = is_active();
|
||||||
if (is_active()) {
|
if (is_active()) {
|
||||||
_exit();
|
// Check if the HSM node is being deleted.
|
||||||
|
bool is_being_deleted = false;
|
||||||
|
Node *node = this;
|
||||||
|
while (node) {
|
||||||
|
if (node->is_queued_for_deletion()) {
|
||||||
|
is_being_deleted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
node = node->get_parent();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_being_deleted) {
|
||||||
|
// Exit the state machine immediately if the HSM is being deleted.
|
||||||
|
_exit();
|
||||||
|
} else {
|
||||||
|
// Use deferred mode to prevent exiting during Node re-parenting.
|
||||||
|
// This allows the HSM to remain active when it (or one of its parents) is reparented.
|
||||||
|
callable_mp(this, &LimboHSM::_exit_if_not_inside_tree).call_deferred();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
|
@ -63,6 +63,7 @@ private:
|
||||||
HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions;
|
HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions;
|
||||||
|
|
||||||
void _get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const;
|
void _get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const;
|
||||||
|
void _exit_if_not_inside_tree();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
Loading…
Reference in New Issue