News Forums RAIN Sample Projects, How To, and Code Information exchange between Unity and Rain

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #34448

    Jaroslav
    Participant

    Hello all!

    I’ve been wondering for days now, how to exchange information between Unity and Rain. For example, when you have FPS shooter and you cast a Raycast via unity script, you hit the enemy and then play death animation. But in behaviour tree the enemy is still moving because I didn’t know how to send message from unity script to rain script informing the rain, that enemy is dead. Today i found out, that by writing custom extension and adding it to the AI I can manipulate working memory and change decisions in behavior tree.

    Now my question is, how to access working memory from outside RAIN (in Unity).
    For example:

    // cast a ray from gun
    if (Physics.Raycast (ray, out hit))
    {  
        // code for parent omitted
        // if object is not an environment or dead enemy
        if (parent.tag != "Environment" && hit.collider.gameObject.tag != "Dead")
        {
            // get animator
            Animator animator = (Animator) hit.collider.gameObject.GetComponent(typeof(Animator));
    	if (animator != null)
    	{
                // play death enimation
                animator.SetBool("Dead", true);
                // CODE TO IMPLEMENT:
                // inform RAIN memory that target is dead so change behavior
    	}
        }
    }

    I’ve seen http://rivaltheory.com/forums/topic/custom-code-to-change-int-value-in-memory/, but I’m not sure I got it.

    Thank you!

    EDIT:

    I think I found it here: http://rivaltheory.com/forums/topic/integrating-with-scripts/

    • This topic was modified 1 year, 5 months ago by  Jaroslav.
    • This topic was modified 1 year, 5 months ago by  Jaroslav.
    #34453

    Moloch
    Participant

    You should create a variable in the AIRig memory and set it using:
    AIRig rig = GetComponentInChildren<AIRig>();
    rig.AI.WorkingMemory.SetItem<T>(string name, <T> value);
    In this case an isDead bool would be appropriate.

    Then in the enemy behaviour tree, have a component that checks if the condition is false - behave normaly as an enemy does, but if the bool is true - animate death. Set the animation through the behaviour tree, it is preferable.

    I am really new to RAIN, so pardon me if I say anaything wrong, but I am using this in my BT and it is working.
    Hope this helps and ask away. We beginner are in this together.

    #34454

    Jaroslav
    Participant

    You should create a variable in the AIRig memory and set it using:
    AIRig rig = GetComponentInChildren<AIRig>();
    rig.AI.WorkingMemory.SetItem<T>(string name, <T> value);
    In this case an isDead bool would be appropriate.

    Then in the enemy behaviour tree, have a component that checks if the condition is false – behave normaly as an enemy does, but if the bool is true – animate death. Set the animation through the behaviour tree, it is preferable.

    I am really new to RAIN, so pardon me if I say anaything wrong, but I am using this in my BT and it is working.
    Hope this helps and ask away. We beginner are in this together.

    Yep, It’s working Thanks for the point about setting the animation through BT, I absolutely forgot about that!

    #34464

    prime
    Keymaster

    Good response from @Moloch.

    One of the techniques we often use is to create a Custom Element in RAIN for managing the interface between game code and AI code. If you’re interested, there are a bunch of great examples here:
    https://www.assetstore.unity3d.com/en/#!/content/23526

    Custom Elements are very easy to create. Here’s a template. You can add variables that are exposed through the RAIN editor, plus you can add code at various steps in the AI processing (AIInit, BodyInit, Start, Pre, Think, Act, Post).

    using UnityEngine;
    using System.Collections;
    using RAIN.Core;
    using RAIN.Serialization;
    [RAINSerializableClass, RAINElement("Your element name here")]
    public class YourCustomElementClass : CustomAIElement 
    {
        [RAINSerializableField(Visibility = FieldVisibility.Show, ToolTip = "Your tooltip")]
        private _someVisibleVariable;
        public override void Act()
        {
            base.Act();
            //Add some custom code here
        }
    }
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.