NoJobEngine 2.0.7
Loading...
Searching...
No Matches
BTScriptExample.h
Go to the documentation of this file.
1
6#pragma once
13
14namespace NoJob {
15// Example integration: assign "BTExampleMover" as a NativeScriptComponent script.
16// Uses only TransformComponent; replace with NavAgent/Perception callbacks later.
17class BTExampleMover final : public BTScriptController {
18protected:
19 std::shared_ptr<const BTTree> CreateTree() override {
20 static const std::shared_ptr<const BTTree> tree = [] {
21 auto registry = std::make_shared<BTNativeRegistry>();
22 registry->RegisterCondition("CanMove", [](BTContext& ctx) {
23 auto* script = BTGetScript(ctx);
24 return script && script->HasComponent<TransformComponent>();
25 });
26 registry->RegisterAction("MoveForward", [](BTContext& ctx, float dt) {
27 auto* script = BTGetScript(ctx);
28 if (!script || !script->HasComponent<TransformComponent>())
29 return BTStatus::Failure;
30 auto& transform = script->GetComponent<TransformComponent>();
31 transform.Position.x += dt;
32 ctx.Blackboard().Set("Elapsed", ctx.Blackboard().GetOr<float>("Elapsed", 0.f) + dt);
33 return BTStatus::Running;
34 });
35 auto sequence = std::make_unique<BTSequence>(1);
36 sequence->AddChild(std::make_unique<BTConditionNode>(2, registry, "CanMove"));
37 sequence->AddChild(std::make_unique<BTActionNode>(3, registry, "MoveForward"));
38 return std::make_shared<const BTTree>(std::move(sequence));
39 }();
40 return tree;
41 }
42};
43
45 ScriptRegistry::Register({"BTExampleMover", "AI", {},
46 [] { return std::make_unique<BTExampleMover>(); }});
47}
48} // namespace NoJob
BTActionNode declarations for the Behavior Trees subsystem.
BTConditionNode declarations for the Behavior Trees subsystem.
Registry for native behavior tree callbacks.
BTScriptBridge declarations for the Behavior Trees subsystem.
BTSequence declarations for the Behavior Trees subsystem.
Registration and creation of native gameplay scripts.
void Set(std::string key, T &&value)
T GetOr(std::string_view key, T fallback) const
BTBlackboard & Blackboard()
Definition BTContext.h:90
std::shared_ptr< const BTTree > CreateTree() override
static void Register(ScriptDefinition definition)
void RegisterBTExampleMover()
ScriptableEntity * BTGetScript(BTContext &context) noexcept