FlowCanvas Forums › Support › Setup an Instantiated "child" UnityObject › Reply To: Setup an Instantiated "child" UnityObject
Thanks net8floz for the reply, I managed to get a decent enough work around for the problem. I took this as an opportunity to delve into custom nodes. I made a custom SpawnPrefab node which takes in all the standard variables but allows you to setup Blackboard values dynamically. I have added the code below for completion sake.
Although I have to admit I am still confused as to why the standard flow script method doesn’t actually work. It seems to me that there may be something wrong with the Instantiate method where its not properly returning the instantiated object. Regardless, what I needed was to set Blackboard variables the moment the object is instantiated. The reason for that is because on Update the spawned bullet will check if its Target is null and destroy itself. I dont think running a method to setup the variable would of solved that minor complication.
Without any further ranting, below is my node and attached is a show of it in use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
public class SpawnPrefab : FlowControlNode { [SerializeField] [ExposeField] [GatherPortsCallback] [DelayedField] private int _BlackboardSetters = 0; GameObject gObj = null; Blackboard ObjectBB = null; protected override void RegisterPorts() { //Prefab values ValueInput<GameObject> Prefab = AddValueInput<GameObject>("Prefab"); ValueInput<Vector3> Position = AddValueInput<Vector3>("Position"); ValueInput<Quaternion> Rotation = AddValueInput<Quaternion>("Rotaion"); //list of Blackboard inputs var inputs = new List<ValueInput>(); for (var i = 0; i < _BlackboardSetters; i++){ inputs.Add(AddValueInput<string>("VarName[" + i + "]")); inputs.Add(AddValueInput<object>("VarValue[" + i + "]")); } //output results AddValueOutput<GameObject>("GObject", "", () => { return gObj; }); AddValueOutput<Blackboard>("Blackboard", "", () => { return ObjectBB; }); var fOut = AddFlowOutput(" ", "Out"); //run the flow logic AddFlowInput(" ", (f) => { gObj = null;//first null it gObj = Object.Instantiate<GameObject> (Prefab.value, Position.value, Rotation.value); ObjectBB = gObj.GetComponent<Blackboard>(); int idx = 0; //set the Blackboard values for (int i = 0; i < _BlackboardSetters; i++) { string name = inputs[idx].value.ToString(); object value = inputs[idx + 1].value; ObjectBB.SetValue(name, value); idx += 2; } //flow out fOut.Call(f); }); } } |