- Home /
Custom Event System, Coroutines, Callbacks?
I'm trying to call events from various circumstances.
I have a custom EventManager set up from a tutorial. It's working fine; I call the first event when the player enters a Trigger, which causes an NPC to approach him and say something. The problem is I want to call the second event when the player reaches a certain distance from the NPC, but I'm not sure how to go about it. I've tried a coroutine but I'm very new to them so I'm not sure if it's the best way to go about this. I'm also not sure how I would go about adding a function callback so I can trigger the desired event from the coroutine. Can anyone help me?
I'm not sure which code would be helpful, I've added my Events code below, if there's anything else you'd like to see just let me know and I'll add it here.
public class Events : MonoBehaviour {
private UnityAction event01;
private UnityAction event02;
public Transform player;
public CompanionAI Laurelai;
void Awake()
{
event01 = new UnityAction(Event01Function);
event02 = new UnityAction(Event02Function);
}
void OnEnable()
{
EventManager.StartListening("Event01", event01);
}
void OnDisable()
{
EventManager.StopListening("Event01", event01);
}
IEnumerator WaitForDistance(Vector3 a, Vector3 b, float distanceToCheck) //function callBack?
{
while(Vector3.Distance(a, b) < distanceToCheck)
{
yield return null;
}
Debug.Log("Distance reached");
//I thought of adding some kind of function callback here so I could call a function to trigger the next event, but I don't know how to go about it.
}
void Event01Function()
{
//Debug.Log("Event01 called");
Laurelai.followTarget = player;
Laurelai.LookAtPlayer(true);
Laurelai.isFollowing = true;
Laurelai.Talk();
EventManager.StartListening("Event02", event02);
StartCoroutine(WaitForDistance(Laurelai.transform.position, player.position, Laurelai.followDistance));
//EventManager.TriggerEvent("Event02");
}
void Event02Function()
{
Laurelai.Talk();
}
}
Maybe I'm going about the whole thing the wrong way? Thanks for any help anyone can give me!
Answer by SaganTucker · Jul 10, 2016 at 12:06 AM
Found a solution!
Took me a while of experimentation, but the problem was my coroutine. I didn't really understand how they worked. It was checking the same values over and over, derp.
I changed it to check the updated values each time. I've also added a coroutine to check if the audio source has finished playing. Then I added another coroutine to manage the whole Event Scenario. I feel like I've become a lot more comfortable with coroutines!
Here's my new class:
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
public class Event01 : MonoBehaviour {
private UnityAction event01;
public Transform player;
public CompanionAI Laurelai;
void Awake()
{
event01 = new UnityAction(Event01Function);
}
void OnEnable()
{
EventManager.StartListening("Event01", event01);
}
void OnDisable()
{
EventManager.StopListening("Event01", event01);
}
void Event01Function()
{
EventManager.StopListening("Event01", event01);
//Debug.Log("Event01 called");
StartCoroutine(LaurelaiSequence01());
}
IEnumerator LaurelaiSequence01()
{
Laurelai.followTarget = player;
Laurelai.LookAtPlayer(true);
Laurelai.isFollowing = true;
Laurelai.SayLine(0);
yield return StartCoroutine(WaitForEndOfAudioClip(Laurelai.audiosource));
yield return StartCoroutine(WaitForOutOfRange(Laurelai.transform, player, Laurelai.followDistance + 1));
Laurelai.SayLine(1);
yield return StartCoroutine(WaitForEndOfAudioClip(Laurelai.audiosource));
yield return StartCoroutine(WaitForOutOfRange(Laurelai.transform, player, Laurelai.followDistance + 1));
Laurelai.SayLine(2);
yield return StartCoroutine(WaitForEndOfAudioClip(Laurelai.audiosource));
//EventManager.TriggerEvent("Event02");
}
IEnumerator WaitForEndOfAudioClip(AudioSource audiosource)
{
while (audiosource.isPlaying)
{
yield return null;
}
Debug.Log("Audio Ended");
}
IEnumerator WaitForOutOfRange(Transform a, Transform b, float range)
{
while (Vector3.Distance(a.position, b.position) < range)
{
yield return null;
}
Debug.Log("Out of range");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612084258im_/https://answers.unity.com/themes/thub/images/avi.jpg)