Search Unity

Behavior Designer - Behavior Trees for Everyone

Discussion in 'Assets and Asset Store' started by opsive, Feb 10, 2014.

  1. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,685
    For a long time there was a free version of A*. It's probably still out there, if you want to use that.
     
  2. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    hi opsive.

    I have finally managed to get the game to run on XboxOne. And it does indeed "work" on XboxOne AOT , but.. not the VariableSynchronizer. :( I use this a lot and my AI doesn't animate well without them. Can you look into it? Is there any other way to fix this internally without I have to use totally different method and re-setup all my AI units?

    ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) System.Runtime.CompilerServices.ExecutionScope:lambda_method (System.Runtime.CompilerServices.ExecutionScope)' while running with --aot-only.

    at (wrapper managed-to-native) System.Delegate:CreateDelegate_internal (System.Type,object,System.Reflection.MethodInfo,bool)
    at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
    at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
    at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType, System.Object target) [0x00000] in <filename unknown>:0
    at System.Linq.Expressions.EmitContext.CreateDelegate (System.Runtime.CompilerServices.ExecutionScope scope) [0x00000] in <filename unknown>:0
    at System.Linq.Expressions.CompilationContext.CreateDelegate (Int32 unit, System.Runtime.CompilerServices.ExecutionScope scope) [0x00000] in <filename unknown>:0
    at System.Linq.Expressions.CompilationContext.CreateDelegate () [0x00000] in <filename unknown>:0
    at System.Linq.Expressions.LambdaExpression.Compile () [0x00000] in <filename unknown>:0
    at System.Linq.Expressions.Expression`1[System.Func`1[System.Object]].Compile () [0x00000] in <filename unknown>:0
    at BehaviorDesigner.Runtime.VariableSynchronizer.CreateGetDelegate (System.Object instance, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
    at BehaviorDesigner.Runtime.VariableSynchronizer.Awake () [0x00000] in <filename unknown>:0
    UnityEngine.Object:INTERNAL_CALL_Internal_InstantiateSingle(Object, Vector3&, Quaternion&)
    UnityEngine.Object:Internal_InstantiateSingle(Object, Vector3, Quaternion)
    UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
     
  3. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    The Variable Synchronzer uses the Expression class which doesn't work well with JIT. I am trying to work on a way to avoid this, but an easy workaround would be to use Variable Mappings. Variable Mappings have a better performance compared to the variable synchronizer because it only accesses the value when it needs to. It'll also work with JIT because of the way that it is setup and it should be painless to switch to. I have actually been considering deprecating the variable syncornizer because it's older than Variable Mappings and you can (almost) do everything with variable mappings that you can with the variable synchronizer. The limitation is that variable mappings do not work with integrations such as Playmaker. I am still looking for a variable synchronizer solution though.
     
  4. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Humm.. ok I must have the old version.. I can't see that little arrow button to do mapping. I have 1.4.6

    Do I need to update? I am afraid of breaking the game... especially when I made modification to the movement pack's scripts. Is there way to update just the core of BT without breaking anything?
     
  5. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Variable Mappings were introduced in 1.5 so it looks like you just missed it. You should be able to update just the Behavior Designer asset and leave the Movement Pack as is - this will update just the core. Behavior Designer 1.5.6 brings some significant serialization performance improvements so it is good to update regardless. As always, make sure you have a backup just in case.
     
  6. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Isn't he using AOT?
     
  7. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Good catch, yes, that's correct
     
  8. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Ok.. so, I have tried the new version.. but I am unable to link between sharedfloat variable and one of my Animator's float value. All I see is basic property of the Animator itself, not the custom variable value I have created inside the Animator. ( which makes it a bit useless..) ? Am I doing something wrong?
     
  9. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Also, I am unable to link the shared variables to multiple target. For example, I need to link my movementSpeed value to 2 Animators. I can only seem to try and link one to one mapping. :( , I can understand this as it seems like it always goes both ways. But that is not what I need sometimes.
     
  10. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    also talking about the performance increase for the serialization. I can see there are two settings, binary (which I is the on I have been using before) and Json.

    I have not noticed the whole serialization with BD before , so which one do you recommend to use for best performance with AOT platform?
     
  11. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Variable Mappings do not support parameters yet, but it should be pretty easy to handle both of those situations. You could create a new property in your c# script which sets the animator values. For example:
    Code (csharp):
    1.  
    2. public float MovementSpeed { get { return animator.GetFloat("MovementSpeed"); } set { animator.SetFloat("MovementSpeed", value); animator.SetFloat("MovementSpeed2", value);} }
    3.  
    Binary will be faster, but I haven't tested it on the Xbox One whereas @magique was having some issues with it. The serialization has since changed so it definitely doesn't hurt to try first.
     
  12. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Well.. I could go with the property in my custom script. Yeah.. but the good point (not all) of using variable syn, and built in - (given) methods is to avoid creating my own script.

    At least I understand how to go about working around the limitation. It's not too bad! I will try it and report back.
     
  13. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Ok.. after converting my set up with new version , I get this error on one of my BT.

    ArgumentException: Object type BehaviorDesigner.Runtime.SharedGenericVariable cannot be converted to target type: BehaviorDesigner.Runtime.SharedGenericVariable
    Parameter name: val
    System.Reflection.MonoField.SetValue (System.Object obj, System.Object val, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoField.cs:133)
    System.Reflection.FieldInfo.SetValue (System.Object obj, System.Object value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/FieldInfo.cs:150)
    BinaryDeserialization.LoadFields (BehaviorDesigner.Runtime.FieldSerializationData fieldSerializationData, System.Collections.Generic.Dictionary`2 fieldIndexMap, System.Object obj, Int32 hashPrefix, IVariableSource variableSource)
    BinaryDeserialization.LoadTask (BehaviorDesigner.Runtime.TaskSerializationData taskSerializationData, BehaviorDesigner.Runtime.FieldSerializationData fieldSerializationData, System.Collections.Generic.List`1& taskList, BehaviorDesigner.Runtime.BehaviorSource& behaviorSource)
    BinaryDeserialization.Load (BehaviorDesigner.Runtime.TaskSerializationData taskData, BehaviorDesigner.Runtime.BehaviorSource behaviorSource)
    BehaviorDesigner.Runtime.BehaviorSource.CheckForSerialization (Boolean force, BehaviorDesigner.Runtime.BehaviorSource behaviorSource)
    BehaviorDesigner.Editor.GraphDesigner.Load (BehaviorDesigner.Runtime.BehaviorSource behaviorSource, Boolean loadPrevBehavior, Vector2 nodePosition)
    BehaviorDesigner.Editor.BehaviorDesignerWindow.LoadBehavior (BehaviorDesigner.Runtime.BehaviorSource behaviorSource, Boolean loadPrevBehavior, Boolean inspectorLoad)
    BehaviorDesigner.Editor.BehaviorDesignerWindow.ReloadPreviousBehavior ()
    BehaviorDesigner.Editor.BehaviorDesignerWindow.OnFocus ()
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
    UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:187)
    UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:180)
    UnityEditor.HostView.OnFocus () (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:90)

    I am not sure which node is causing problems but perhaps sendmessage one?
     
  14. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Is that an old behavior tree that was previously saved in 1.4.9? If you send it to me I can take a look at it to see why it can't be converted. In this case I shouldn't need any of the actual tasks or anything - just the tree will work!
     
  15. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    ugh.. how do i send you the tree? humm let me save the package.. maybe.
     
  16. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    If it's within a scene you can move that GameObject to a prefab and then send me just that prefab. I just need the prefab - you can send the link to me via PM or email.

    Edit: I just sent you a link to the latest in my dev branch. Give that version a try and if it still doesn't work then send me your tree - I may have inadvertently fixed it.
     
    Last edited: Jun 4, 2016
  17. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    I've been working with @castor76 and I figured out the cause of this. Posting it here just in case somebody else hits the same error:

    This error is caused by duplicate SharedGenericVariable definitions. Prior to Behavior Designer 1.5.x SharedGenericVariable was outside the dll, but with newer versions it's located inside the DLL. To fix this error all that you need to do is delete Behavior Designer/Runtime/Variables/SharedGenericVariable.cs. When you're updating Behavior Designer to a newer version it's always best to completely remove the entire Behavior Designer directory as Unity Packages don't handle changes like this too well.
     
  18. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Arhh, I see. In my case I had my custom tasks also inside the behaviour package folders so I didn't really want to delete the whole thing. I should be putting them to the different folders next time. Thanks for the suggested fix. It is always good working with you.
     
    opsive likes this.
  19. kilju

    kilju

    Joined:
    Nov 4, 2013
    Posts:
    127
    hi opsive.
    i just bought behaviour designer and i emailed my invoice number to justin@opsive.com
    to get that free movement pack
    is this right address to contact u guys?

    thank you

    -Toni
     
  20. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    That'll work - just replied!
     
    kilju likes this.
  21. kilju

    kilju

    Joined:
    Nov 4, 2013
    Posts:
    127
    Just started to play with behaviour designer and i love it :D really nice. i got little problem now tho. how can i loop my walk animation? i tried to put paraller and under it seek and play animation. it starts and plays my walk animation 1 time and then it stops. any help for this?

    thank you for great product!

    -Toni
     
  22. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Justin, I've been taking a look at the Utility AI by Apex and reading a lot about the state-of-the-art in AI design. According to Apex, Behavior Trees are out-dated and limited in their capability and that Utility AI is the way of the future. Now, I don't know whether they are right about that necessarily as I can still see quite a bit of value in behavior trees and I don't think Utility AI is a solution that fits every scenario. However, I do see some big advantages to having some of the features they are providing. Is there any chance we can get some sort of Utility AI functionality built into Behavior Designer? That way we could have the best of both worlds.

    EDIT: I even found this article that talks about incorporating utility theory into a behavior tree. It might be useful.

    http://www.gameaipro.com/GameAIPro/...ecisions_into_Your_Existing_Behavior_Tree.pdf
     
    Last edited: Jun 11, 2016
  23. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Glad you're enjoying it. You can use the Repeater with Repeat Forever set but I actually recommend not controlling the animator directly from the behavior tree. Take a look at this post for more info.
     
  24. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Just like any AI technique, there are advantages and disadvantages to each and it really depends on the game that you are making. I have been thinking though how to incorporate the utility AI concept into a behavior tree and have come up with some ideas.

    The actual utility AI concept is pretty basic - evaluate a set of actions and take the action that has the highest score. The existing Priority Selector task is a rudimentary version of this, it just doesn't reevaluate every tick or interrupt children so it needs some expansion. I'd like to experiment by creating a new task that will do just that - reevaluate every tick as long as the branch is active and then also allow for children interruption. It's kind of a combination of the Priority Selector and the Selector Evaluator tasks.

    When I start to play with the idea I can let you try out a beta, but I should be able to add it to the next version. I don't know too much more right but, but if you have any particular suggestions I can take that into account when designing it.
     
    John-G likes this.
  25. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Sounds good. I'm new to the concept of utility ai, but it looks like a good way to give an NPC a more organic feel. That article I posted the link to above has some good ideas on how to make it work in a behavior tree. I'm very interested in hearing if other BD users think this is a good idea and any suggestions they might have as well.
     
  26. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Thanks for that link - it looks like others have thought about this as well. I'm happy that my thoughts were pretty much the same as what the author proposed so it definitely should be doable. In fact I actually don't think that it'll take too much work - just a new task and possible a new method overload.
     
    magique likes this.
  27. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Like Justin wrote, every AI technique has pro's and con's. I've worked fairly extensively with utility-based AI. (I have a utility-based AI system practically ready to submit to the store once my quest asset is out the door, but I'm just going to let Apex fill that niche now.) Utility-based AI is good at identifying priorities (e.g., right now, getting a first aid kit is more important than attacking the enemy), but by itself it's not as good at actually doing something with it (e.g., run to the kit, pick it up, use it).

    Take for example The Sims, which is the quintessential example of utility-based AI. Sim agents decide what object to use based on a utility value. But then they basically use a simple FSM to actually use the object. Since a behavior tree is a superset of an FSM, you can use Behavior Designer the same way.

    The challenge with utility-based AI is not the AI itself, but annotating the game world with enough useful information for agents to make informed utility evaluations. I'm just throwing that thought out there for anyone who's thinking of using utility-based AI with Behavior Designer or any other system.
     
    opsive likes this.
  28. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Thanks for your insights Tony. It does seem like it would take a lot of experimentation in order to annotate all of the actions so they balance each other in a realistic way.

    Any estimate on when you'll be submitting? I'd be happy to add integration as soon as it's out.
     
  29. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    No more forecasts from me. Every time I do that, it seems to summon another delay. Sometimes they're awesome delays that I'm really grateful for, like the Dialogue System being in last week's Madness Sale, but delays nonetheless. :) I'll be sure to send you an advance copy as soon as I can, though!
     
    hopeful and opsive like this.
  30. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    @TonyLi, do you have WIP thread for your quest system? I'm just about to purchase a quest system, but I like to see all the options.
     
  31. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Not yet. But I'll look into the possibility of making it work with Joris's quest system as well. They're different beasts. My quest system is built around dynamically-generated quests, although it also does traditional hand-written quests and has a quest journal interface. But Joris's system is actually on the store now.

    We'll make sure my quest system has Behavior Designer support, too, of course!
     
    Tinjaw and hopeful like this.
  32. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Excellent. Yeah, that's the system I am buying so if yours can work with it and enhance it then I'll keep an eye out for it too.
     
  33. kilju

    kilju

    Joined:
    Nov 4, 2013
    Posts:
    127
    ok i got my zombie ai to work nicely. i got one more problem.
    how i can do melee damage to my player?
    im using ufps so do i need to make empty melee weapon to get it to work? or is there easy work around?
    my zombie got punch aimation so i would only need to send damage to player.

    thank you
     
  34. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Is your zombie a UFPS character? Or is it a standard player? If it's a UFPS character you can use the Attack task, otherwise you'll need to call Damage on the vp_LocalPlayer:

    https://docs.google.com/document/d/...m3KzMfsPUGBI/pub?embedded=true#h.3uqjbih3evpl
     
  35. kilju

    kilju

    Joined:
    Nov 4, 2013
    Posts:
    127
    Last edited: Jun 12, 2016
  36. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    In that case you'll want to create a script which interacts with your character and does the attack. Your attack method can then call the vp_LocalPlayer.Damage function.
     
  37. kimsama

    kimsama

    Joined:
    Jan 2, 2009
    Posts:
    166
    Hello,

    EditorUtils class conflicts with other package(especially with NodeCanvas). It needs its namespace.

    I'm using Behavior Designer 1.5.6 but not asset store version but hotfix version with Runtime source code.

    -Kim
     
  38. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Where is the EditorUtils class located? Behavior Designer doesn't include a EditorUtils class and all of its classes are in a namespace.
     
    Last edited: Jun 16, 2016
  39. kashifkamil

    kashifkamil

    Joined:
    Apr 6, 2016
    Posts:
    4
    Hello

    Is it possible to use Behavior Designer on flying objects. For example, choppers which can patrol, follow and attack the player while flying
     
  40. SuperNewbee

    SuperNewbee

    Joined:
    Jun 2, 2012
    Posts:
    196
    @kashifkamil

    I tried it with some pterodactyls and they seemed to fly around pretty nicely. I changed the base offset in the Navmesh agent to raise them into the air. There are probably better ways of doing it with behavior designer, but I just needed some quick eye candy for the scene.
     
  41. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    @SuperNewbee has a great solution. For some background, the actual behavior tree implementation doesn't care if the agent is on the ground or in the air. It does matter more for the pathfinding implementation of the Movement Pack and for that you can use the NavMeshAgent offset. This will prevent your ship from being able to pathfind "under" another ship, but pathfinding in the 3D space is computationally expensive and no pathfinding implementation on the Asset Store can currently do it
     
  42. javier11om

    javier11om

    Joined:
    Apr 27, 2014
    Posts:
    28
    Hi, at import for ICode I'm getting an error ...
    _AIForMecanim.cs(27,38): error CS0104: `StartStateMachine' is an ambiguous reference between `BehaviorDesigner.Runtime.Tasks.ICode.StartStateMachine' and `ICode.StartStateMachine'

    There used to be integration from ICodes end but I don't see anything on his site.
     
  43. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Thanks for the heads up. I just updated the ICode integration and sample scene.
     
    javier11om likes this.
  44. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I have a simple question: Does Behavior Designer support teams without writing everything myself? Something like "this sensor should only detect enemies" and a way to assign at runtime just who is on which team?
     
  45. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    The sensor to only detect enemies can be done with the Can See Object which contains a layer mask field that you can set at runtime. Can See Object and Can Hear Object are the two major tasks which use a layer mask that could be used for teams. Beyond that teams are pretty game specific so setting the various layer masks are the extent of the team support.

    Our next asset uses Behavior Designer and it'll allow for team-based deathmatch games. I have started to implement the team behavior tree and agents can attack the other team and communicate with each other for cases like following one agent or calling for backup. Agents communicate with each other using Behavior Designer events which prevent you from having to write any code. I did however have to write some custom team code in order to decide which agent should be a leader of a group formation. Take a look at this post for a quick overview of what the branch of that tree looks like.
     
  46. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Is there a tutorial or video where this layer maks thing can be seen in action? I did a quick search and couldn't find anything.
     
  47. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    The Can See Object has a field called objectLayerMask which has a type of LayerMask. You can get a reference to that task with behavior.FindTaskWithName(string name) and then once you have a reference to that task you'd be able to set the layer mask, something like canSeeObject.objectLayerMask = 1 << LayerMask.NameToLayer("RedTeam").
     
  48. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    I've mentioned our next asset, the Deathmatch AI Kit, a couple of times in this thread and we're far enough along now that it has it's own WIP thread. This post has all of the details.

     
    TechnicalArtist likes this.
  49. acgleader

    acgleader

    Joined:
    Apr 17, 2013
    Posts:
    40
    Can you support static compiling trees to source code for improving imperformance
     
  50. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,127
    Compiling the tree would be a pretty major change - I've written it down on my todo list so I'll take a closer look at what is required to actually make that change as well as the tradeoffs.
     
    Last edited: Jul 19, 2016