NodeCanvas Forums › General Discussion › Communication via Blackboard or Events
Hei,
I’m nearly going crazy trying to communicate between my hunting DroneObject driven by Nodecanvas and a TargetObject controlled by a DamageHandler from UFPS.
Last I used a Blackboard way, but:
I found that example for SetDataValue in another topic (http://forum.unity3d.com/threads/nodecanvas-behaviour-trees-dialogue-trees-state-machines.227190/), but using this I get a bunch of errors (see Img). Is this due to using the code snippet inside my OnMessage.Action?
And why I need get AND set, when I only want to set a Blackboard variable? I’m a bit overwhelmed by the various traps at the moment. It took me one day to got this Eventhandler from UFPS working in my own script and now I’m not able to bring the easy GotKilled-Message in any form in NodeCanvas. First I simply tried to use a condition in nodeCanvas to check if my targetObject is destroyed (=inactive), but didn’t find an appropriate action and failed to implement a functioning new one..
Any advice for a preferably simple solution is very welcome.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
1.using UnityEngine; 2. 3.public class Unit : MonoBehaviour { 4. 5. public Blackboard blackboard; 6. 7. GameObject target{ 8. get {return blackboard.GetDataValue<GameObject>("target");} 9. set {blackboard.SetDataValue("target", value);} 10. } 11. } |
My Code:
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 |
using UnityEngine; using NodeCanvas; using NodeCanvas.Variables; public class DroneCtrl : MonoBehaviour { // Blackboard referenzieren public Blackboard blackboard; // PlayerEventHandler verfügbar machen vp_PlayerEventHandler m_Player; void Awake() { m_Player = transform.GetComponent<vp_PlayerEventHandler>(); } // Target Script beim EventHandler registrieren protected virtual void OnEnable() { if (m_Player != null) m_Player.Register(this); } protected virtual void OnDisable() { if (m_Player != null) m_Player.Unregister(this); } protected virtual void OnMessage_GotKilled() { GameObject nearestTarget { get {return blackboard.GetDataValue<GameObject>("nearestTarget");} set {blackboard.SetDataValue(“nearestTarget”, null);} } Debug.Log("-------------------ok, Drone knows that target is dead ----------------------"); } |
Hey,
That get/set code is a property and you are trying to declare it in a method :). You can simply omit the property and do this, directly calling SetDataValue on the blackboard:
1 2 3 4 5 6 7 |
protected virtual void OnMessage_GotKilled() { blackboard.SetDataValue(“nearestTarget”, null); Debug.Log("-------------------ok, Drone knows that target is dead ----------------------"); } |
Furthermore, you can check if a game object is active with the ‘CheckProperty’ Condition, through which you can check any property on any component on any game object (image attached) 🙂
If you wanted to check if a blackboard gameobject variable is null instead, you can do so with the ‘Check GameObject’ condition. Simply check ValueA against a null ValueB in that condition.
There are more ways to communicate between objects but I don’t want to confuse you right now.
Let me know if you have any questions and the above suffice 🙂
Cheers
Join us on Discord: https://discord.gg/97q2Rjh
Hey Gavalakis,
thanks for your explanations! I got a bit clearer now, but there are still some obstacles.
The Code for SetDataValue is really nice and simple, but using exactly this I got an:
Assets/02-Scripts/DroneCtrl.cs(34,33): error CS1525: Unexpected symbol `<internal>’
I don’t know what this error means, there is no <internal>, googling CS1525 brought hints for weird Symbols……
aaaah ok! Retyping the whole line, and the error vanished 🙂 first success!
-> Blackboard SetDataValue seems to work finally.
I’ve also tested the other ways with checkProperty/checkGameObject, since they would be the easiest solution without the need of events.
1. CheckProperty – can’t use this like in your example, the targetObject is dynamically saved in Blackboard ‘nearestTarget’, so I get ‘this Condition needs the Agent to be known…’, see screenshot.
2. Blackboard Check GameObject doesn’t do the job, because my Blackboard variable ‘nearestTarget’ does not reflect, if the referenced target gameObject is set active or not, and active/inactive seems to be the only status I get back from my target with DamageHandler-Script without using extra events.
So I’m a few steps nearer but not done now.
Hey,
Indeed the Script Control tasks do not work with variable objects that are set at runtime due to not knowing what components will actualy be on that gameobject. I am looking at ways to improve that though.
You can use the following simple Condition Task to check if a game object is active or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using UnityEngine; namespace NodeCanvas.Conditions{ [AgentType(typeof(Transform))] [Category("GameObject")] public class IsActive : ConditionTask { protected override bool OnCheck(){ return agent.gameObject.active; } } } |
As in every task, if you want to check the condition for a blackboard variable, override the agent and select your variable from the dropdown.
Cheers!
Join us on Discord: https://discord.gg/97q2Rjh
Hey,
that action works perfect, thanks!
🙂
I installed the nodeCanvas Update this afternoon and got some strange behaviors now – it seems that all NC-Action stops at my subtree and doesn’t return to the main tree afterwards. Is this a possible change after update or must the reason be located somewhere else?
PS: Investigated further, BehaviourTree is executed as usual, Subtree also, then at end of Subtree NC seems stopped – PlayButton shows initial state, NC-Editorwindow shows no activity. If I push the StepButton, NC returns to Maintree and performs first action, as expected.
…sometimes nodeCanvas stops just somewhere in Maintree, no Message is displayed, no reason apparent.
hmm, what can I do??
Can you please send me a project that this happens at info_at_nodecanvas.com ?
I can’t reproduce this strange behaviour no matter what.
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh