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.
-
AuthorPosts
-
October 26, 2022 at 12:48 pm #33511
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.
October 27, 2022 at 6:40 am #33525Let’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…
October 27, 2022 at 9:10 am #33529Oh ok, I can use the classic stuff to find Rain objects. I thought I had to use specific functions.
Thanks a lot.
October 27, 2022 at 2:04 pm #33537RAIN 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)
October 30, 2022 at 3:10 pm #33795Hi 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.
October 30, 2022 at 4:02 pm #33799I don’t know what’s wrong either, except if that code never executes.
October 30, 2022 at 4:17 pm #33802I 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.
October 30, 2022 at 5:15 pm #33804can you post the entire script file?
October 30, 2022 at 5:26 pm #33806This 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.
October 31, 2022 at 5:53 am #33816Still 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”);
October 31, 2022 at 6:07 am #33823Right 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.
October 31, 2022 at 7:23 am #33824I 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.
November 1, 2022 at 2:57 pm #33838Hi,
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?
November 1, 2022 at 3:22 pm #33841Let 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); }
November 1, 2022 at 4:11 pm #33845Thanks it works perfect. Damn I spent two days for this. I sometimes whish I was a good programmer haha.
-
This reply was modified 2 years ago by
-
AuthorPosts
You must be logged in to reply to this topic.