Help with understanding events

Larsson24

Member
Hi, I'm having troubles understanding the event system and was hoping someone could help me understand it. I have read the documentation and read the older forum posts.

This is what i might understand plus some questions (main questions in red for clarity):
- My script needs to listen for events by first registering to it, by using the EventHandler.RegisterEvent<int>(object obj, string name, Action<int> handler).
- Object obj = Explained in another post as "the object that should receive the event". Does this mean the game object that the script is on and how come all examples I see on this just use "gameObject" without declaring which game object this variable is? Or is "gameObject" some kind of way of saying basically this game object (the game object that the script is on)?
- string name = the event name that can be found here: https://opsive.com/support/document...ller/programming-concepts/events/event-names/
- Action handler - the function that should run when the event have been executed.
- <int> = Is this the parameters that should be recieved by the event and then pass trough to the function (lin with action handler)? Basically, should I just use all the parameter types stated on the Event Names site (linked above) for that specific event? However, for OnUseAbilityStart the following parameter and variable is stated, "Use useAbility". I get error if I include the "Use" within the <>. Do I need to add something more than "using Opsive.Shared.Events;" or is it something else i'm missunderstanding?
- My function that should run should have the same parameters but with variables inside the ().

Testing
I managed to get this working for the event "OnCharacterMoving". However, I cant get it to work on OnStateChange or OnUseAbilityStart.

Here's the example of OnStateChange that doesnt work.

-----------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opsive.Shared.Events;

public class OpsiveChangeState : MonoBehaviour
{

public void Awake()
{
EventHandler.RegisterEvent<GameObject, string, bool>(gameObject, "OnStateChange", OnStateChange);
}

private void OnStateChange(GameObject gameObject, string stateName, bool active)
{
//Just printing to see if it's working and to check what the variables returns.
Debug.Log("OnStateChange " + gameObject.ToString() + " " + stateName.ToString() + " " + active.ToString());
}

}

-------------------------------------------
Here is my example for the OnUseAbilityStart that doesnt work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opsive.Shared.Events;

public class OpsiveChangeState : MonoBehaviour
{
public void Awake()
{
EventHandler.RegisterEvent<bool, Use>(gameObject, "OnUseAbilityStart", OnStateChange);
}

// I know the function name is wrong but this is just for testing.

private void OnStateChange(bool start, Use useAbility)
{
Debug.Log("OnStateChange_OK " + start.ToString() + " " + useAbility.ToString());
}

}
 
gameObject is a variable that is defined in the MonoBehaviour script and is a reference to the game object hosting the script. So you should not use it as a variable name in your function definition (as you did in the OnStateChange example).
The data types within <> define the parameters of your receiving function. For the Use data type you need to add another using statement:

using Opsive.UltimateCharacterController.Character.Abilities.Items

You should search the Opsive source code for the different events, as there are examples for most events somewhere.
 
Thank you Christian, for making it a bit more clearer. However, my functions still dont work. I combined the two scripts from above to one and implemented the fixes you mentioned, i.e. added the using line and changed my variables name to not interfer with any other references.

The script is added to the root of the character, a little bit modified test version of Nolan.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Character.Abilities.Items;

public class OpsiveChangeState : MonoBehaviour
{

public void Awake()
{
EventHandler.RegisterEvent<GameObject, string, bool>(gameObject, "OnStateChange", OnStateChange);
EventHandler.RegisterEvent<bool, Use>(gameObject, "OnUseAbilityStart", OnUseAbilityStart);

}


private void OnStateChange(GameObject testGameObject, string testStateName, bool testActive)
{
Debug.Log("OnStateChange_OK " + testGameObject.ToString() + " " + testStateName.ToString() + " " + testActive.ToString());
}

private void OnUseAbilityStart(bool testActive2, Use testAbilityUse)
{
Debug.Log("OnUseAbilityStart " + testActive2.ToString() + " " + testAbilityUse.ToString());
}

}

What is wrong with this code? I get no logs and no errors. :)
 
Make sure, the events are actually fired on that game object. So you should put break points in the code where the events supposed to be triggered.
 
OnStateChange also doesn't get called unless you explicitly enable it on the StateManager.
 
Thank you Justin, I now enabled the Send State Change Event in the the StateManager. I feel like stuff like this should be included in the event documentation to make it easier for noobs like me.
However, the script still don't work. I tried having in on both the root of the character and on the StateManger itself, as I don't know what game object that is sending the event. I'm just trying to get information about when I use the Height Change ability (default Crouch state from the demo character). I assume that the events are firing correctly from the built in scripts.
 
You should place your script on the character as all of the events are listening based on the first parameter, the gameObject reference. The HeightChange ability does not fire the OnStateChange or OnUseAbilityStart event so that may be why you aren't receiving the events. You should instead subscribe to OnCharacterAbilityActive, and this page has an example: https://opsive.com/support/documentation/ultimate-character-controller/character/abilities/
 
You should place your script on the character as all of the events are listening based on the first parameter, the gameObject reference. The HeightChange ability does not fire the OnStateChange or OnUseAbilityStart event so that may be why you aren't receiving the events. You should instead subscribe to OnCharacterAbilityActive, and this page has an example: https://opsive.com/support/documentation/ultimate-character-controller/character/abilities/
Thanks, that fixed it! ?
 
Top