Easy Simple Game Architecture in Unity Using Events

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

events are absolutely essential. All of my UI is wired to events, and only updates when it needs to. I do everything I can to remove the void Update() method in monobehaviors

👍︎︎ 6 👤︎︎ u/lemming1607 📅︎︎ May 24 2020 🗫︎ replies

Great video... thanks for sharing!

👍︎︎ 2 👤︎︎ u/creahmc 📅︎︎ May 24 2020 🗫︎ replies

I’ve been watching a lot of event tutorials lately and with this one it finally clicked. Thanks

👍︎︎ 1 👤︎︎ u/baz4tw 📅︎︎ May 25 2020 🗫︎ replies

This is great, really powerful stuff, and I plan to implement these patterns ASAP in my game.

The Sebastian Lague video he links to, and its follow-up video, are really essential for understanding and expanding on delegates, which is what this is one subspecies of. An "Action" is a shortcut for declaring a delegate, so you really want to watch the other two videos to know what you're really doing. You should also know Funcs, which is an Action that can return something. He also briefly mentions lambdas, another powerful tool.

It's going to take me some time to internalize all of this. I'm going to have to force myself to use them a few times before it really sinks in.

👍︎︎ 1 👤︎︎ u/KungFuHamster 📅︎︎ May 26 2020 🗫︎ replies
Captions
sup nerd do you have problems even when you're making a small game with plugging every little piece in together or maybe once again with a small game you pull out a single thing and it breaks the whole thing well in this video using events I'm going to show you a pretty basic design pattern that can help with those problems it can be used to make a pretty simple small game entirely or it can be used as a building block for a larger more complicated game first we'll see it in action then we'll go over the idea then we'll look at it in code and at the very end I'll go over a few more tips and pitfalls so stick around for that and of course the project I'm going to show you will be available for download in the description so we've got a real simple game here to show what I'm talking about at the bottom is this blue cube that's the player you can move back and forth we have a little UI telling you the press space to start we also have a score and a high score so we press Start it says go and these little red cubes start falling if they hit the bottom you get a point and if they hit the player then it says game over and restart saves the high score and you can play again trying to avoid the cube so let them hit the bottom pretty simple game but let's say we want to change some things here we want to get rid of the score system we don't want that anymore for some reason so we delete that and then I want to add this screen shake you'll just have to take my word for this for now but this has no reference to the player and we start the game and you may expect to get some errors because we removed some things but even though we took out the score there's no no reference exception or anything the game is not complaining that something's been removed and then when we get hit we get this nice little screen shake effect and we didn't have to have any reference to the player to do that we're able to add and remove things easily let's take a look at the idea behind this so I've got this very artistic diagram here of our simple game and let's take for example when the player gets hit in the game ends there's a lot of things that have to happen even with this really simple game we have to tell the spawner to stop spawning enemies we have to tell the UI system to change we have to tell the score system check for a new high score with that screen shake maybe doing a screen shake effect and the first thought might be to just link this all up to the player have the player tell all these things when it gets hit and that this all this stuff should happen and then we also have to do stuff when the game starts so we've already got all these connections on the player so I guess we'll just add that on there and it should change the UI it should go to the spawner it should go to the maybe there's an effect you want to play when you start you got a reset the score system very quickly this player class is getting super bloated it has to know about pretty much everything else in the game when all it really should be is a thing that moves back and forth and all the sudden it has all these connections all these dependencies and if we remove something unrelated like a screen shake effect that can break our player so as your project is growing or you're changing things changing and removing and adding things can become a real headache in this big tangled web we have an alternative is to have a central event caller and this can call out major events like the game starting the game ending or incrementing the score the extra magical part about this is that the event caller doesn't even have to hold any references to these other various scripts they themselves subscribe to those events and then they're notified when those events are called by the event caller this way the player can tell the central event caller to call the game over event for example and any script that has subscribed to that event will then do what it needs to do so now instead of dependent on each other these scripts are self-contained and we can easily add and remove them all so things like the player don't know about unrelated systems like the UI system finally I think this makes things a lot easier to reason about I know this might just seem a bit weird like the same thing as earlier but with extra steps but I think if you see the code or if you try using this yourself you'll see how just clean it is and how non dependent on other elements everything is you're able to plug in things and remove them easily so let's look at that code though I keep talking about events and that's literally what they're called in code I know unity has a built-in event system but I just prefer to use it in c-sharp code it seems easy to me if you don't know anything about delegates or events sebastien like you as videos on them I don't know I don't know how to pronounce his name but I'll link in the description really good explanation of delegates and events but basically they can hold references to methods and when that event is invoked all the methods that it's holding references to will be called to use these you have to make sure you're using the system namespace in your script here I just have this game manager I'm making a singleton out of this so I can easily call it from other scripts you don't have to do this you can find another way to reference it and then here's where I declare my events I just picked three for this game three major events that happen in this game game started game lost and score incremented that's really the core of this game pretty simple now we're keeping track of a little bit of state is playing starts as false if the player presses space we start the game and set playing to true and starting the game just that's all it is right there game started invoke this little question mark just checks to make sure that the event is not no we don't want to call an event that doesn't have any references to any methods that will return a null reference exception but you can see here that this game manager does not have any explicit reference to any other scripts it's up to those scripts to add themselves to this event and then they'll be called when this event happens so we can remove them without breaking the game manager so let's go over and look at one of those and see how they add themselves for example in the enemy spawner it is really this simple we get a reference to the event game manager instance that game started and we plus equals a method we want to call this simply just adds this method to the list of references in that event and whatever that event has invoked this method will run so in the case of game started start spawning or on that just starts a KO routine which starts spawning some enemy is pretty simple down here and then we do the same thing for lost game lost plus equals stop spawning that subscribes the stop spawning method to the game lost event and in this way all the like connection setup responsibility for each element is contained within that and we can just plug this in easily and make it subscribe and get it connected without having to add anything to game manager so we can see doing this in the score system and the score incremented events we increment score the game started event we reset the score we can really simply add in new elements and if we were to remove one of these elements game manager doesn't have any reference to them they add themselves so if they're not there in the first place they're not gonna add themselves there'll be no problems we can remove them and it won't affect anything else there are a couple of things to watch out for for example if you're creating and destroying objects or hopefully pooling objects if it is still subscribed when it is destroyed and then that event is called it's gonna look for that object to call the method that it's subscribed to the event with the object no longer exists it will throw an error all you have to do is make sure that if an object is being destroyed that was subscribed then it unsubscribes and this is quite simple as well you just get a reference to the events you want to unsubscribe to and instead of subscribing by plus instead of subscribing by pressing plus equals you unsubscribe by doing minus equals and that removes this method from that event secondly don't overuse this or at least don't put too many events on one event caller for example let's say you are trying to use this pattern in a slightly more complex game the player can take damage multiple times before dying so you could have overarching game state stuff like in this example on one event caller but then maybe a take damage event on the player and you can have things subscribe to that like sounds or post-processing effects or a health system it's useful to know for this example that events can take parameters and they can also return values so for example on your take damage event it could take in an int value and then all the other methods that subscribe to that event will take in that int value so you could take a variable amount of damage and you could pass a large amount of damage or a small amount of damage to the sound effects so a different sound effect plays all I'm still one single same event and still retain all this nice clean modular ability that events gives you that's all I got if you want to download this project you can in the description and if you want to subscribe you will see more unity game development related stuff and maybe some VR stuff too but I'll see you around thanks for watching
Info
Channel: Andrew George
Views: 6,700
Rating: undefined out of 5
Keywords: Unity, tutorial, C#, events, game development, learn, game dev, easy, simple, game, architecture, programming, coding, make a game, free, learn unity, indie, game architecture, game design
Id: Ve-8kGnj8u4
Channel Id: undefined
Length: 9min 14sec (554 seconds)
Published: Sat May 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.