Injecting a subtree in runtime?

NodeCanvas Forums General Discussion Injecting a subtree in runtime?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #9136
    zxc

    I have an Enemy in my game and want to customize his behaviour depending on weapon he has. I imagine that a weapon in enemy’s hand would have its’ own BT and this BT would be injected into the main BT.

    Ie. enemy has stadard wander behaviour, when he becomes alerted he has standard seek behaviour when he doesn’t see the player. But when he does see the player, he has a diffrent attack scheme depending on weapon he is using.

    How should I approach implementation of this idea in Node Canvas? I assume I would need to find a tagged node for an injection point and then add weapon’s BT as a subtree at runtime. I don’t clearly see the methods for that in the API, though.

    Is it a valid approach and does the Node Canvas support it?

    #9143
    Gavalakis
    Keymaster

    Hello,

    One way to approach this (probably the easiest) would be to make and use an enum variable for the available weapons and then check which is the current using weapon and run the coresponding SubTree behaviour.
    So instead of replacing subtrees, you have them already in your graph and “switch” between them accordingly.

    Do you want to replace subtrees for a specific reason, instead of having them placed already?

    Let me know 🙂

    Join us on Discord: https://discord.gg/97q2Rjh

    #9142
    zxc

    That’s also a solution, but I don’t feel it takes advantage of the behaviour tree modularity. Weapons is just a simple example, but consider dynamic usage of the environment – for instance a bomb placed in a level, that an AI Enemy can shoot to kill a player; or a terminal it can hack for some other advantage. I see it that the behaviours associated with these special objects should be BTs that are stored and coded in the objects. AI just “takes” those BTs and injects them into the main BT.

    Not that I invented this approach. I just “heard” that that’s how it’s done sometimes and wanted to test it :). I’m currently listening to AIGameDev podcasts like crazy and also this Gamasutra article suggests similar approach.

    Anyway I managed to inject a subtree with a following code (simple stuff actually):
    _treeowner.BT.GetNodeWithTag<BTSubTree>("I").nestedGraph = subTree;
    So I have an empty subTree node in the graph, thats tagged “I”. In runtime I find this node and replace the SubTree with a reference to my custom weapon BT. Only drawback is that I need to create a “stub” SubTree that fails when called – so that the injection point doesn’t return SUCCESS when there is no tree injected.

    If I could specify the return value of the empty SubTree Node, that would be great. Then the unused injection points wouldn’t have to be handled additionally.

    #9141
    zxc

    I’m now trying to implement the above mentioned “stub” SubTree – how do I create a node that returns Status.FAILURE? I mean – without creating a custom node type of checking against a blackboard value that’s always false.

    EDIT: nevermind, a condition node with no condition fails enough :).

    #9140
    Gavalakis
    Keymaster

    Hey,

    Generaly speaking, I am working hard on bringing some more modularity and parametrization in the couple of next NC versions. The hardest part of it all is still keeping the nice workflow, editor wise.
    That article is great by the way (it’s one of those I’ve added in the references links :))

    For you question, you can use the Remap Decorator and remap Success and Failure to either Success or Failure, thus possible to either always return the same status, or invert the status returned from the decorated child.

    Is that what you are asking? 🙂

    Join us on Discord: https://discord.gg/97q2Rjh

    #9139
    Gavalakis
    Keymaster

    Yeah. the condition node also fails enough if a simple failure is what you want 🙂

    Join us on Discord: https://discord.gg/97q2Rjh

    #9138
    msl_manni
    Participant

    A different approach would be to use a action task to do the same thing. The only thing would be better if the BT is cloned or instantiated so that multiple agents can use the same BT.

    I use similar approach for FSM for dynamic FSMs.

    #9137
    msl_manni
    Participant

    Here is the code for Injecting NestedFSM in a FSM and changing it by, an action task. This is much better but there might be some cleaning needed.

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