News › Forums › RAIN › General Discussion and Troubleshooting › Custom action class to trigger other script?
This topic contains 30 replies, has 3 voices, and was last updated by Aaron Mueller 2 years, 6 months ago.
October 31, 2022 at 9:44 am #4880
I’ve been trying to work this out for a little while.
I have a character that when it detects an enemy within a range will turn and run a ‘shoot’ animation.
However I also need to run the script to set off the particles (tracer effects) the raycast (which sends a message to the enemy about health and also puts impact particles on).
Is there a simple way to do this?October 31, 2022 at 1:35 pm #4881
Seems like you answered the question in your post title. Create a Custom Action that runs whatever logic you need. Put your logic in the Execute method, and use it to trigger whatever other games scripts you have for particles, etc. You would probably put it in a parallel with the shoot animation, but it would just run once (not loop).October 31, 2022 at 3:16 pm #4883
Ah ok, I’m being dense, but how does it call the other script?
I guess the script it calls can just loop and then send another call to stop it once it leaves the shoot animation in the BT.October 31, 2022 at 4:59 pm #4884
Custom Actions are created in the BT Editor using Create->Action->Custom Action. You can add your own script by choosing the Action from the Class pulldown field and selecting Create Custom Action. You can then specify a name and a scripting language, and a default script will be added for you and linked to the behavior tree node.
From there, fill out the Execute method of the Custom Action. Your action will behave like any other behavior tree node, so make sure you return Success, Failure, or Running from your Custom Action code as appropriate. In your case, do whatever logic you need and then return ActionResult.SUCCESS, which essentially means “I’m done and don’t loop”October 31, 2022 at 5:23 pm #4885
Thanks for that, the note on ensuring Success, Failure, or Running is really handy.
However I actually meant; what within the custom script itself messages to the other script on a child object to the AI itself so that part of the child’s script runs?
Sorry to be a pain.October 31, 2022 at 9:42 pm #4899
I’ve been looking around and it seems that there might not be a way for RAIN to interact with the rest of unity as far as scripts go?
So, there would not actually be a way to set another script, event or whatever elsewhere with a Custom action.
Is that right or am I missing something?October 31, 2022 at 10:09 pm #4900
Redofpaw, Not sure if this will help you out, but for example, in my current project I have a character that senses the player and then moves to a new location where he retrieves an object and then moves to the next location. Then he opens the door of a vehicle and places the object inside (no animations to support making it look like he's taking these actions, but hopefully you get the idea). These custom actions actually have to call methods that exist in other scripts which are com- ponents on other objects. For example, the player can click on a button to open and close the door of the vehicle. The NPC doesn't have the mouse and keyboard interface to use, so I have to be able to trigger the same behavior (Open / Close door, Pick up / Drop item) in script code. Ultimately, I use a combination of the AI.WorkingMemmory with a custom AIParameters script which is attached to the NPC (a component attached to the AI.Body effectively). So, in AIParameters script, I have public variables that you can drag items into (GameObjects) and also animations. I am using the array of GameObjects to act as a list of places (almost Waypoints in a way). When my NPC senses the Player Aspect the Behavior Tree sets him in motion to that first location. I grab the  element and store that game object in the NPC's WorkingMemory: agent.WorkingMemory.SetItem<GameObject>("destination", go); Then he uses that GameObject's transform to find a path there. * NOTE: I am using Unity's NavMeshAgent to walk on specific custom navigation meshes at the moment. Once he gets there, he grabs the item and then moves to the next location (near the vehicle), opens the door, drops the item and then chooses a random idle animation to play. He doesn't go anywhere after that for the moment. Not sure if that helps. But I hope that gives you an idea of some of what you can do. Good luck! P.S. Sorry for the preformatted code. I can't stand that all CR/LF are returned in this and the <br> html tag seems to be ignored.
October 31, 2022 at 10:15 pm #4903
- This reply was modified 2 years, 6 months ago by Aaron Mueller.
- This reply was modified 2 years, 6 months ago by Aaron Mueller.
Not sure really. That seems to be something ‘outside’ the Behaviour Tree driving the BT. What I need is for the detected entity in the BT to drive animation (Mecanim Move) and also set off the particles and other behaviours related to the gun.
Does that make sense?October 31, 2022 at 10:20 pm #4904
This is exactly what Custom Actions are used for in FPS Control. The Custom Action node calls into your associated custom action script at runtime whenever the node is executed. Your custom script can do whatever it wants, including particles, firing the gun, affecting health, whatever. It’s your code, so there really aren’t any limitations on what you can do there.October 31, 2022 at 10:30 pm #4907
Well, I’m not 100% sure. I’m trying to understand your needs and was trying to relate that to my experiences so far.
In your case, if I understand correctly, when your NPC detects the enemy, he turns to face it and then shoots. Is that the desired outcome?
So, once your NPC turns and is now facing the enemy, you just create a Sequencer that play Aim animation, play Shoot animation, turn on particle system, fire off a delegate event to send the damage information to the enemy if the bullet collides (this could probably exist completely outside your BT anyway). You can probably combine some of them into one custom action.
The bottom line is, I believe you can do all that you need, but I guess the implementation details are more up to you. If you can’t give away too much about your project, perhaps an email to support@rivaltheory is in order. Hope that is of some help.October 31, 2022 at 10:48 pm #4908
Thanks Prime, makes sense now! Just a beginner with scripting with unity and also of course RAIN so sometimes get a bit lost. What you say makes sense.
Aaron; You are absolutely right The problem is the implementation. The particle system is attached to the gun, which is a child of the Character which has the AI. This gun has all the necessary elements to Instantiate the hit particles, send message removing health from the enemy etc.
My inexperience is leaving a gap of logic between the custom action and how that affects the other script of the child object of the AI character.
Don’t mean to be a pain, and you guys are being very patient!
I’m happy to send the entire project to you if that would helpOctober 31, 2022 at 10:57 pm #4910
Now I could add these parts to the custom action, but I’d have to figure out a way to add the particles to the object at the end of the gun from there and then…. well, I have no idea. Open to any thoughts.November 1, 2022 at 10:44 am #4922
So, it was as Prime stated, you can indeed just script whatever you want. It was just a case of finding the game object (with GameObject.Find) and sending it a message (transform.SendMessage) to run the relevant part of the script.
This all comes down to my lack of experience with scripting as all this seems rather straight forward and simple stuff for coding in Unity. I suspect it was so simple you guys couldn’t understand how I could miss it!
Thanks again for your patience.November 1, 2022 at 1:06 pm #4923
Glad you got it sorted out. Just be aware of using GameObject.Find can get a bit expensive so use it sparingly and cache the results (just store the value in a variable in your script so each time you want to start that behavior you don’t have to search for the component or child object you want).
I guess that’s what I was trying to say in my original post, as long as you can get a reference to the component (in my case that AIParameter script, or the Mecanim Animator component, you should be able to access any public properties, methods, etc.
Don’t get me wrong, I’m far from being an expert. I have a lot to learn myself.November 1, 2022 at 2:49 pm #4929
Thanks Aaron. Only thing now…. how do I cache?
Found this - is it what I need?
If you can point me in the right direction that would be great.
Also, how are you with custom action actions in general on the new RAIN?
Would you be able to have a look at this?
In short: Want to script in the custom action for the object to move away from the detected variable/aspect.
You must be logged in to reply to this topic.