Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
We still haven't implemented what should happen if the player is defeated, right?
So we take damage now we take damage and all of these things.
If we take enough damage now, our player is just being despawned and we get this weird screen where
we just don't see anything because we're not possessing any pawn.
And also if we exit out, we get an error message.
So let's start taking care of these problems and actually respawn the player in here.
Just dismiss this, and we can just close all of the windows to the right and just start from fresh
here.
The first thing we want to do is that if the player is destroyed, we just want to respawn a new player
in this player, start location and change the pawn that we're possessing from the old one to the new
one.
When it comes to respawning, there's usually two options.
Either we destroy the old character, spawn a new character and possess it, or we just teleport the
current character to the spawn position and reset the health and all of the other states.
Both of these methods have their own pros and cons and things you need to look out for, but generally
speaking, destroying the old actor and spawning a new one is the easier method.
There is no risk of forgetting to reset the health or other states and the bugs that come with that.
For testing, we just want to set the player health very low now.
So in the BP player, uh, we just look for the, uh, sorry here, the BPC vitality, and we just set
the max health to one.
And this is just so we can easily test just being destroyed and can test the respawning much easier
than just having to get hit ten times.
Next up, we want to create a function that spawns a new player.
And we don't want to put this on the player of course, because the player is being destroyed.
And this is kind of an overarching gameplay thing that the game mode should be taken care of.
So in the content drawer we go to blueprints and we created this GM action in the beginning, open it
up.
And we mostly created this so we can set the default pawn class.
But actually this is a full blueprint.
So we can add logic here.
We can add functions.
We can add variables that can be called from other places and from other actors.
So this is the perfect place for something like a respawn here.
Let's just create a new function and it's called the respawn player.
And what can we do here?
Well, we just spawn actor from class.
And here, well, we could just select the player class, but actually we can also just get the default
pawn class right.
Because the default pawn class exists inside of this game mode.
Right.
So instead of having to manually pick this well we can just use the same one that is in here.
And now we just can split the spawn transform um split struct pin.
So these are separate nouns that of having to pass a transform.
And for the collision handling override we want to make sure to always spawn ignore collision because
we don't want it to stop spawning the player just because it's being blocked by something.
And in the end we then want to get player controller.
Get player controller.
And again this is a built in thing with Unreal Engine.
And here we can then say possess.
And pass it, this new character that we are going to spawn.
And yes, so this spawns a new player in this location.
We got to give it a better location later on, and then it just tells the player controller, hey,
you are supposed to be controlling this character now, not the old one.
And yeah, I'm just rearranging some notes here.
But we still need to have a place where we can call this from.
So if we look back on the other parts of our code.
So in the content drawer, blueprints, characters, action, char base, and in here we have the,
um, any damage, right?
We take damage, we check if we are defeated and then we destroy the actor.
So now a problem we have here is that, well, we do want to destroy the actor in case that it is an
enemy.
But we don't want to only destroy the actor if it's a player.
We want to call this respawn function that we just created.
We also want to later put in some camera fade and like all of those kind of things, have a little delay
before we destroy the actor.
So actually we need to add some branching logic here for the player and for the enemy.
And there are multiple ways we could go about this.
The most obvious one, which I don't recommend.
It works, but it's a little bit dirty, is to just get self.
And then self cast to be player.
And then we can decide here.
So if it's not a player right cast failed would be an enemy.
We could then just destroy.
And if it's true then we do something else.
But this works.
Um, it's not the best way though, because in this way we put all of the logic we want to use in the
player inside of the action char base.
I think we just want to use an event dispatcher, which you can see here, and basically just tell the
child, hey, you got destroyed.
Decide by yourself what you want to do with this information so we can just get rid of this here.
Don't use this.
And we're going to create a new event dispatcher.
Click on the plus and just call it defeated.
Like this.
And we can then drag this out here and call the feed it and get rid of this destroy actor.
Only connect the call defeated.
Right.
So this is an event that other blueprints not just children, but pretty much anybody who has a reference
to this blueprint can subscribe to.
But we're going to use it in the children.
So when we open up the content drawer, we go to enemies and we want to have the BP enemy base.
And here on Begin Play because you subscribe to an event like this on Begin Play well, we can just
look for bind defeated write bind event to defeated which is again this defeated event dispatcher which
we created.
So in here we just want to connect this here.
And here we drag off and.
And add custom event.
And we just want to call this on defeated.
Like this.
And yeah, so if the feed it happens we call this undefeated.
And here we just destroy the actor for an enemy, right.
For a player, we're going to do something else.
But if the enemy gets the message he got defeated.
Well, just destroy yourself.
It's all fine just to show how this works, right?
I can compile, save, I go here, I shoot the enemy.
The enemy gets defeated, it's all fine.
But the player actually nothing happens, right?
Because we didn't listen to the event.
Dispatcher so nothing happens.
So we can implement this now in the player as well.
So for the player, open up the blueprints characters, player by player.
And on the event graph we want to look for Beginplay and we can just type it in Beginplay.
It's going to teleport us up here and we want to add yet another row.
So maybe open up some space here and here.
We want to bind the defeated.
Just like we did there and here.
We can also just drag off, add custom event on the feed it.
Here.
We want to call this function on the game mode, which we created.
But the problem is that right now we don't have a reference to the game mode.
So we can actually just open up some space here.
And get game mode.
This is something you can call from every blueprint.
And it's usually customary to do this on begin play.
And here we can just cast two GM action, which is our instance of the game mode, and connect this
here and then save a reference right.
So promote the variable and just call it.
And just call it GM action.
Like this and connect this here as well.
And one risk you have with this now is that if we cannot find a game mode that is GM action, the cast
is going to fail and nothing after that is going to execute.
Right?
So we're not going to set up the player control and all of these things.
So we could put this on a separate line.
But the real truth is that if we cannot find our game mode, the entire game is broken anyway.
So I don't see a problem with having this lined up in this fashion.
Right?
But now we have the game mode, we get it at the begin play, and we can then access it in our entire
blueprint whenever we need it.
And well, here we do need it.
So bind undefeated.
We can just get the GM action.
And call respawn player on this and connect this here.
Right.
So on the enemy we decided we just destroy the enemy here.
For now.
We just decided we're just going to respawn the player and we're not going to destroy anything yet.
And let's just have a look at how this works.
And what we can do now is that if we jump in here, you're going to see some very weird behavior.
We're just going to keep on spawning, um, new players as we take damage.
Uh, first of all, we need to make sure that we can only be defeated a single time, so this doesn't
happen.
So in the GM action char base here, where we do the whole thing with, uh, receive damage and is defeated.
Well, we just want to drag off here to the side.
And go like this and just add a branch here.
You add a branch.
And check for BPC.
Vitality is defeated.
Not boolean.
Like this.
And connect this here.
Need to open up some more space.
Right.
So we're not gonna do any damage calculations.
Anything related to that if we are already defeated, right?
We're not supposed to be able to take damage if we are defeated.
And this will also prevent this whole issue with respawning the character multiple times.
Right?
So like this.
We only spawn one.
And here we're going to keep on taking damage, right?
But we don't respawn more players.
But this is also a small issue we got to take care of.
So in the BP player where we have our any damage event.
Any damage.
Right.
We have the cancel slide.
We have this.
Was hit during charge.
We have the trigger invincibility and knockback.
So both of these things we actually only want to do if we aren't defeated either, right?
So because we run a logic here, this is still going to happen.
So on the BPC vitality we can drag out here and again check for is defeated.
And not boolean.
And add a branch here.
Open up some more space here.
And yet we're not gonna trigger the invincibility if we are already defeated, because there is no point
to this.
And we just get this kind of weird behavior, which we already had.
Right?
So now.
Yeah.
So this is already better, right?
But the character just gets stuck here.
Um, and this looks very funny, but this is already better, right?
Because now we're just gonna despawn the player and take care of those things, and this is going to
fix the problem.
So for now, just go back to the BP player.
Right.
And if we go back to our begin play on the event graph, just look for Beginplay.
And after the on defeated event after respawn player later on we want to have a delay, but for now
just destroy the actor.
So we spawn the new actor and we destroy the old actor, the old player and just have a look at this.
Right?
So now this looks a lot better, right?
So this is the function that we want.
Um, but it is instant.
So we want to have a delay.
We want to make it very clear to the player, um, that they got defeated.
And also the spawn location is not correct yet because you can see we are just kind of in the foreground
in front of the crab.
So we also have to do that.
But you can see we get all of these error messages now because we delete the player at a critical time
when it's still being processed.
But this is going to fix itself, because we're actually going to implement a delay between getting
the last hit and actually deleting the actor.
We can just close this and in the beep.
Player.
Right.
We can just put all of this to the right side for now.
And we want to just take care of all of these overlap issues, the errors we got here and just turn
all of these things off, have a delay, and then after that respawn the player.
The first thing you want to do is to just get the capsule component, and we set collision enabled to
no collision because we don't want anything to be able to still collide with our character after it
is defeated.
And for now, just have a look at what this looks like.
Right.
So we can do this.
And I jump into the enemy and I just fall through the floor.
And in 3D levels this looks kind of weird, but in 2D levels, this has a really nice effect that is
often used in retro games.
However, we don't want the camera to keep following.
We just kind of want to detach the camera.
You kind of see the character fall out of the map, and this makes it very clear that you got defeated,
and detaching the camera is very easy.
Just create some more space here.
For now.
We can get the spring arm and we just call detach from component.
And this is going to detach the spring arm from the capsule component.
And just put this in here.
And for all of these rules, we just want to say keep world, keep world, keep world, because we want
to completely detach everything from the parent.
Let's have a look at this again now.
Right.
So I jump into the enemy and you see this kind of nice effect of falling through the floor, but actually
to have this nice effect that you see in the games, we don't just want to fall through the map, we
actually want to trigger the knockback one time.
After all, if we get hit.
So in here in the BP player on the any damage event we set that the is defeated.
Not true.
Only in that case do we trigger the invincibility.
This is fine, but we do want to trigger the knockback either way.
So here we can just shortcut through from the false here and go like this.
Double click like this.
And in this case, if we use the false, we just skip over this trigger invincibility and go into the
knockback.
We could also drag the knockback over here, but it is a little bit complicated with these collapsed
nodes because we would have to create an output and we have multiple lanes that we go along here.
So this makes it a little bit complicated.
And this is just an easy way that we can do it.
Still just by tunnelling around this trigger invincibility.
And this is just a little blueprint trick.
And now if we do it it looks a lot nicer right?
We get knocked back, the camera stays in place, and the player just kind of falls out of the map.
This is a very comedic effect you see in many games.
And now let's also input the respawn again.
Right?
So in the BP player, if we go to our begin play event on top right, the undefeated, we do this thing
with the collision and the detaching the camera.
And now let's just put in a delay here.
So delay and let's just start with a two seconds and connect here.
Go like this.
And now let's just try it right.
So I get hit I fly out of the map, I wait two seconds and the next character spawns in and I can play
normally there's no errors.
And this is how we prevented the errors from the overlap that we got before.
And we also made getting defeated look a lot nicer.
However, there still are a couple of things we can do to make this even smoother.
Let's just turn the delay into a variable now and promote the variable and just call the respawn delay.
This and open up some space.
And we want to now add a camera fade.
So before we just respawn and switch positions, we want to fade the camera out to black and then fade
in again.
To do this, we can just get camera Manager, which again is something built in with Unreal Engine like
this.
And we can say start camera fade.
And put this up here.
Connect all of these.
This.
And yes.
So what do we want to do?
We want to start from alpha zero, which means transparent to alpha one, which is the full color.
And you can actually pick the color here.
So we can pick black if we want to fade to black or white or whatever color you want.
And we want to use the same amount of time as the respawn delay.
So we can just copy paste this here and use this for the duration.
And hold one.
Finished.
Yeah.
So it doesn't just snap back to zero.
Just hold when finished.
So we stay at one and now we can just look at this right.
We do this and we jump here and you see the camera fade out but it never fades back in.
So we need to take care of this.
And we can just copy paste all of this.
And after the delay, after responding to character and between the straw actor, we can just put this
in here.
Wedge this in here.
And we want to do the opposite.
So we want to go from Alpha one, which is the color, to alpha zero which is transparent.
And here we hit the other nodes.
And now we can just look at this I jump in here, we fade out.
And we fade back in with a new character.
So this is how you do a basic respawn.
Now in here and now we just want to make this a little bit more modular.
So make sure to select this and select all of these.
The ones at the bottom here hold control like this.
Make sure this one is not included.
This one is not included.
Just all of the logic that happens after the undefeated.
Also the lower ones.
Otherwise you get an error and we just right click and collapse nodes.
And we can now drag this over here.
And just call this respawn sequence.
And one last thing we still want to do in this video is that again, if we get defeated, we just kind
of respond this random position, right?
We aren't in our lane anymore.
We kind of move forward.
We are off our lane.
So we just want to get the position of this player start and want to use this for our spawn position.
And where we do this is in the GM action right here in the respawn player function, we just have the
zero, zero, zero and the rotation.
We actually want to use the location and rotation of this player start.
And how do we do this?
Well, on begin play we first want to get a reference to this player start.
So here on Beginplay we can just get actor of class get actor of class like this and look for the player
start.
Players start like this.
Right.
And it's just going to look for this in the map and it's going to return the first one it finds.
And generally we should only have one player start.
So it's going to return a the player start.
And this is already being casted.
So we just have to save it, promote the variable and just say player start like this.
And we can then use this in the respawn player function.
Right.
So here we can just get player start.
And we can get Actor Transform like this and split the struct pin.
And we're only going to use the location and the rotation.
Again, scale is weird because if we change the scale here, our character is going to be huge, so
we don't want to mess with that.
It should always be one.
And now we just get the player start right.
If we get defeated, let's try this out.
I jump in here, we get defeated and we are spawning in the correct position.
Right?
You can see that we are now in the middle of this cube instead of being at the beginning, right?
And this kind of fixes our issue.
Of course, if the game continues, we also want to have checkpoints and spawn in later locations if
we pass a certain point in the game.
So that's what we're going to do in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.