Hello, everyone, and welcome in this lecture, we'll be making it so that when we kill the enemy base,
the game will actually end.
It will display to all the clients in the game who the winner is.
So for now, this will say that Player one is the winner and that they've won.
Now, I can't now click on the base and spawn in any more units or anything.
And if I now click leave game on here as a client, I will leave and then the server the host can leave
to, the game's over and we're done.
That's what we'll be doing in this lecture.
Let's get started.
So when the game is over, we want some UI pop up and UI is all client side.
So we need to have some event that's triggered on the client side when the game is over.
So let's head over to our scripts and we'll want to go into our buildings.
Game over handler.
So let's make the event that's raised on the client side.
So the naming convention I usually do for event starts with a client based on where it's being raised.
So let's make a public static event action system and what data do we want to pass through while all
we're doing is we're going to show on the UI the name of the person who won.
And right now we don't have a way to set name.
So we'll just say, like player ID, play a one, play two.
So we want to pass through a string, which is going to be the player's name.
So we'll say clients on game over and it's the server that that goes out when the game is over down
here.
This is when the game's over.
So the server needs to actually tell the client that the game's over and the way the server calls code
on the client is by using an APC.
So let's make a client APC and we have to start it with APC and we'll call it obviously game over.
And that takes in the string, which is the the winner, the winner's name.
And all we do here on the client side is then raise that client side event.
So client on game over, don't invoke the winner.
And that will allow us to now make a script for our UI and subscribe to this event.
And then when this event is triggered, we pump it up saying the game's over.
So instead of debug logging now we're going to say, OK, game over.
And the actual name that we're going to pass through would be the play his name.
But for now, we just want to say the ID so we can do here is we can figure out what the ID is by saying
the player winner, because that's the play script.
The winner is Bass's zero because we got the list of bases and if there's one left, then we can access
is zero, which is that person connection to client because this is on the server side and then we can
get the connection ID, for example.
So this is just a unique identifier for that player.
And rather than actually storing as a player for now, we'll store it as a and so player ID
and we want to as a string.
So let's say play and then player.
If you forgot what the string means, it just means if you put a dollar sign before it, then you can
insert variables inside Kelly Brace's.
So it's player space and then the variable player ID.
So when someone wins the server tells all the clients the winner is player one, player two or whatever,
and then the client say, OK, I'm going to let anything on my side that cares about that.
No.
And the only thing that cares about it right now will be the UI that pops up.
So let's go make the UI now.
We'll head back into unity, give it a second and we don't need to do anything else on this game.
Object for the game over, handle it, because what we've changed is just raising an event.
But we do however, want to make a new folder for menu I think makes sense.
And we'll call this the game over display.
We'll open it up now.
The game over display is completely client side, so it doesn't actually need to be a network behavior.
Anything that's running this is just going to be client side.
And all we want to do is we want to subscribe to the event for when the game is over on a client.
We want to subscribe to this so we can do that in Stott and undestroyed.
So private void on destroy and we'll make it stop.
Get rid of the comment on where to go.
So we'll want to say here, game over handler DOT and we can listen to the client on Gameover event
plus equals client handle game over and then down here we'll unsubscribe.
It's probably becoming very easy to understand what's going on now because we've done so much subscribing
and unsubscribing of events and then down here will actually make the method so private void client
handle Gameover.
And this takes in a string, remember?
Which is the winner.
So we need some UI here to actually set that name, so let's add a reference to some UI serialize field
priva and we want reference to using text mesh.
So it's Tempy unschool text.
Is the text element the winner name text that requires us to import Tuam.
So it's now in here whenever the game is over and we're told the winner's name will put it on our UI.
So say the winner name text dot text equals and then we'll say here
winner has one exclamation mark and I'll actually capitalize this.
So now will say play it to has one, but this guy is going to be disabled until someone actually wins,
then we need to enable it.
So to enable it, we're going to need something to stay active.
So let's make a reference to a game object and we'll call it the game of display objects or parent.
We'll call it a game over display.
Then down here, we can enable it, so the game of active trade, so enabling the parent and the one
of the thing we need here is the way to actually shut down the server or leave the server when we're
done.
And this will be called from a button.
So we need to make this public and we'll call it leave game, public void leave game.
So we've leave game.
It's a bit different based on whether your hosting or if you're just another client.
So we can say, well, if network server active, so remember to be using their networks over the active.
And so we're running a server and we're running a client so that when a client is connected, then we
want to stop hosting.
So stop posting of the lies.
We want to say stop client because we can't be just a server on its own because the server on its own
can't press the button.
So we're going to say her network manager, Singleton, stop host.
And then down here we can say network manager Singleton, don't stop client.
And this will automatically send us back to the home screen because we have set up in the network manager
the online and offline scene.
So as soon as we become offline, we actually go over that.
So if we're just a client that leaves, we'll go back to the home screen.
The server will keep running, whereas if we're the host, we'll stop the host.
So we'll go back to the home screen and all the other clients will be kicked.
And that's it for the code.
So now we just need to go make the UI.
This UI will be a prefab.
It doesn't have to be, but I like making those things prefabs, so we'll go ahead and make an empty
game.
Object will reset that and we'll call it the game over the display.
Then we'll make that a prefab and then we can just open up in its own view.
So if we double click the game over display, go into 2D mode, we can then get a good view of it.
So the game over display has the game over display component on it, and then we'll have to drag these
things.
And once we've got them and then we want to make a canvas.
So UI canvas and with the canvas we to say screen space overlay, this is all fine, whereas this needs
to scale with a screen size because it'll be a full screen thing and we'll say our current resolution
is nineteen twenty by ten, eighty and much height.
And we do want a graphic, right Casta.
Because we need to actually be able to click on this UI and since we'll end up having building buttons
and other things, you can click on the scene.
We kind of want an empty game.
Object to non-empty but sorry, an invisible game, object to block all our clicks into the game scene.
So what we can do is we can make a UI image and images, as you see a lot of block requests if this
is change and we can hold alt click and well that does, as you can see, all the settings that this
is now scale to the screen size.
So if your screen is wider, this will be wider to this fills up the entire screen and we can say here,
so the alpha to zero.
So it's completely transparent, but it means it can stop us now clicking on things behind the UI.
It's like a shield because we don't want to click on our bases or unit spawners to try spawning in more
units.
This should be a kind of first line of defense to stop us interacting with stuff when the game's over,
we can also add some text.
So let's click on the canvas.
New UI text text pro.
Since we haven't actually use text message area in this project, we have to import the essentials.
Now that that's done, we can go back and this and this text should be a child of the canvas that we
go.
So now for the text, I'll set up how I put it in my own project.
Let's do the anchor's first.
So I had twenty five point five point five one and then for the positions, zero four hundred zero seven
hundred zero.
And that leaves it like this.
And then for the text, we can say someone has one exclamation mark to see kind of what it's going to
look like.
And I had it as bold with a font size of seventy.
You know, this is kind of up to you to do it however you like.
I'm just going to show you, this is how I did it.
And now it's time for your challenge.
And I've already done some of it with you because I don't want to leave you in the dark.
But the challenge is to finish the game over display or just have a go at making it look however you
like it, too.
So we just need the game object, which you've already made the game over display, which we've already
put on.
But be great.
If you could actually drag in the components into it.
We'll be doing that together.
Don't worry, create some UI text to display the window just like we have and create a button for leaving
the game.
We'll hook it up to the code in a minute if you don't know how to do that.
But this should be enough just to get on with the now.
So have it.
Go pause the video and create your game over UI.
OK, hope you paused and gave that a go.
Let's head back and as I said, I've already done most of it anyway.
I just wanted you to be able to have a little bit of design time on your own now for the Butson Will
Make and the UI.
But Syntex Met-Pro as a child of the canvas is.
And for this Butson, we'll just set up the end because like this zero point five and then zero is the
second and that's it then for the positions.
Zero twenty four hundred, four hundred and sixty is what I found to be good.
I'll also get rid of this UI Sprite so I'll put it to Norn and then we don't really want anything else
on here right now.
We want to go to the text itself and then for the text click on here.
It's already stretched anyway, which want to add some padding of, let's say, five around each side.
And the actual button itself will say we've game on it and then that can be bold.
And the font size, I put it as also size.
And then over here, this is fine.
I think it's just good to go.
Let's have a look in the games.
If I was to drag this into the scene, this is what it would look like.
Obviously, it's kind of hard to see what the white text on the horizon here, but it's fine that'll
do for this.
So let's head back into the actual prefab itself.
So double click on the display, go back to the scene and we need reference to the Gameover display
parent, which I'll just do as the actual canvas game object will be toggling that on and off and the
winner name text is this text over here would be best if we were to rename it.
So I can just say here, this is the background.
This is the when a name text, this is the leave button and then this is just the lead button text LG
and we want to make the leave button.
If we scroll down on this button on click event, we want to add drag in the game over display and we
have the game over display leave game function.
So we want to leave game to be cold when we click on this button.
So if we want to go to the canvas and disable that because by default that should be off, we can then
go back to the scene and we want to jump over it to the of a scene, which is where it's going to happen.
And we want to add in our game up display pre thought we can just stick it wherever you want, may be
here under the post-processing and everything.
It doesn't matter too much.
And yeah, there it is, it's in the scene and when it starts existing, it listens for when the game
is over and when the game is over, it sets the text and it turns on the UI.
So let's give that a go.
But just before we build, let's go over to the network manager.
And this is important.
I almost forgot to do it, which is we need to add into this.
It's portable prefabs, the game over Andula, because this is an object that we spawn over the network
and anything we do spawn over the network has to be in this list.
So let's quickly go over here and we want to say prefabs gameover handle.
So let's go back, drag that in to this list.
We'll go down unities not wanting to cooperate.
They go Gameover handler overrides apply.
Because if you don't do that, then what happens is the whoever's the server when they win or when they
lose, whatever happens when the game is over, the server will show up the UI, the client won't because
it fails to actually spawn the game of a handler on both client.
And we should technically have done that in the previous video.
But it didn't cause any issues because we were just logging in the console and it doesn't really matter.
And we were only doing that server side.
But now that we're doing stuff on client side, we need the game over, handle it to be in this list
so now we can build and actually test them.
Let's give it a go.
Take this tank.
We'll come over here and we'll try and destroy this space.
And hopefully when it dies, some UI should pop up for both players saying who won?
So it says one has one, and if I was to leave over here as a client, then I leave and this keeps going
and I'll test the other way around in the second to show you what would happen if I now host here and
join as a client here.
There is still a bit of a weird issue.
If I start as a host on this external one, then they spawn at the bottom left and bottom right instead
of diagonally.
That will be sorted later on when we make a lobby.
But for now, I'm really too big of an issue in my thinking and I can go kill over here.
I should say play a zero yet play zeroes one, and that's fine, and if we leave over here on the host,
then the client leaves two because there's no server anymore.
So, yeah, that's it for this lecture.
We now have the game loop working.
Of course, the game isn't that fun.
Right now we only have one type of unit and it's really big and slow.
Maybe you want big and slow unit, but you probably also want some really cheap fast to produce small
units.
So be making the game a lot more fun now that we have the game loop working.
So I'll see you in the next lecture.
Goodbye.