NoJobEngine 2.0.7
Loading...
Searching...
No Matches
BTNativeRegistry.h
Go to the documentation of this file.
1
6#pragma once
7#include "BTContext.h"
8#include "BTNode.h"
9#include <functional>
10#include <stdexcept>
11#include <string>
12#include <string_view>
13#include <unordered_map>
14#include <utility>
15#include <vector>
16#include <algorithm>
17
18namespace NoJob {
19// Build/register before sharing as shared_ptr<const BTNativeRegistry>.
20// Callbacks must not retain references to a temporary BTContext.
22public:
23 using Action = std::function<BTStatus(BTContext&, float)>;
24 using Condition = std::function<bool(BTContext&)>;
25
26 void RegisterAction(std::string name, Action callback) {
27 if (name.empty() || !callback)
28 throw std::invalid_argument("Action requires a name and callback");
29 if (m_Actions.contains(name))
30 throw std::invalid_argument("Duplicate native action: " + name);
31 m_Actions.emplace(std::move(name), std::move(callback));
32 }
33
34 void RegisterCondition(std::string name, Condition callback) {
35 if (name.empty() || !callback)
36 throw std::invalid_argument("Condition requires a name and callback");
37 if (m_Conditions.contains(name))
38 throw std::invalid_argument("Duplicate native condition: " + name);
39 m_Conditions.emplace(std::move(name), std::move(callback));
40 }
41
42 bool HasAction(std::string_view name) const {
43 return m_Actions.contains(std::string(name));
44 }
45 bool HasCondition(std::string_view name) const {
46 return m_Conditions.contains(std::string(name));
47 }
48
49 // Stable sorted copies for editor dropdowns. No callbacks or DLL pointers escape.
50 std::vector<std::string> ActionNames() const {
51 std::vector<std::string> result;
52 result.reserve(m_Actions.size());
53 for (const auto& [name, callback] : m_Actions) result.push_back(name);
54 std::sort(result.begin(), result.end());
55 return result;
56 }
57 std::vector<std::string> ConditionNames() const {
58 std::vector<std::string> result;
59 result.reserve(m_Conditions.size());
60 for (const auto& [name, callback] : m_Conditions) result.push_back(name);
61 std::sort(result.begin(), result.end());
62 return result;
63 }
64
65 const Action& GetAction(std::string_view name) const {
66 const auto it = m_Actions.find(std::string(name));
67 if (it == m_Actions.end())
68 throw std::out_of_range("Unknown native action: " + std::string(name));
69 return it->second;
70 }
71 const Condition& GetCondition(std::string_view name) const {
72 const auto it = m_Conditions.find(std::string(name));
73 if (it == m_Conditions.end())
74 throw std::out_of_range("Unknown native condition: " + std::string(name));
75 return it->second;
76 }
77private:
78 std::unordered_map<std::string, Action> m_Actions;
79 std::unordered_map<std::string, Condition> m_Conditions;
80};
81} // namespace NoJob
Per-agent behavior tree execution context.
Base interface for behavior tree nodes.
void RegisterCondition(std::string name, Condition callback)
std::function< bool(BTContext &)> Condition
const Condition & GetCondition(std::string_view name) const
bool HasCondition(std::string_view name) const
std::function< BTStatus(BTContext &, float)> Action
std::vector< std::string > ActionNames() const
bool HasAction(std::string_view name) const
std::vector< std::string > ConditionNames() const
const Action & GetAction(std::string_view name) const
void RegisterAction(std::string name, Action callback)
BTStatus
Result of a behavior tree node tick.
Definition BTNode.h:13