aigamedev/btsk

Parallel behaviour should reset children onInitialize

AtheMathmo opened this issue · 1 comments

Hey!

I've been using this code as a reference and I ran into a small problem. I think that the Parallel behaviour will not reset states correctly for behaviours that terminate (that succeed or fail).

The resetting is left to Behaviour::tick(), which updates the status of the behaviour. But the Parallel behaviour will not tick behaviours that were previously terminated.

One fix is to have Parallel implement onInitialize and reset the children's state there (or tick them, though this wont interact well with the Repeat decorator).


class Parallel : public Behavior {
public:
    // Other members and methods...

    void onInitialize() override {
        // Reset or reinitialize the state of all child behaviors
        for (auto& child : children) {
            if (child->getStatus() == BH_SUCCESS || child->getStatus() == BH_FAILURE) {
                child->reset(); // Reset the child behavior if it has terminated
            }
        }
    }

    Status update() override {
        // Implementation of the parallel behavior's update logic...
    }

    // Other members and methods...
};

class Behavior {
public:
    // Other members and methods...

    virtual void onInitialize() {
        // Default implementation of onInitialize, if any
    }

    void reset() {
        status = BH_INVALID;
        onInitialize();
    }

    // Other members and methods...
};

// Other necessary code...