News Forums RAIN General Discussion and Troubleshooting Getting Entity from an outside script

This topic contains 17 replies, has 3 voices, and was last updated by  daedalus29 1 year, 8 months ago.

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #33511

    Valon
    Participant

    Hi,

    I am using RAIN (which is awesome) to get my AI done for a game about stealth.

    For this game my character can hide in closets and stuff like this (like in outlast) and for this purpose I have a script not related to Rain at all, the problem I have right now is that the AI always spots me.

    I would like to temporaly disable the entity attached to my character or any other thing that could make my character invisible to the AI.

    I tried multiple things with “Entity” and “EntityRig”. I managed to create some and other things but I can’t figure out how I can access a specific one that is attached to the gameobject which is my character and then do operations on it.

    #33525

    prime
    Keymaster

    Let’s say you have the game object for you character (the one on which you want to disable entity/aspects).

    GameObject myCharacter; 
    //... myCharacter gets assigned somehow
    EntityRig tRig = myCharacter.GetComponentInChildren<EntityRig>();
    tRig.Entity.IsActive = false;

    That should do it…

    #33529

    Valon
    Participant

    Oh ok, I can use the classic stuff to find Rain objects. I thought I had to use specific functions.

    Thanks a lot.

    #33537

    prime
    Keymaster

    RAIN separates the AI functionality from the Unity component. So there are a number of “Rigs” that are Unity Monobehaviours you can search for: AIRig, EntityRig, NavigationTargetRig, WaypointRig. NavMeshRig

    The rigs support editing via the Unity Editor, and provide linkage to the main Unity methods like Awake, Start, Update, LateUpdate.

    You can search for any of the rigs via GetComponent or GetComponentInChildren. Then you can access the AI “element” from the rig (e.g., tRig.Entity)

    #33795

    Valon
    Participant

    Hi again excuse me but I have a new question, actually to do the almost the opposite.

    I have a variable called : “isDead” in a script called bad guy. The script is attached to the Soldier who has the AI a a child.

    I want my AI to have a constraint so he does his thing while he his alive and stops when he is dead so I did this in a Rain script (ActionResult function to be exact) :

    if(BadGuy.isDead)
    	ai.WorkingMemory.SetItem<bool> ("Dead", true);
    else
    	ai.WorkingMemory.SetItem<bool> ("Dead", false);

    If I manually change Dead it works and my “isDead” variable works too, it’s just the part where I use the “isDead” to change “Dead” value that doesn’t work.

    I don’t know what’s wrong with that, I get no errors.

    • This reply was modified 2 years ago by  Valon.
    #33799

    prime
    Keymaster

    I don’t know what’s wrong either, except if that code never executes.

    #33802

    Valon
    Participant

    I did put a debug into the function ActionResult and the debug is seen. But the variable “isDead” is not seen because the “if” never execute doesn’t matter if I put “isDead” at true or false.

    #33804

    prime
    Keymaster

    can you post the entire script file?

    #33806

    Valon
    Participant

    This is the Rain script :

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Core;
    using RAIN.Action;
    using RAIN.Navigation;
    using RAIN.Navigation.Graph;
    [RAINAction]
    public class DeathScript : RAINAction
    {
        public DeathScript()
        {
            actionName = "DeathScript";
        }
        public override void Start(AI ai)
        {
            base.Start(ai);
        }
        public override ActionResult Execute(AI ai)
        {
    		Debug.Log ("In Script");
    		if (BadGuy.isDead) {
    			Debug.Log ("here");
    			ai.WorkingMemory.SetItem<bool> ("Dead", true);
    				}
    		else
    			ai.WorkingMemory.SetItem<bool> ("Dead", false);
            return ActionResult.SUCCESS;
        }
        public override void Stop(AI ai)
        {
            base.Stop(ai);
        }
    }

    This is the script attached to the gameobject who has the AI :

    using UnityEngine;
    using System.Collections;
    public class BadGuy : MonoBehaviour {
    	 bool iSeeYou = false;
    	public static bool isDead;
    	float BadGuyHP = 100;
    	void Update()
    	{
    		if (BadGuyHP <= 0) {
    				isDead = true;
    				Debug.Log("Dead");
    				}
    				else 
    				isDead = false;
    	}
    	void OnTriggerStay (Collider col)
    	{
    		if (col.gameObject.tag == "Knife") 
    		{
    			if(iSeeYou && ObjectGestion.HitKnife)
    			{
    				BadGuyHP -= 20;
    			}
    			else if (!iSeeYou && ObjectGestion.HitKnife)
    				BadGuyHP -= 100;
    		}
    	}
    }

    I feel like I did something really stupid because I really can’t figure out what’s the problem.

    #33816

    prime
    Keymaster

    Still not clear to me. It does seem strange that isDead is static. You only have one bad guy in the scene? This line never runs? Debug.Log (“here”);

    #33823

    Valon
    Participant

    Right now yes but you are right I need to change that for the future.

    And yes this is it, the line Debug.log(“here”); never runs.

    #33824

    Valon
    Participant

    I actually just noticed that I have almost the exact same thing with another variable and it works fine.I really don’t know what’s up, it might be somewere else completly.

    #33838

    Valon
    Participant

    Hi,

    Me again, sorry I hope it’s not too annoying. I managed to solve my weird problem who indeed had not much to do with RAIN (it might tho because it was about removing the static keyword) but now I think I have a more legit problem with RAIN.

    I am still doing the same thing (death of the bad guys) and I set everything up so they each have their own HP their own isDead variable which works fine. The problem then is that I get “isDead” like this :

    GameObject Character = Find.GameObject("Soldier");
    	BadGuyHP myScript;
    ....
    myScript = Character.GetComponent<BadGuyHP>();
    ....
    myScript.isDead

    My big problem is with the first line, all of my soldiers have exactly the same name + same scripts + same BT.
    What I did for the rest in Unity is I put variables public and then I dragged and dropped objects so each script knows what Soldier they deal with.

    Apparently I can’t do that with RAIN, if I do :

    public GameObject Character;

    It doesn’t bug but I have nowhere to drag and drop my gameobjects.

    Is there a way around that or will I have to use a dictionnary or something like that?

    #33841

    prime
    Keymaster

    Let me make sure I understand. Each of your characters has both a BadGuy script and a RAIN AI on it, yes? And you are just trying to get the two to talk to each other?

    If so, you have 2 options:

    1) In the RAIN custom action Start, just do

    myScript = ai.Body.GetComponent<BadGuyHP>();

    2) Don’t use a RAIN custom action at all. In the BadGuy script:

    #using RAIN.Core;
            AIRig rig = null;
            void Start()
            {
                    rig = gameObject.GetComponentInChildren<AIRig>();         
            }            
    	void Update()
    	{
    		if (BadGuyHP <= 0) {
    				isDead = true;
    				Debug.Log("Dead");
    				}
    				else 
    				isDead = false;
    		rig.AI.WorkingMemory.SetItem<bool>("Dead", isDead);
    	}
    #33845

    Valon
    Participant

    Thanks it works perfect. Damn I spent two days for this. I sometimes whish I was a good programmer haha.

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic.