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.
|
▲ ▼ |
There is a great tutorial here demonstrating a really basic way to set up the events system: |
▲ ▼ |
You probably know but the Wiki section for that is completely blank. Thanks! |
▲ ▼ |
Ahhh thank you so much. |
▲ ▼ |
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. |
▲ ▼ |
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! |
▲ ▼ |
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”. |
▲ ▼ |
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. |
▲ ▼ |
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. |
▲ ▼ |
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? |
▲ ▼ |
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. |
▲ ▼ |
Lots of ways to do this, depending on how much work you want to do: So far I’ve opted for #3 in projects I’ve worked on. |
▲ ▼ |
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. |
▲ ▼ |
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. |
▲ ▼ |
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: should look something like this:
|