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());
}
}
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());
}
}