Skip to content

Commit 0c93761

Browse files
authored
enable to fetch data from graph instance at runtime. (#130)
1 parent 02efff9 commit 0c93761

13 files changed

+1181
-683
lines changed

Packages/builder.visual/Runtime/BehaviorTreeBuilder.cs

+34-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void Execute<TCtx>(TCtx ctx) where TCtx : IGraphInstance
5353
Blob = blob, Thread = Thread, AutoCreation = AutoCreation
5454
});
5555

56+
dstManager.AddComponentObject(entity, new GraphInstanceComponent {Value = instance});
57+
5658
#if UNITY_EDITOR
5759
var debugName = ctx.ReadString(DebugName);
5860
if (!string.IsNullOrEmpty(debugName)) dstManager.SetName(entity, debugName);
@@ -102,7 +104,34 @@ public static IEnumerable<INodeDataBuilder> ToBuilderNode(this OutputTriggerMult
102104
}
103105

104106
[Pure]
105-
public static unsafe IVariableProperty<T> ToVariableProperty<T>(this InputDataPort port, [NotNull] GraphInstance instance, [NotNull] GraphDefinition definition) where T : unmanaged
107+
public static IVariableProperty<T> ToVariablePropertyReadOnly<T>(this InputDataPort port, [NotNull] GraphInstance instance, [NotNull] GraphDefinition definition) where T : unmanaged
108+
{
109+
return ToVariableProperty(port, instance, definition, () => new GraphVariableProperty<T>(port));
110+
}
111+
112+
[Pure]
113+
public static IVariableProperty<T> ToVariablePropertyReadWrite<T>(this InputDataPort port, [NotNull] GraphInstance instance, [NotNull] GraphDefinition definition) where T : unmanaged
114+
{
115+
return ToVariableProperty(port, instance, definition, () => ToConstVariable<T>(instance, port));
116+
}
117+
118+
[Pure]
119+
private static unsafe IVariableProperty<T> ToConstVariable<T>([NotNull] GraphInstance instance, InputDataPort port) where T : unmanaged
120+
{
121+
T data;
122+
void* ptr = &data;
123+
var value = instance.ReadValue(port);
124+
Value.SetPtrToValue(ptr, value.Type, value);
125+
return new CustomVariableProperty<T> {CustomValue = data};
126+
}
127+
128+
[Pure]
129+
private static IVariableProperty<T> ToVariableProperty<T>(
130+
InputDataPort port
131+
, [NotNull] GraphInstance instance
132+
, [NotNull] GraphDefinition definition
133+
, [NotNull] Func<IVariableProperty<T>> createGraphVariable
134+
) where T : unmanaged
106135
{
107136
var inputPortIndex = (int)port.Port.Index;
108137
Assert.IsTrue(inputPortIndex < definition.PortInfoTable.Count);
@@ -118,11 +147,10 @@ public static unsafe IVariableProperty<T> ToVariableProperty<T>(this InputDataPo
118147
if (dataNode is IVisualVariablePropertyNode<T> genericPropertyNode)
119148
return genericPropertyNode.GetVariableProperty(instance, definition);
120149

121-
T data;
122-
void* ptr = &data;
123-
var value = instance.ReadValue(port);
124-
Value.SetPtrToValue(ptr, value.Type, value);
125-
return new CustomVariableProperty<T> { CustomValue = data };
150+
if (dataNode is IConstantNode)
151+
return ToConstVariable<T>(instance, port);
152+
153+
return createGraphVariable();
126154
}
127155

128156
// copy from `GraphInstance.GetComponentFieldDescription`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Runtime;
2+
using Unity.Entities;
3+
4+
namespace EntitiesBT.Builder.Visual
5+
{
6+
public class GraphInstanceComponent : IComponentData
7+
{
8+
public GraphInstance Value;
9+
}
10+
}

Packages/builder.visual/Runtime/GraphInstanceComponent.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/builder.visual/Runtime/Node/VisualTimer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public INodeDataBuilder GetBuilder(GraphInstance instance, GraphDefinition defin
2929
void BuildImpl(BlobBuilder blobBuilder, ref TimerNode data, INodeDataBuilder self, ITreeNode<INodeDataBuilder>[] builders)
3030
{
3131
data.BreakReturnState = @this.BreakReturnState;
32-
@this.CountdownSeconds.ToVariableProperty<float>(instance, definition)
32+
@this.CountdownSeconds.ToVariablePropertyReadWrite<float>(instance, definition)
3333
.Allocate(ref blobBuilder, ref data.CountdownSeconds, self, builders)
3434
;
3535
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using EntitiesBT.Core;
5+
using EntitiesBT.Entities;
6+
using EntitiesBT.Variable;
7+
using Runtime;
8+
using Unity.Entities;
9+
using UnityEngine.Scripting;
10+
11+
namespace EntitiesBT.Builder.Visual
12+
{
13+
public class GraphVariableProperty<T> : VariableProperty<T> where T : unmanaged
14+
{
15+
private readonly InputDataPort _port;
16+
public override int VariablePropertyTypeId => ID;
17+
18+
public GraphVariableProperty(InputDataPort port)
19+
{
20+
_port = port;
21+
}
22+
23+
protected override void AllocateData(ref BlobBuilder builder, ref BlobVariable<T> blobVariable, INodeDataBuilder self, ITreeNode<INodeDataBuilder>[] tree)
24+
{
25+
builder.Allocate(ref blobVariable, _port);
26+
}
27+
28+
static GraphVariableProperty()
29+
{
30+
var type = typeof(GraphVariableProperty<T>);
31+
VariableRegisters<T>.Register(ID, type.Getter("GetData"), null, GetComponentAccess);
32+
33+
IEnumerable<ComponentType> GetComponentAccess(ref BlobVariable<T> variable)
34+
{
35+
return ComponentType.ReadOnly<CurrentBehaviorTreeComponent>().Yield();
36+
}
37+
}
38+
39+
public static readonly int ID = new Guid("A3B60190-98AF-42DC-A723-35AD725172BB").GetHashCode();
40+
41+
[Preserve]
42+
private static unsafe T GetData<TNodeBlob, TBlackboard>(ref BlobVariable<T> blobVariable, int index, ref TNodeBlob blob, ref TBlackboard bb)
43+
where TNodeBlob : struct, INodeBlob
44+
where TBlackboard : struct, IBlackboard
45+
{
46+
ref var port = ref blobVariable.Value<InputDataPort>();
47+
var behaviorTree = bb.GetData<CurrentBehaviorTreeComponent>().RefValue.BehaviorTree;
48+
// HACK: how to support multiple worlds?
49+
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
50+
var graphInstance = entityManager.GetComponentObject<GraphInstanceComponent>(behaviorTree).Value;
51+
52+
T data;
53+
void* ptr = &data;
54+
var value = graphInstance.ReadValue(port);
55+
Value.SetPtrToValue(ptr, value.Type, value);
56+
return data;
57+
}
58+
}
59+
}

Packages/builder.visual/Runtime/Variable/GraphVariableProperty.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/builder.visual/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.quabug.entities-bt.builder.visual",
33
"description": "Behavior Tree for Unity Entities - Visual Builder",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"unity": "2020.1",
66
"displayName": "EntitiesBT - Visual Builder",
77
"dependencies": {

0 commit comments

Comments
 (0)