34 registry.
RegisterAction(std::move(actionName), [name = std::move(scriptName), method](
BTContext& ctx,
float dt) {
36 auto* scene = owner ? owner->GetEntity().GetScene() :
nullptr;
37 auto* script = scene ?
dynamic_cast<T*
>(scene->FindRuntimeScript(owner->GetEntity().GetHandle(), name)) :
nullptr;
43 std::string scriptName,
bool (T::*method)(
BTContext&)) {
46 auto* scene = owner ? owner->GetEntity().GetScene() :
nullptr;
47 auto* script = scene ?
dynamic_cast<T*
>(scene->FindRuntimeScript(owner->GetEntity().GetHandle(), name)) :
nullptr;
48 return script && (script->*method)(ctx);
71 if (!tree)
throw std::runtime_error(
"BTScriptController: CreateTree returned null");
72 m_Instance = std::make_unique<BTInstance>(std::move(tree));
73 auto& ctx = m_Instance->Context();
75 m_BlackboardPrevious.clear();
76 m_BlackboardChanges.clear();
79 CaptureDebugSnapshot();
84 m_LastStatus = m_Instance->Tick(deltaTime);
85 CaptureDebugSnapshot();
90 if (!m_Instance)
return;
94 m_BlackboardPrevious.clear();
95 m_BlackboardChanges.clear();
96 m_Instance->Context().UnbindScript();
114 void CaptureDebugSnapshot() {
115 if (!m_Instance)
return;
117 next.
LastResult =
static_cast<int>(m_LastStatus);
118 const auto& ctx = m_Instance->Context();
119 std::function<void(
const BTNode&,
int)> visit = [&](
const BTNode& node,
int depth) {
120 const auto status = ctx.LastStatuses().find(node.
GetID());
121 const auto ticks = ctx.TickCounters().find(node.
GetID());
123 status == ctx.LastStatuses().end() ? -1 : status->second,
124 ticks == ctx.TickCounters().end() ? 0 : ticks->second,
125 node.
DebugLabel(), depth, ctx.CurrentStatuses().contains(node.
GetID())});
126 std::vector<const BTNode*> children;
128 for (
const auto* child : children)
if (child) visit(*child, depth + 1);
130 visit(m_Instance->Tree().Root(), 0);
133 const auto entries = ctx.Blackboard().DebugEntries();
134 std::unordered_map<std::string, std::string> now;
135 now.reserve(entries.size());
136 for (
const auto& entry : entries)
137 now.emplace(entry.Key, entry.Value);
138 const auto& runtimeEvents = ctx.Events();
139 const std::uint64_t tick = runtimeEvents.empty() ? m_DebugTick : runtimeEvents.back().Tick;
140 auto record = [&](
const std::string& key,
const std::string& before,
const std::string& after) {
141 m_BlackboardChanges.push_back({tick, 0,
"Blackboard", key +
": " + before +
" -> " + after});
142 constexpr std::size_t limit = 48;
143 if (m_BlackboardChanges.size() > limit)
144 m_BlackboardChanges.erase(m_BlackboardChanges.begin(),
145 m_BlackboardChanges.begin() + (m_BlackboardChanges.size() - limit));
147 for (
const auto& [key, value] : now) {
148 const auto it = m_BlackboardPrevious.find(key);
149 if (it == m_BlackboardPrevious.end()) record(key,
"<unset>", value);
150 else if (it->second != value) record(key, it->second, value);
152 for (
const auto& [key, value] : m_BlackboardPrevious)
153 if (!now.contains(key)) record(key, value,
"<removed>");
154 m_BlackboardPrevious = std::move(now);
156 for (
const auto& event : ctx.Events())
157 next.
Events.push_back({
event.Tick,
event.Node,
event.Kind,
event.Detail});
160 next.
Events.insert(next.
Events.end(), m_BlackboardChanges.begin(), m_BlackboardChanges.end());
161 std::stable_sort(next.
Events.begin(), next.
Events.end(),
162 [](
const BTDebugSnapshot::Event& a,
const BTDebugSnapshot::Event& b) {
163 return a.Tick < b.Tick;
167 if (
const auto* target = ctx.Blackboard().TryGet<std::uint32_t>(
"TargetHandle"))
169 if (
const auto* position = ctx.Blackboard().TryGet<glm::vec3>(
"TargetPosition"))
171 m_DebugSnapshot = std::move(next);
173 std::uint64_t m_DebugTick = 0;
174 std::unordered_map<std::string, std::string> m_BlackboardPrevious;
175 std::vector<BTDebugSnapshot::Event> m_BlackboardChanges;
176 BTDebugSnapshot m_DebugSnapshot;
177 std::unique_ptr<BTInstance> m_Instance;