NoJobEngine
2.0.7
Toggle main menu visibility
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
18
namespace
NoJob
{
19
// Build/register before sharing as shared_ptr<const BTNativeRegistry>.
20
// Callbacks must not retain references to a temporary BTContext.
21
class
BTNativeRegistry
{
22
public
:
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
}
77
private
:
78
std::unordered_map<std::string, Action> m_Actions;
79
std::unordered_map<std::string, Condition> m_Conditions;
80
};
81
}
// namespace NoJob
BTContext.h
Per-agent behavior tree execution context.
BTNode.h
Base interface for behavior tree nodes.
NoJob::BTContext
Definition
BTContext.h:24
NoJob::BTNativeRegistry
Definition
BTNativeRegistry.h:21
NoJob::BTNativeRegistry::RegisterCondition
void RegisterCondition(std::string name, Condition callback)
Definition
BTNativeRegistry.h:34
NoJob::BTNativeRegistry::Condition
std::function< bool(BTContext &)> Condition
Definition
BTNativeRegistry.h:24
NoJob::BTNativeRegistry::GetCondition
const Condition & GetCondition(std::string_view name) const
Definition
BTNativeRegistry.h:71
NoJob::BTNativeRegistry::HasCondition
bool HasCondition(std::string_view name) const
Definition
BTNativeRegistry.h:45
NoJob::BTNativeRegistry::Action
std::function< BTStatus(BTContext &, float)> Action
Definition
BTNativeRegistry.h:23
NoJob::BTNativeRegistry::ActionNames
std::vector< std::string > ActionNames() const
Definition
BTNativeRegistry.h:50
NoJob::BTNativeRegistry::HasAction
bool HasAction(std::string_view name) const
Definition
BTNativeRegistry.h:42
NoJob::BTNativeRegistry::ConditionNames
std::vector< std::string > ConditionNames() const
Definition
BTNativeRegistry.h:57
NoJob::BTNativeRegistry::GetAction
const Action & GetAction(std::string_view name) const
Definition
BTNativeRegistry.h:65
NoJob::BTNativeRegistry::RegisterAction
void RegisterAction(std::string name, Action callback)
Definition
BTNativeRegistry.h:26
NoJob
Definition
BTActionNode.h:13
NoJob::BTStatus
BTStatus
Result of a behavior tree node tick.
Definition
BTNode.h:13
Engine
AI
BehaviorTree
BTNativeRegistry.h
Generated by
1.18.0