- Home /
Using Listeners for Sequences and Different Classes
I have three classes of C# things. One is a PuzzleController, another is a CameraGlide, and the last is a CameraRotate. Just a quick summary of what the 3 classes are. Controller has all the puzzle variables, trigger actions, stuff to send to UI, and controls what state the puzzle is in. CameraGlide moves the camera from one point to another while looking at a point. CameraRotate allows the player to mouse drag the mouse around a point and zoom in/out.
What I want to do is this:
-Player walks into trigger
-Camera disables 3rdPersonCamera
-Camera glides from behind player to predetermined puzzle location
-Tells the Controller when the camera has reached the puzzle position
-PuzzleController turns off the glide and turns on the Rotate
-Player out of trigger
-Turn off CameraRotate
-CameraGlides from where it is to the player
-Tells the controller when the camera has reached the player position
-Turn back on 3rdPersonCamera
The problem is the sequence of events. CameraGlide and CameraRotate should be PuzzleController agnostic (there different classes of controllers at different levels). So I don't want to hard code anything into Glide Update() something like:
"if(at_loc) Puz_Con_A.glideDone()".
Because now this Glide class only works with the one type of controller. Similarly I don't want a constantly running update in the PuzzleController checking the status of the Glide like:
"if(glide.at_loc =true)".
Since this would be constantly checking when it only needs to be when the player has triggered the puzzle.
Ideally I want to use some kind of Delegate/Listener that any PuzzleController can add to their own code so that when it triggers it tells the CameraGlide, and listens for CameraGlide to send back that it's at the location. The problem is I'm terrible at Listeners and I'm not sure how to add it while keeping CameraGlide and CameraRotate agnostic.
Answer by tyruji · Jul 24, 2021 at 03:13 PM
You definitely might want to check out UnityEvents, as of right now you are going in a direction in which every class will have to know about each other just to subscribe to some events, and by using UnityEvents they won't have to know about each other and your code can stay clean. e.g. you said there is a trigger so let's assume this class:
using UnityEngine;
using UnityEngine.Events; // important to use UnityEvents
class Trigger : MonoBehaviour
{
// ...
[SerializeField] private UnityEvent _OnPlayerEnter = null;
private void OnTriggerEnter( Collision col ) // or whatever you want
{
Player p;
// ... your logic for player checking or whatever ...
_OnPlayerEnter?.Invoke(); // call the event - the player entered
}
}
Then you can subscribe various stuff to the event and have everything smoothly organised. UnityEvents can handle:
No parameter public methods
Public methods with 1 parameter
Setting a public field to a given value
Using them is pretty intuitive, so I bet you'll get the hang of it. Good luck!
Okay but how do I add it to do things and wait?
So I make a UnityEvent _event in the PuzzleController right? Then when the puzzlecontroller gets the trigger I need to tell it to start the cameraGlide and then WAIT. When the cameraGlide is at the location, the puzzle controller needs to turn on the CameraRotate.
This is what I'm confused on, how is cameraGlide supposed to Invoke the next event? Because I can't Invoke cameraRotate until the glide has completed.
You could make a public method in "cameraGlide" that starts it's logic and put another UnityEvent "_OnLocationReached" (or whatever you want to name it) in your "CameraGlide" class, and you could just drag in the CameraRotate to turn it on.
Your answer
![](https://koobas.hobune.stream/wayback/20220613040739im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add a function from another class to a Slider's OnValueChanged with AddListener()? 1 Answer
Button Listener Takes Reference But I Want to Pass Value? 1 Answer
Sequence of methods stored in the list with parameters 1 Answer
UI toggle.onValueChanged assigning method via script 1 Answer
How can I animate a death sequence for the FPS controller? 1 Answer