How can I access RAIN from a Monobehaviour script?« Back to Previous Page

Hello, I've like to attach a Monobehaviour script to my AI enemy so that I can do things like deal damage and manage a few other variables. How can I attach a Monobehaviour script to my Enemy AI so that through the Monobehaviour script, I can pass the AI variables. I recall the previous way was getting reference to the RAINAgent and using methods such as ai.Agent.actionContext.SetContextItem("damage", 0); but I don't think this work any more with your new updated system.
Posted by justinliebregts (Questions:19, Answers:40)
Asked on October 25, 2022 4:34 am
0

There is a great tutorial here demonstrating a really basic way to set up the events system:

https://www.youtube.com/watch?v=ihIOVj9t0_E

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 28, 2022 11:00 pm #
0

You probably know but the Wiki section for that is completely blank. Thanks!

http://rivaltheory.com/wiki/doku.php?id=ai:extension:start

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 28, 2022 8:38 pm #
0

Ahhh thank you so much.

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 28, 2022 8:37 pm #
0

Once you have created a CustomAIElement, go to the Extensions tab on your AIRig (the one that looks like a pencil.) There you will see a pulldown list with the label ”Add Custom”. You should see your CustomAIElement in that list. Just select it from the list to add it to your AI. It will automatically start receiving the various callbacks at runtime.

Posted by prime (Questions:2, Answers:127)
Answered On October 28, 2022 8:35 pm #
0

Thanks Prime. For some reason I think I’m missing something… How do I use the CustomAIElement? I cannot attach it to my Enemy AI object because it’s not derived from a Monobehaviour script, and in the Behaviour Tree, the custom action node doesn’t allow me to reference this class so that’s not how to use it. Is there some documentation (besides the API) that explains how to apply this script to my AI?

Sorry for all the questions. Thanks!

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 28, 2022 8:31 pm #
0

Custom AI Element is here in the API: http://rivaltheory.com/api/?topic=html/8cc48a4a-ab31-8850-a1cb-627d4121005d.htm

If you want to get access to it from a MonoBehavior, it would be AIRig.AI.CustomElements - the search for the element you want. **The return value of CustomElements is currently an array. That will be changing to an IList<CustomAIElement> in the next update (the array was an error.)

Or if you use the script I posted as a starting point - you would do things in reverse and pull information out of the component attached to ”attachedGameObject”.

Posted by prime (Questions:2, Answers:127)
Answered On October 25, 2022 7:13 pm #
0

Would you be able to provide a really basic Monobehaviour script that has a Monobehaviour interact with a CustomAIElement script? I’ve been experimenting for a while with no result. I couldn’t even find CustomAIElement in the API documentation on your website. The API docs are a bit difficult to browse with those scrollable frames and no search field.

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 25, 2022 6:18 pm #
0

Yes, Unfortunately I’m using the Ultimate FPS kit at the moment and it uses messages to pass information around. I’d typically prefer to use an alternative method as well.

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 25, 2022 6:00 pm #
0

Thanks! Yes, #3 seems to be the best solution. So just to confirm, there’s no way to communicate directly with the AI via normal Monobehaviour script. You have to go through a CustomAIElement. Correct?

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 25, 2022 5:58 pm #
0

I’ll also mention that I strongly dislike Unity’s ”message” system. Not only is it hard to manage and write good code for, but it is also slow, relying on C# reflection. I’ve never used it on any of my projects when I wasn’t forced to by someone else’s code.

Posted by prime (Questions:2, Answers:127)
Answered On October 25, 2022 5:56 pm #
0

Lots of ways to do this, depending on how much work you want to do:

1) You could just call a Damage method on your custom element directly. This would mean your code would have to locate it and make the call.
2) You could do as you suggested, except I might have the CustomAIElement poll for information on the GameObject on every Think call.
3) You could set up events (either C# events or regular callbacks using delegates) that are raised by your damage code. The custom element would register as a listener and then receive the events when they are raised.

So far I’ve opted for #3 in projects I’ve worked on.

Posted by prime (Questions:2, Answers:127)
Answered On October 25, 2022 5:54 pm #
0

ps - Your Q&A system looks similar to the StackExchange format. However, there’s no commenting which seems weird because it forces ever response to be another answer which breaks the usefulness of voting and I think also makes it confusing because everything is marked as being an ”answer”. Also we can’t go back and edit our posts so I’m force to add this after-though as another ”answer”. I love the changes on the website. I just wanted to give further suggestion.

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 25, 2022 5:45 pm #
0

Thanks Prime. This is a great way to get started. However, I have a class that sends targets a ”Damage” message when they get hit. Normally I would attach a mono script to that object with a void Damage(float damageAmount){} and then I can receive the damage call and appropriately do things such as dispatch events and decrement health. However, those messages only seem to be received by mono scripts. Does this mean if I want to have monobehaviour interact with that CustomAIElement script, I’d need another mono script attached to the object that passed this ”damage” information directly to the CustomAIElement script, which then hands that data to the AI system? It seems like an unusual amount of work to have to write two scripts to pass some data. I’m probably missing something here. Please inform! Thank you.

Posted by justinliebregts (Questions:19, Answers:40)
Answered On October 25, 2022 5:41 pm #
0

The right way to do custom code like that in RAIN is to create a CustomAIElement. Simply create a script that inherits from CustomAIElement, then override one or more of the callbacks Pre, Sense, Think, Act, Post, IK, RootMotion.

Other things to know:
add these attributes to your class: [RAINElement] and [RAINSerializableClass]
if you are in C#, add ”using RAIN.Core” and ”using RAIN.Serialization”

should look something like this:


using UnityEngine;
using System.Collections;
using RAIN.Core;
using RAIN.Serialization;

[RAINElement("My AI Element")]
[RAINSerializableClass]
public class MyAIElement : CustomAIElement
{
[RAINSerializableField(Visibility = FieldVisibility.Show, ToolTip = "My Value")]
public float someValue;

[RAINSerializableField(Visibility = FieldVisibility.Show, ToolTip = "Some attached game object")]
public GameObject attachedGameObject;

public override void Act ()
{
//Do something here or in one of the other overrides
base.Act ();
}
}
Posted by prime (Questions:2, Answers:127)
Answered On October 25, 2022 4:17 pm #