Using Scriptable Objects for Events in Unity | Scene Independent Event System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so i've dragged our broadcaster over to a different scene this listener is in our sample scene doesn't need to know about the scene it doesn't need to know about broadcaster within that scene all it cares about is this channel which is living just on the hard drive if i can hit play and you can see that we're still receiving the events so a lot of event systems out there use the singleton pan to manage the their events there are pros and cons to the singleton pattern if you want to fully explore what the pros and cons are i've got a link in the description below to some articles about that the main con for me is that they have to be a mono behavior on an object in a scene and then that that's fine you know if you're using a single scene but if you've got multiple scenes where the singleton has to move between scenes then every object in that scene then has to get a reference to that object on their sort of unenabled function or other unawake or they have to cache the object in some way there's a lot of dependency there like stuff needs to know about other things and and the less of that that we can have in our projects the better the project will be and the more scalable the project will become as well so this is where this uh scriptable object event system uh sort of wins out over the singleton pattern so scriptable objects are data containers they don't live in the scene they live in the actual project files but they're not just for containing data you can actually run methods on them and use unity events so because of this they can be accessed from any scene because they're just on the disk that reference will never change between scenes like its location in your project file is the location unless obviously you move it this removes a lot of dependency out of everything and like i said the the less dependent scripts are of knowing about each other the the more scalable your project is and just the better that process will be of developing your game if you want to know more about scriptable objects unity have a great video on them and i'll link that in the description too i first saw this scriptable event kind of workflow on another unity video they've got an open game project called chop chop which they're developing with the community showing off really good coding practices and kind of how you can really get the most out of unity in all of its systems again there'll be more information on that below so how does this all work so it's using something we're going to call channels and this kind of scene i've set up just shows a rough estimation of this process so we've got a broadcaster which you can imagine as sort of a radio tower and it's broadcasting something on a radio channel in this case for example on the radio they're trying to do some home fitness and there's a home exercise show that they're broadcasting out and then this is going to go to our channel relay and then anyone who's listening to that channel on their radio they'll hear this radio show and they'll get the instruction for this homework out thing to do jumping jacks so if they're listening you know they'll start to do their jumping jacks anyone else who's in the room if they're not really listening to the radio maybe they're just scrolling on the phone they don't have to jump they're not going to do the jump only the thing that is like actively listening to the radio and participating in this show event uh they're gonna be the ones who do the jump so let's just have a look at how this all works so in our scene i've got my kind of setup here and i've got a broadcaster which is our gem on the left here and i've got a listener and the listener's got a listener script on it the broadcaster's got a broadcaster script on it but the red gem doesn't need to know about the left gem so if we had a singleton pattern the listener would need to know about the broadcaster so these two objects would be linked and then if the listener was a player and they went through a door and went into a different level the broadcaster would have to come too but using this method that's not a thing we don't have to do that um so i'll have a look at the script for this first so in our script uh which i've just called void event channel so for scriptable object uh we've got a bit of a summary here so this class is used for events that have no arguments um we're using unity engine and unity engine.events and so that just lets us have our public unity action i'm just going to call it on event raised we have our create asset menu here so menu name equals and then we've got events void event channel so that's just so we can right click in the project and create a new channel um so we've got a public class void event channel so public unity action on event raised and then we've just got a method called uh raise event and we're saying that if our on event raised has some subscribers so if anyone's listening to that radio station then just you know carry on broadcasting invoke the event that we want what we need to do then is if i go over to my scriptable objects folder i've got my jump event here um because of that create asset menu sort of attribute i can go to create events void event and i could call this the spin uh event spin event the spin event i could have multiple events um and these are all type voids so they don't have any sort of parameters they're just normal kind of basic like hey i'm doing something but you're not passing any information along uh with that so we've got a jump event here if i go back to our broadcaster script so in this case let's just get rid of these let's just sleeken out the script so so we've just ignored the color one for now so we've got our void event channel so which is our scriptable object script and i've just called this the jump channel oh here i'm saying if jump channel isn't equal to null so for example so we've actually assigned the jump channel in the inspector and if the space key was pressed then just raise events so in our example this is now the instruction to jump so anyone who's sat at home listening to their radio they've heard the information to do jumping jacks so they're going to jump and you see that there's no singleton patterns there's no instances it's just a very simple line of code um jump channel dot raise event now if we go over to our listener script you can see that here we've got a public uh void event channel so we'll just call that jump channel as well in our on enable function i'm just subscribing uh a method here called jump to the on event raised function and then on disable i'm just gonna uh unsubscribe from that event as well i've got void jump and then i'm just using do tween to to do a jump so just back over to our project here so now you've seen the code this is broadcaster uh when i press space it's going to uh broadcast to the channel relay or our radio in this case and just invoke the event um our jump channel event and then our listener is this gem and it's just gonna do the jump but that's our void event channel um but using the exact same code you can see that i've just copied and pasted this into a new script called colour event channel so this is used for events that pass a colour and we've got a create asset menu events so this is our colour event channel and then we're doing public unity action color on event raised and raise event this is going to take in a color and then it's just going to invoke it and pass that color on to any listeners and so our unity action and then in our braces here we're just saying that we need a color when we raise this event like it's expecting a color so on our broadcaster script we've got our color event channel scriptable object called color change channel if that is assigned in the inspector and left control was pressed and we're just going to raise the event and we're going to pass in a random color now we can jump and we can press control and get a random color this is sort of infinitely scalable so in our scriptable objects we could make another one uh so let's make it a new c-sharp script and we'll call this uh float event channel so we'll just open up this new script we've got our float event channel so i'm just going to copy over our void event channel um code so this class is used for events that have um a float argument and this unity action we want to take in a float and then uh our raise event will take in a float called uh value and then we're just going to pass that value on and just so we can make this in the inspector get our create asset menu attribute we'll do events and we'll do a float event channel so let's just go back to unity and if we go over to our scriptable objects folder i can now right click create event event and that's not worked oh not derived from scriptable object so it's not a mono behavior because it doesn't say in the scene it's a scriptable object so let's just go down okay so back in our project i'm going to delete this spin event and then i will create events float event channel and i'm going to call this one the spin event because we're going to pass in a float and have our gems spin that amount of degrees so we've got our spin event channel here if i now go over to our um broadcaster and i can make a public float event channel and i'll call this the um spin then i can say that if our spin channel isn't equal to null and keyboard dot current dot left alt key dot let's press this frame we're gonna do our um spin channel dot raise event we see this takes in a value i'm just gonna call this um spin amount which will be a public float old spin amount and i'll equal this to 45 degrees at first we change this in the inspector and we can also you know do a public color color change we'll set that inspector as well so instead of doing a random color we can pass on our color change spin amount and so over in our listener script we can have a public uh float event channel call this our spin channel and i'm actually going to get rid of the jump channel so we've changed station we're not listening to the jump injection we're going to be listening to the spin show avoid jump that's not going to do anything so avoid spin it's going to take in a flow value so now we can subscribe to our spin channel on event raised pass in our spin and then we need to make sure we unsubscribe from it as well actually instead of unsubscribing um i'm gonna have a public bull uh should jump that is equal to true by default and we'll just say if should jump then we'll subscribe to it we'll unsubscribe from it and if we shouldn't jump then we're not going to do that for our spin we can do transform dot rotate uh transform dot do row eight a new vector three and we're just going to rotate around our y axis by the value and it's gonna be a spin of 0.5 f and we'll do a rotate mode fast beyond 360 so it can spin around itself so if we go back over to unity or add an extra uh bracket so now on our broadcaster we will pass in our spin event actually the good thing about this is as well if we go to our broadcaster and we click the radio button next to the spin channel it's only going to show us the events that can actually plug in there so we can just choose our spin event um we don't have to navigate through our project to find it so spin channel an event and i'm saying i'm going to say that this listener should jump this one couldn't jump so all being well uh we've now created another sort of event in our scriptable object event system oh because i've commented out the jump event we lost the reference to said jump event so we'll just plug that back in to avoid that what you could do um when you're subscribing to the event in the listener uh you could book it should jump and uh jump channel isn't equal to null and you could do that on these as well so make sure they're not null before you try and uh subscribe to them to avoid those null errors um but if we hit play now so we can see that um this left gem is listening to the instruction to jump and this one isn't so we hit uh space only this gem will jump and remember none of these know about the broadcaster this broadcaster could be anywhere in the scene it could be this broadcaster could be in uh any scene it could be in a different scene for example you may have like a two scene structure where you've got like an initialization scene and then you've got uh this other when you've got the actual game scene and all this is doing is telling the data on the hard drive to raise an event and these are listening to that so as this one jumps this one doesn't but they can both spin they'll both spin around i've obviously got the axes that they were around wrong you can see that as i press alt they both spin and then i can press uh control and they both change color just to illustrate the point um if i get if i make a new scene so create um scene and i'll just call this uh initial scene i'm gonna bring this over it doesn't need a camera or a light so in our initial scene i'm gonna move the broadcaster to our other scene we've got our two scenes here we've got our initial scene and our sample scene now if this was a singleton it'd be very hard to get a reference to the objects in the other scene so i've dragged our broadcaster over to a different scene this listener is in our sample scene they don't it doesn't need to know about the other scene it doesn't need to know about the broadcaster within that scene all it cares about is this channel which is living just on the hard drive if i can hit play and you can see that we're still receiving the events this really shows the power of this event system using scriptable objects this just means it's so much more scalable than using a singleton pattern and having your scene full of like audio manager event manager input manager for example we could do this uh having an input manager it just lives in the project it's not cluttering up a scene nothing needs to know about it just works i hope this video was useful you'd like the project files and the scripts from this video they'll be available over my patreon which is in the description below if this video was helpful i'd really appreciate a like or a comment to say so and of course if you could subscribe that'd be brilliant in the meantime thanks for watching and i'll see you in the next one bye
Info
Channel: Dan Pos
Views: 15,405
Rating: undefined out of 5
Keywords: Unity Scriptable Objects, Unity event system, unity actions, unity input system, unity tutorial, game development, unity 3d, made with unity
Id: e8WHdMI8hxk
Channel Id: undefined
Length: 15min 24sec (924 seconds)
Published: Wed Nov 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.