Emerald AI Integration

magique

Active member
I previously had Emerald AI integration working with old TPC, but now the code that I had for DamagePlayer in Emerald_AI script will not work due to namespaces, hierarchy of components, etc. What would be the equivalent code for the new TPC?

C#:
if (CurrentTarget != null && CurrentTarget.GetComponent<Opsive.ThirdPersonController.Health>())
        {
            CurrentTarget.GetComponent<Opsive.ThirdPersonController.Health>().Damage(CurrentDamageAmount, transform.position, Vector3.zero, 0f);
        }
 

magique

Active member
OK, I got the AI to damage the player, but now I have 2 issues. First, after applying damage to the Health trait that takes the player under 0, the player doesn't die. I would have thought that would have occurred automatically, but it doesn't. What should I do to insure the player dies when the health is below zero?

Second, when I modified my script for the player to damage the AI, it crashes. Previously I had to provide the name of a Damage callback function in the weapon Impact Options, but that no longer seems to exist. I found code in the Melee script that calls OnObjectImpact so I modified my code to use that named function. Here is the code:

Code:
public class OpsiveBridge : MonoBehaviour
{
    private Emerald_AI _emeraldAI;
 
    private void Awake()
    {
        _emeraldAI = GetComponent<Emerald_AI>();
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject>(this.gameObject, "OnObjectImpact", OnObjectImpact);
    }
 
    private void OnObjectImpact(float amount, Vector3 point, Vector3 normal, GameObject originator)
    {
        if (_emeraldAI != null)
        {
            _emeraldAI.Damage((int)amount, Emerald_AI.TargetType.Player);
            Debug.Log(this.name + " took " + amount + " damaged at location " + point + " with normal " + normal + " from object " + originator);
        }
 
    }
}


However, when I shoot it with the gun, it just crashes with the following error:

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.Events.EventHandler.ExecuteEvent[Single,Vector3,Vector3,GameObject,Collider] (System.Object obj, System.String eventName, Single arg1, Vector3 arg2, Vector3 arg3, UnityEngine.GameObject arg4, UnityEngine.Collider arg5) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/EventHandler.cs:535)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.HitscanFire (Single strength) (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:906)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.Fire (Single strength) (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:692)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.UseItem () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:621)
Opsive.UltimateCharacterController.Character.Abilities.Items.Use.LateUpdate () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/Use.cs:580)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.LateUpdateActiveAbilities (Opsive.UltimateCharacterController.Character.Abilities.Ability[] abilities, System.Int32& abilityCount) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:843)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.LateUpdateUltimateLocomotion () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:831)
Opsive.UltimateCharacterController.Character.CharacterLocomotion.Move (Single horizontalMovement, Single forwardMovement, Single deltaYawRotation) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/CharacterLocomotion.cs:476)
Opsive.UltimateCharacterController.Game.DeterministicObjectManager+DeterministicCharacter.FixedMove () (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:146)
Opsive.UltimateCharacterController.Game.DeterministicObjectManager.FixedUpdate () (at Assets/Opsive/UltimateCharacterController/Scripts/Game/DeterministicObjectManager.cs:590)

So, I’m not quite doing something right, but not sure what. This is basically the same thing I had before except previously I had to specify the name of the event and then I registered for that event. Maybe OnObjectImpact is not the correct event, but I can’t find anything else yet.

Thanks
 

Justin

Administrator
Staff member
First, after applying damage to the Health trait that takes the player under 0, the player doesn't die. I would have thought that would have occurred automatically, but it doesn't. What should I do to insure the player dies when the health is below zero?
You'll want to make sure you have the Die or Ragdoll abilities added.

Second, when I modified my script for the player to damage the AI, it crashes. Previously I had to provide the name of a Damage callback function in the weapon Impact Options, but that no longer seems to exist. I found code in the Melee script that calls OnObjectImpact so I modified my code to use that named function. Here is the code:
The callback has changed in version 2. Take a look at this page: https://opsive.com/support/document...roller/items/actions/usable/shootable-weapon/
 

magique

Active member
Excellent. Thanks for the information. I did a search for OnObjectImpact, but the search feature couldn't find it. Not sure why not. I'll give these a try and let you know if I get it working.
 

magique

Active member
I got this all working now. When I get a chance I will post the instructions for integrating Emerald AI so others can use it.
 

magique

Active member
OK, here is the integration instructions. I think I remembered to include everything, but I threw it together rather quick so some points might not be as clear as they could be. If anyone tries this and has issues, let me know and I will try to help.
 

Attachments

  • Opsive_V2_EmeraldAI_Intgration.pdf
    59.1 KB · Views: 284

Sandninja

New member
OK, here is the integration instructions. I think I remembered to include everything, but I threw it together rather quick so some points might not be as clear as they could be. If anyone tries this and has issues, let me know and I will try to help.

Works Awesome!
Thanks Magique!
 

flink

New member
OK, here is the integration instructions. I think I remembered to include everything, but I threw it together rather quick so some points might not be as clear as they could be. If anyone tries this and has issues, let me know and I will try to help.

Thanks!
 

GearedGeek

Active member
OK, here is the integration instructions. I think I remembered to include everything, but I threw it together rather quick so some points might not be as clear as they could be. If anyone tries this and has issues, let me know and I will try to help.

I would like to thank you very much for providing this tutorial integrate Emerald AI with Opsive! :love:
 

DocQ

Member
Any of you guys using Emerald, are you using the built in UI in the package for the agent you are fighting? and does it work? Health get updated. etc?
 

magique

Active member
Any of you guys using Emerald, are you using the built in UI in the package for the agent you are fighting? and does it work? Health get updated. etc?
What do you mean by "built-in UI for the agent you are fighting"?

I've used Emerald with Opsive and the health gets updated and all works fine.
 

DocQ

Member
yes, I was referring to the one that comes with for the AI agents in Emerald, thanks for your reply.
 

DocQ

Member
Just a couple of questions to clarify, when you say add the first script to your emerald AI, you are referring to the emerald AI object, as in the enemy, bad guy, etc? and the 2nd part to modify the Emerald_AI script, does it matter if you put it before or after the
if (CurrentTarget != null && CurrentTarget.GetComponent<PlayerHealth>() != null){
CurrentTarget.GetComponent<PlayerHealth>().DamagePlayer((float)CurrentDamageAmount);
?
like at line 2988 or earlier?
 

magique

Active member
Just a couple of questions to clarify, when you say add the first script to your emerald AI, you are referring to the emerald AI object, as in the enemy, bad guy, etc? and the 2nd part to modify the Emerald_AI script, does it matter if you put it before or after the
if (CurrentTarget != null && CurrentTarget.GetComponent<PlayerHealth>() != null){
CurrentTarget.GetComponent<PlayerHealth>().DamagePlayer((float)CurrentDamageAmount);
?
like at line 2988 or earlier?

Yes, for your first question. Attach the OpsiveBridge script to your bad guy.

For the second question, the code to be added can be the only code in the DamagePlayer function if you so desire. It all depends on whether or not you need the default code. If you do then you can place the new code above or below the old code. It doesn't matter.
 

DocQ

Member
just wanted to get it straight, trying to figure out what I might have done wrong, because when I change the tag of the capsule collider under my character from untagged to player, it throws my camera way up in the air and I can no longer control my player. I am using 2018.2.19 in case that matters. Any idea's?
 

magique

Active member
just wanted to get it straight, trying to figure out what I might have done wrong, because when I change the tag of the capsule collider under my character from untagged to player, it throws my camera way up in the air and I can no longer control my player. I am using 2018.2.19 in case that matters. Any idea's?
Is the Capsule collider layer set to Character?

I don't know what the issue could be off hand. I don't use 2018 Unity so not sure if there is an issue there. I'm using 2017.1.
 
Top