News Forums RAIN General Discussion and Troubleshooting Global variables in and out tree Behavior

This topic contains 13 replies, has 2 voices, and was last updated by  prime 1 year, 3 months ago.

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #34473

    Simsure
    Participant

    I have a group of enemies , and when you get too close and make an animation start to chase you .

    I would like if you get close to one of them he warns others .
    Is there a way to create variables that if activated to take effect on other behavior tree ?

    #34478

    prime
    Keymaster

    Not directly, no. There is no shared variable space or global blackboard in RAIN. However, it isn’t too difficult to create a communication system that allows AI to share information. That’s exactly what we did in our Squad Command package - we created a Communication Manager that manages channels and subscribers. AI can post information into the channels and subscribers can receive the information. We also created a communication Custom Element that automatically binds incoming information to AI memory - doing exactly what you are looking for.

    Here are some options for you:

    1) You can access another AI’s memory by grabbing their AIRig and then using their AIRig.AI.WorkingMemory to set variables.

    2) You can code up a more comprehensive comm system like I described. It takes a little effort, but isn’t too bad.

    3) You can go grab the comm system (and a few other helpful bits) out of our Squad Command Package, available here:
    https://www.assetstore.unity3d.com/en/#!/content/23526

    Whichever way you go, we’re happy to help answer questions. Just post back here.

    #34531

    Simsure
    Participant

    Ok , but ( excuse me if I’m wrong but I’m still learning something good to use RAIN ) I can also enter the actions personalizate in the behavior tree , I create the script , through the ones I can not create variables and reference varci always within them ?
    So basically the variables linked scripts ?

    #34533

    prime
    Keymaster

    I don’t think I understand your question. Maybe this will help:

    All variables that are used in a RAIN Behavior Tree come from the AI.Memory of the AI that is running the tree. So if 3 different AI are running the same behavior tree, they will each have their own variables with their own values.

    If you want to send information to an AI to use in its behavior tree, you must do so by creating or modifying a variable in that AI’s memory. You can do so on any AI by calling AI.WorkingMemory.SetItem() from code. Usually in code you will have access to the AIRig component rather than the AI itself, so you would call rig.AI.WorkingMemory.SetItem()

    So, to “warn the others” you would grab each other AI’s rig and do the SetItem call on each.

    #34606

    Simsure
    Participant

    Ok thanks , just one last thing that I came from this same problem

    I have a system ragdoll But if my character follows his AI do not perform .

    I realized that I just remove the ” mind ” but I’d like to do it from a script , how can I do ? obviously referring to the same gameobject there being more and more enemies at a time ?

    #34607

    prime
    Keymaster

    Are you saying that at some point (presumably when the AI dies) you want to enable Ragdoll and disable AI behavior?

    If so, to do that you simple need to set AI.IsActive = false from code. In a custom action, it would be

    ai.IsActive = false;

    or from a MonoBehavior

    AIRig tRig = GetComponentInChildren<AIRig>();
    tRig.AI.IsActive = false;
    #34757

    Simsure
    Participant

    Thanks Prime , I Found Another way Directly from System FPS to Activate ragdoll ( and to tell the truth I do not know even why Have worked , but it works ! )
    However there is Over What to disable the behavior tree , as in your example , there is A WAY TO CHANGE THE BEHAVIOR DIRECTLY FROM TREE CODE MonoBehavior

    #34758

    prime
    Keymaster

    I’m not sure what you are asking there. If you would like to simply disable the behavior tree, then no- there’s no way to do that from within the tree. However, you could always just put the tree in a non-functional state by creating a Repeating Sequencer with a yield node inside.

    #34816

    Simsure
    Participant

    Sorry , basically I want to change the behavior tree of a AI, from a script,a normal script to be included as a component of an object

    #34837

    prime
    Keymaster

    You’re going to want to use this method of the BasicMind:

    public virtual void SetBehavior(BTAsset aBTAsset, List<BTAssetBinding> aBindings)

    assuming you aren’t using bindings, the second parameter can be an empty list.

    #35440

    Simsure
    Participant

    Hello, sorry to re-open this discussion in a long time, but I is useful information that you gave me here, but now I can not apply, I’ve answered before:

    1) You can access another AI’s memory by grabbing their AIRig and then using their AIRig.AI.WorkingMemory to set variables.

    Prime

    I have many instances of the same object as I can then access the same variable of all the “memory”?

    I tried so:

         var see: int;
         see = AIRig.AI.WorkingMemory.GetItem. <int> ("see");
        
        
         see = 1;

    But issues an error where it says this

    An instance of type ‘RAIN.Core.AIRig’ is required to access non-static member ‘AI’.

    • This reply was modified 1 year, 3 months ago by  Simsure.
    • This reply was modified 1 year, 3 months ago by  Simsure.
    #35447

    prime
    Keymaster

    Where is this code happening? In a RAIN Custom Action in the behavior tree, or in some other code of yours (like a Monobehaviour)?

    If this is happening in a custom action, then you should be able to access the AI directly - a reference is passed in to the Start/Execute/Stop methods.

    If this is happening in a Monobehaviour attached to the AI “avatar”, then:

    var tRig: AIRig;
    tRig = gameObject.GetComponentInChildren.<AIRig>();
    tRig.AI.WorkingMemory.SetItem.<int>("see", 1);
    #35453

    Simsure
    Participant

    is a “custom action”, could you give me an example, do not think I’ve got it right!

    Basically I’m trying to do what I said in the first message of this topic, that is, if an enemy sees you feel all the enemies to attack you, I’m trying to do this by putting a variable “see” on all memory, and in behavior tree a “condition” detects that variable, the problem is precisely change that variable for all the enemies and i’m trying this method.

    #35456

    prime
    Keymaster

    Custom actions are scripts executed by the behavior tree. You are probably using a Detect node for the AI to notice when the player is nearby. You probably then want to execute a custom action to notify the other AI. Add a custom action node. Then in the inspector for that node, click the Class pulldown and do Create Custom Action. Give your action a name and click OK. After the behavior tree refreshes, you can right click on the custom action and choose Edit Custom Action. You will see that a script has been created for you. You will want to add code to the Execute method of the custom action.

    In your code, you will need to grab the other enemy AI in the scene - the AI that you will be sending information to. That can actually be a bit tough - there isn’t a system wide call to find all the other AI. In Squad Command, we handled this problem by adding a script that caused all AI to register with a common manager. You could then request a list of all AI from the common manager. If you do something similar, then you can iterate through the list and call WorkingMemory.SetItem on each AI.

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.