How to use BindProperty in code

NodeCanvas Forums Support How to use BindProperty in code

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #17141
    zd
    Participant

    I’m adding a behaviour tree owner and blackboard to an object at runtime, injecting a behaviour tree, and I think the best way to setup my blackboard is to do it in code since I can’t do it on an object then save it as a prefab right?

    So my Brain code looks like this:

    Start:
    _navMeshAgent= _controlledUnit.AddComponent<NavMeshAgent>();
    _behaviourTreeOwner = _controlledUnit.AddComponent<BehaviourTreeOwner>();
    _blackboard = _controlledUnit.AddComponent<Blackboard>();
    _behaviourTreeOwner.blackboard = _blackboard;
    InitializeBlackboard();

    InitializeBlackboard:
    Variable navMeshDestination = _blackboard.AddVariable(“_navMeshDestination”, _navMeshAgent.destination);
    //InitializeOtherVariables

    After this though, it appears the way to actually bind it to _navMeshAgent.destination so it gets updated during runtime is to use the BindProperty function? But I am unable to figure out how to use it. A one line example even would help me figure it out.

    I’m wondering if this is even the appropriate way to approach this problem of instantiating gameobjects and adding components at runtime, and thus needing to link all of those components/values at runtime without my behaviour tree graph turning into a cascading “GetComponent” chain.

    Variables required include: Current position of the object it’s on, the navmesh destination, target position, anything related to AI.

    #17142
    zsoik
    Participant

    After you added the variable to the blackboard, an easy way to initiate a binding is by calling:

    variable.BindProperty(typeof(YOUR_TARGET_TYPE).GetMember("NAME_OF_PROPERTY")[0]);

    Be sure to use the correct variable type that matches your binding target. If you’re building to AoT platforms be sure that the parts you’re referencing here indirectly won’t get stripped from the build.
    Also from what I know, you could just setup the bindings on a prefab then delete the binding target components from it, but you’ll have to notify the blackboard of the “binding context” at runtime in some cases (see IBlackboard.propertiesBindTarget to find out more about that) and re-bind the variables if you have to change it.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.