✅ Get my courses with discount: 👉 Unity 2D Master: www.udemy.com/course/unity2dmaster/?couponCode=2D-MASTER 👉 Unity Mobile Course: www.udemy.com/course/unitymobilecourse/?couponCode=MOBILE 👉 Unity Input System with Rebinding: www.udemy.com/course/unity-input-system-rebind/?couponCode=UNITY-INPUT
Very straightforward! You describe exactly what the goal is and then outline it using limited code You speak clearly and say only as much as necessary for teaching. Thank you, very good tutorial
Oh man I literally just came from one of your videos where you used event's and I wanted to learn more. Thank you Root Games for this very good and concise tutorial!
Hello, what is the advantage of having a static EventManager (the singleton pattern) instead of the first method which is shown? Anyway thank you for the video, it's going to clean my code a lot I think
Thanks for this very useful tutorial. One thing I don't fully get, is how the EventManager can be used/defined in other scripts. Is it because of the static? What makes its event data accessible from other objects and their scripts?
The "static" keyword marks data that belongs to an entire class and not an object created by that class. That means you can access these methods, variables etc. (the ones marked as "static") simply by typing "ClassName.ThingInClassYouWant". Another example that might help is, when you want to find the square root of a number, the code you type is "Mathf.Sqrt(number)". Mathf is a class and Sqrt() is a static function in that class. You don't need to create an object of class Mathf and then call the Sqrt() function on it. I hope that helps.
Not really, it depends on what you need for your project. But I like to have a centralized singleton manager that will be responsible for most of the events.
Thank you so much for this! I'm just beginning unity but this is a great help. I do have a question. The only error I'm getting out of this is for the Door script. Within the Start method I have: EventManager.StartDoorOpen += OpenDoor; It wants me to place a variable for EventManager.StartDoorOpen(). And I cannot place variable for either trigger ID or doorID because Unity has the error, [13:11:34] Assets/Door.cs(16,9): error CS1656: Cannot assign to 'StarDoorOpen' because it is a 'method group'. And another error the same but with: Assets/Door.cs(45,9) Any help of what I'm missing. Besides the action of the doors and some variable names I've followed suit on everything before you transition to singletons.
WARNING: if the object is disabled, the event wont trigger because the update is not executing. so there is absolutely no need to bother removing/adding the event action. SPECIALLY IF YOU DONT REGISTER THE ACTION "ONENABLE" BUT ONLY "ONSTART" LIKE THE VIDEO SHOW YOU. It wont be added back when you re enable the script... A really nasty mistake... What i would suggest instead register/unregister elements, just make sure the event wont happen on the action side instead. Event will still be triggered on every target, but each target will have the responsibility to handle weather they should responds or not to that trigger. ( Eg locked door vs unlocked one (if locked then don't trigger event). no need for premature optimisations if you only have 2-3 actions.
It's fine but honestly I think Unity's IEventSystemHandler is better than what you showed here because it is a 100% decoupled way of doing the observer pattern.
may i know what is the component inside MovePoint is it only object on where door goes (Transform)? i got a problem The variable movePoint of Door has not been assigned. You probably need to assign the movePoint variable of the Door script in the inspector. UnityEngine.Transform.get_position () (at :0) Door.Update () (at Assets/Door.cs:20)
nvm i have solved my problem. it was because i got DoorParent That havent been attached by movePoint. i thoug we just need to attach it on child only (Door)
It depends on the situation and what you need. Actually, in this example, it will give you a null reference error if you subscribe in OnEnable function. Subscribe anywhere as long as it works.
But i still do not get why should i use it instead of normal prefab of the door with triger? I that case de Player does not schare any data with the door.... Only check if the object in the tiger is a Player...
I will try to explain it more. Let's use the door example. Let's say you want to open the door and do one or more things at the same time. For example, open the door and zoom in the camera. In your case with prefabs, every prefab now has to have reference to the camera. It's better to just subscribe the camera to the event and in that case, in some levels/places when you open the door, you can, but don't have to use the camera feature. The code will work fine in either case. Then imagine you also want to enable some UI or play a specific sound that is played only with specific doors. Putting all that in prefabs and making references is a mess. Also, you can use events when picking up gems, for health bars and other stuff, your player doesn't have to store a reference to the UI. Hope it helps 🙂
yo me los aprendi asi Clase Jugador public delegate Mievento evento; public static Mievento evento(bool abrir) piblic void perdioVida() { Invoke?.evento( true ); } CClase dibujarVida private void dibujarVida( bool quitarVida ){ if( quitarVida == true) { //quita la vida y que se vea en la interfaz grafica } } public OnEnable(){ Jugador.evento+= dibujarVida; } y ya lo que tu haces en las otras clase suscribirse y llamar al metodo algo asi me lo public delegate Mievento evento; public static Mievento evento(bool abrir) es viejo esta forma de hacerlo?
(Me) public delegate Mievento evento; public static Mievento evento(bool abrir) (old form, old method) (Your) public static Action evento (new form, new method)
✅ Get my courses with discount:
👉 Unity 2D Master: www.udemy.com/course/unity2dmaster/?couponCode=2D-MASTER
👉 Unity Mobile Course: www.udemy.com/course/unitymobilecourse/?couponCode=MOBILE
👉 Unity Input System with Rebinding: www.udemy.com/course/unity-input-system-rebind/?couponCode=UNITY-INPUT
I've watched tons of videos on events, and this is by far the simplest and easiest to follow!
What a clean example with no unnecessary code. Great explanation, as simple as it should be. Awesome!
Thanks, man. Always trying to keep it short and simple.
Very straightforward! You describe exactly what the goal is and then outline it using limited code
You speak clearly and say only as much as necessary for teaching.
Thank you, very good tutorial
That's the point, thanks 😊
my shape is old and your shape is the new one used now?
Great video, very well explained, events are awesome!
Thanks so much! 😉
Oh man I literally just came from one of your videos where you used event's and I wanted to learn more.
Thank you Root Games for this very good and concise tutorial!
Plz do not delete this wonderful tutorial. So awesome !!
Don't worry 😉
Very nice tutorial. I'd recommend every beginner to watch this!
Short and to the point. Love this kind of tutorials!
one of the only videos in the known universe that says "dont forget to unsubscribe"
The best tutorial I've ever seen on the subject
Thank you so much, this was way more clear than any other tutorial and was short and to the point
Glad to help! :)
your explanation was very very helpful and thank you for that final tip, I found that very useful in my game. Thank you!
Really like how you lay things down so simply and easy to understand. Thank you so much!
Glad you like it! 😊
Hello, what is the advantage of having a static EventManager (the singleton pattern) instead of the first method which is shown?
Anyway thank you for the video, it's going to clean my code a lot I think
Such a sharp explanation you have made. Thank you!!
Finally got the idea thanks a lot.
Most welcome, subscribe so you don't miss future tutorials 😊
@@RootGames as you wish ^^
Thanks for this very useful tutorial. One thing I don't fully get, is how the EventManager can be used/defined in other scripts. Is it because of the static? What makes its event data accessible from other objects and their scripts?
That's correct. You can easily use EventManager in other scripts because it's static (saved in the memory)
The "static" keyword marks data that belongs to an entire class and not an object created by that class. That means you can access these methods, variables etc. (the ones marked as "static") simply by typing "ClassName.ThingInClassYouWant".
Another example that might help is, when you want to find the square root of a number, the code you type is "Mathf.Sqrt(number)". Mathf is a class and Sqrt() is a static function in that class. You don't need to create an object of class Mathf and then call the Sqrt() function on it.
I hope that helps.
Hey! This was pretty good! Thank you! :D
😉
Amazing Video, Thank You so much
Glad it was helpful!
Thank you :)Thanks to your video I understood the Event System
i was hoping my event system could get a reference to my player and send that reference to whatever script needed it. is that possible?
Now, do I need to to make that event manager static as well for reloading the scene mid-game or will this clean up on it's own?
Very Nice! Quick, Easy to Understand, and a little quick tip at the end. Keep at it brother!
Thanks, will do! 😉
Great video, very well explained!
Beautiful! Been having so much trouble understanding the logic and you explained it wonderfully!
Glad to help! :)
DAMN !!THIS IS A GREAT VIDEO! CONGRATS AND THANK YOU !
awesome video man. The explanation is very good. Thank you for the video & keep making more useful content like this
Very helpful tut!
Great video, thank you
Glad to help! 😉
Thanks you, you explain this very well!
Glad it was helpful! 😊
nice video, I understood now
For the last part, is there a particular reason you might want to do it in Singleton style with an instance over the way you were doing it before?
Not really, it depends on what you need for your project. But I like to have a centralized singleton manager that will be responsible for most of the events.
Thank you so much for this! I'm just beginning unity but this is a great help. I do have a question. The only error I'm getting out of this is for the Door script.
Within the Start method I have:
EventManager.StartDoorOpen += OpenDoor;
It wants me to place a variable for EventManager.StartDoorOpen().
And I cannot place variable for either trigger ID or doorID because Unity has the error,
[13:11:34] Assets/Door.cs(16,9): error CS1656: Cannot assign to 'StarDoorOpen' because it is a 'method group'.
And another error the same but with:
Assets/Door.cs(45,9)
Any help of what I'm missing. Besides the action of the doors and some variable names I've followed suit on everything before you transition to singletons.
Awesome video! Thanks
WARNING:
if the object is disabled, the event wont trigger because the update is not executing. so there is absolutely no need to bother removing/adding the event action.
SPECIALLY IF YOU DONT REGISTER THE ACTION "ONENABLE" BUT ONLY "ONSTART" LIKE THE VIDEO SHOW YOU.
It wont be added back when you re enable the script... A really nasty mistake...
What i would suggest instead register/unregister elements, just make sure the event wont happen on the action side instead. Event will still be triggered on every target, but each target will have the responsibility to handle weather they should responds or not to that trigger. ( Eg locked door vs unlocked one (if locked then don't trigger event). no need for premature optimisations if you only have 2-3 actions.
Great video too!
do you recommend events or interfaces?
Depending on what you need to do, but usually I prefer events.
It's fine but honestly I think Unity's IEventSystemHandler is better than what you showed here because it is a 100% decoupled way of doing the observer pattern.
may i know what is the component inside MovePoint is it only object on where door goes (Transform)? i got a problem The variable movePoint of Door has not been assigned.
You probably need to assign the movePoint variable of the Door script in the inspector.
UnityEngine.Transform.get_position () (at :0)
Door.Update () (at Assets/Door.cs:20)
i already assign my movepoint into thus script thoug
nvm i have solved my problem. it was because i got DoorParent That havent been attached by movePoint. i thoug we just need to attach it on child only (Door)
I believe it's better practice to subscribe to events in OnEnable function instead of Start function.
It depends on the situation and what you need. Actually, in this example, it will give you a null reference error if you subscribe in OnEnable function. Subscribe anywhere as long as it works.
Nice à Good tuto
Hi, when i try to type EventManager it gives me an error "The name 'EventManage' does not exist in the current context" any help?
I think you left the letter 'r' out of the word 'EventManage' ? So it should be 'EventManager' instead?
why the game keep pausing when i hit a boxcollider2d
Check if you have “pause on error” option turned on. Maybe you have a null reference error or something.
@@RootGames ty it works now
Why are you not using delegates?
But i still do not get why should i use it instead of normal prefab of the door with triger? I that case de Player does not schare any data with the door.... Only check if the object in the tiger is a Player...
I will try to explain it more. Let's use the door example. Let's say you want to open the door and do one or more things at the same time. For example, open the door and zoom in the camera. In your case with prefabs, every prefab now has to have reference to the camera. It's better to just subscribe the camera to the event and in that case, in some levels/places when you open the door, you can, but don't have to use the camera feature. The code will work fine in either case. Then imagine you also want to enable some UI or play a specific sound that is played only with specific doors. Putting all that in prefabs and making references is a mess. Also, you can use events when picking up gems, for health bars and other stuff, your player doesn't have to store a reference to the UI. Hope it helps 🙂
Observer pattern
What is the meaning of ?, when you are used in ExampleEvent?.invoke();
It's a short version of the "if" statement. Just syntax.
yo me los aprendi asi
Clase Jugador
public delegate Mievento evento;
public static Mievento evento(bool abrir)
piblic void perdioVida()
{
Invoke?.evento( true );
}
CClase dibujarVida
private void dibujarVida( bool quitarVida ){
if( quitarVida == true)
{
//quita la vida y que se vea en la interfaz grafica
}
}
public OnEnable(){
Jugador.evento+= dibujarVida;
}
y ya lo que tu haces en las otras clase
suscribirse y llamar al metodo
algo asi me lo public delegate Mievento evento;
public static Mievento evento(bool abrir) es viejo esta forma de hacerlo?
Sorry, I don't understand you, only English.
(Me)
public delegate Mievento evento;
public static Mievento evento(bool abrir) (old form, old method)
(Your) public static Action evento (new form, new method)
love your video, 1 sub from me, tks u