NodeCanvas Forums › Support › [Question] Bind to components that are added at runtime.
Tagged: Blackboard; Variables; Binding
Hi,
This is my first post here, so hello š
I bought NC a couple of weeks ago and fell in love with it, so much so that I proceeded to buy FC and Slate based purely on the quality of NC. Congratulations on such a great product.
Anyhoo, the question. I’m comfortable with binding blackboard variables to properties etc. however I have hit a problem. To set the scene, I recently decided to restructure my ‘actors’ (actor being my chosen name for anything that is controlled by AI/Player), I created a scriptable object which contains the settings for the actor, the profile of that actor. At runtime, the base actor class processes this profile and dynamically adds the components which are required to satisfy the aforementioned profile. For example, if the actor can move it will add a MoveSkill component. Now the issue is that I know that a particular actor will have a component, but the NC binding complains that the MoveSkill component isn’t attached (at design time) to the object that the BT is bound to. I wondered how I could work around this?
I really don’t want to expose these properties on the actor object, because there will be LOTS. Also, I really don’t want to add the skill components at design time, again because there is going to be lots as the game develops. I realise this might be my only choice.
I have noticed an ‘isPresumedDynamic’ flag when creating your own actions/conditions. I wondered if there was something similar for BB variables? Essentially, informing NC that ‘I know this binding isn’t valid yet, but it will be’.
Thank you for taking the time to read this, and likewise if you can help out.
EDIT: I have just tested this out with a behaviour tree and it doesn’t complain, but when doing the same with an FSM on the same object it does complain.
Hello there and sorry for the late reply!
Also thank you very much for your positive kind feedback!
Just to clarify, are you referring to Variable Property/Field Binding?
If so, you can actually add a property/field bound variable in design time, even if the component does not exist (yet).
In runtime, when the variables initialize, only then will the required components that the variables need, be tried to be found from the gameobject. As such, if you add the required components in runtime, the variables will initialize correctly.
Please note however, that variables initialization happens in Blackboard “Awake”. As such, you will need to have the components attached before the Blackboard “Awake” call. This can be sorted by modifying Unity Script Execution Order and have your component attachment code run before Blackboard (which runs in Default Time).
Please let me know if the above is what you mean, or if you meant something different.
Can you please provide a bit more specific info about what you try to do (and what complaints do you get), if the above is not the case?
Thank you in advance!
Join us on Discord: https://discord.gg/97q2Rjh
Hi Gavalakis,
Thanks for replying.
This is correct, Iām referring to binding a blackboard variable to a property in a company which is not yet attached to the graph owner.
as I mentioned in my post earlier, this works fine in behaviour designer, when I add the runtime component to the prefs. however if I try and do the same in FlowCanvas, I get an error stating that this component is not attached to the owner.
Hello again,
Just to clarify, are you referring to Behaviour Designer as in NodeCanvas Behaviour Trees and FlowCanvas as in doing this with a FlowScriptController? š I try to replicate this but I wasn’t really able to.
– Can you please post for me the full error log stack ?
– Can you please ensure that the required components are added before the Awake call of Blackboard is called?
Please let me know.
Thank you!
Join us on Discord: https://discord.gg/97q2Rjh
Hi,
Yes exactly. Sorry, I’ve just realised the possible source of misunderstanding here. When I said Behaviour Designer, I was referring to the editor for the BT’s within NodeCanvas.
Essentially, when I add a component class to the Preferred Types in NodeCanvas, then from within the BT blackboard add a variable binding to a property on this component, when the target agent doesn’t have that component, it works fine. However, in FlowCanvas when I do the same, I get a warning at design time stating that the agent does not have that component.
Steps to Reproduce (FlowCanvas):
Hello again,
I just tried doing this once again (in FlowCanvas) and I didn’t get any problems.
Can you please post the full error log stack for me?
Also, what NC/FC version are you using?
Thank you.
Join us on Discord: https://discord.gg/97q2Rjh
Hi,
I think I’ve figured this out, the graph was loading and binding before my components were added. Setting the ‘First Activation’ to a lifecycle event after the one which assigns the dynamic components fixed the problem.
It’s been a while since I posted the original message, but I’m fairly certain I was receiving a warning during edit time. This is not the case anymore, so take that with a pinch of salt.
The attached picture shows two errors, one from the Behaviour Tree and one from the FlowScript, both are attached to the object which dynamically assigns its own components at runtime.
Please consider this issue resolved.
Hello again and thank you for the follow up.
I am glad this is resolved for you. Another way to fix this by the way, would be to open up and change the Unity Script Execution Order, but using a different “First Activation” like (eg On Start), is definitely a fine solution.
Thanks š
Join us on Discord: https://discord.gg/97q2Rjh