Make a Replay System with C++ in Unreal Engine 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how the replay system and Unreal Engine works it works thanks to this system basically the demo net driver along with these things called streamers there are different types of streamers and the stream has basically recalled the information back there are things like the local file streamer so that would be used for single-player game recording and then there's also stuff like the memory streamer which is for stuff like if you've ever played Call of Duty the killcam system and then there's the HTTP streamer as well which lets you send the replays over the Internet so today we're just going to use the replay system to record some gameplay and then play it back this video is sponsored by the Unreal Engine C++ survival game course and the course I show you how to make an online open world survival game from scratch and superboss plus and hosted on Steam so you can play it with other people I show you how to do everything from installing the tools to creating an inventory system the clippable clothing weapons and vehicles and tons of other stuff the course comes with over a thousand dollars in assets alone and you can get it now on my patreon for $25 you do get lifetime access even if you cancel so if you're interested the link is in the description below and enjoy the video we're going to start by going to the Unreal project browser we're gonna make a C++ class and then go to first person you could technically do this with blueprint although it's better exposed to C++ for sure and what we're gonna do is we're going to just call this project replays so the first thing to mention is that this whole system works thanks to this thing called the demo net driver it's basically a fake net driver and it's only purpose is for recording traffic what this means is that when you watch your game playback you will only see stuff that is replicated so if your game players not networked properly you're not going to see it on the playback for example say that I shoot a bullet and I don't replicate that to other players when I watch the replay I won't actually see the bullet be shot so it's important that anything you want to show up on the replay needs to be networked in C++ recording gameplay takes place in the game instance so to access the game instance would show the sources panel here go to the Super Plus classes and then go to new C++ class we're then going to search for the game instance class select it and I'm gonna call this replay game instance and then we'll click on create class I think they use the game instance because the game instance is a class that is persistent basically it always exists even if you change levels so I think that just makes it a good fit for the replay system to be inside okay to get started inside of our replay game instance we're going to add three functions and two variables all of it's quite straightforward and easy to follow basically we have a name this is the name that we're saving our recording under we're going to have this friendly recording name so this is a name that you might display on say some UI if you wanted to show the use of their recordings and then we're gonna have a start recording stop recording and start replay function we're also going to implement a constructor so I'm just going to say you replay a game instance and then implement my constructor and then I'm going to go through and implement all of these functions as well and the constructor will just sit a couple of values I'm gonna call my recording my replay and then the friendly name is just my replay with the space and what we're gonna do is we're gonna say start recording replay and we don't need any URL options I'll explain what that is in a sec so basically inside of the game instance there's just this function that you need to call and this will actually start recording this is not exposed to blueprints so you need to call this from C++ and start replay is another function basically just play replay it's the same thing it's just inside of the game instance and then we have this stop recording replay function as well now I'm going to show you there's actually these URL options that you can pass in as well and an example of where you might want to use these is to change the strainer so as we went over before there's plenty of different types of streamers and if you wanted to use a different type of streamer then you could for example say URL options and then want to set the replay streamer override to whatever streaming you want to use and then you could go ahead and pass that in as a URL option and now it's going to use a different streamer personally I haven't played around with the different streamers because they're not very well supported but if you just leave it as the default the default streamer is the local file streamer so it's just gonna save the recording to your local hard drive on your computer we're gonna go ahead and run a local windows debugger and then we'll see if this code works so let's hook up our recording functions now I'm going to go to first person CBP open up my character blueprint and then I'm just going to type 1 2 & 3 so I'm going to say that when you press 1 we want to actually start recording you will need to get the game instance and then cast it to our replay game instance that we created and then from there we have our start recording function next we're going to do the same for stopping recording so we're just going to drag out and type stop recording and then lastly pressing the three key on your keyboard is going to actually play back that recording that we just took so we're going to connect this up here and this time we're just going to start the replay now this is not going to work unless we tell Unreal Engine that we actually want to use this new game instance that we have created so to do that we're going to go to edit project settings all settings and then if you search for instance there's this game instance class and we want to just change it to that custom line that we made let's make sure that we can see projectiles in the recording to do this we're going to open up first-person projectile we're gonna go to the replicate movement option turn that on and we're gonna turn the replicates option on as well and basically I'm just gonna play the game I'm gonna press 1 on my keyboard I'm gonna shoot a bunch of times and just run around and now I'm gonna press 2 to stop recording and I'm gonna press 3 to play my recording back it might take a little while to load but you then spawn in and you can see me shooting projectiles the only problem with us is that you can't see my player and this is because it's treating it like a networked client when we watch it back the problem is if we open up the character here you can see that the character's arms are actually only see it to be seen by the owner if you search for only you can see it sees only owners see so all we have to do is just turn that off compile and save and now I'm gonna press 1 I'm gonna run around shoot some bullets I'm gonna praise 2 and then I'm gonna press 3 and you can see now we can see my guy actually running around the level what if we want to scrub through the footage right what if I want to play back at a slower speed or you know restart the recording stuff like that how do I do that well the answer lies in the demo neck driver that's actually playing back the footage so let's take a look at the demo net driver what I'm gonna do is I'm gonna add another C++ class we're gonna add another player controller so by default when you spawn into the game you're gonna have a player controller we're gonna add another player controller that will be spawned in for playing back the footage unreal actually does this for you and I'll show you how this works so we're gonna say we'll just call it the replay player controller and inside the replay a player controller we're just gonna add a blueprint callable function and I'm just gonna say restart recording so let's implement this function by creating an implementation or you can just type this out manually so what we're gonna do inside of this function is we're basically going to grab the world check if there is a demo net driver and if there is a valid one just tell it to go to the start of its recording we will also need to include a couple of header files here and so let me show you how to use this function we've just created we're gonna click on local windows debugger and then i'll show you how to get this replay player controller set up so that we can press a key to actually restart the recording okay so we'll go to first person cpp we're going to make a blueprint version of a replay player controller they'll call this BP underscore replay controller so we'll open this up so I'm just going to make it that if you press the four key on your keyboard it's going to restart the recording to tell unreal engine to use our replay controller we need to make a new game mode so just search for game mode and then click on your replays game mode we're gonna call this BP underscore replays game mode and then inside of it there's actually this option here replay spectator player controller class just go ahead and select our BP replay controller and then the world settings which you can go to windows world settings to show go to game mode override and then go BP replays game mode now I'm gonna jump into the game I'm gonna press one to start recording and I'll run around and shoot a bunch of balls I'm being gonna press two to stop recording and then I'm going to press three to watch the recording back this is loading a separate level that the recording actually plays inside so if you ever want to exit the recording all you need to do is load the level up that you want to go to so now I can press four on my keyboard and you can see that the recording actually restarts from scratch I'm watching myself running around the level and shoot balls and I can press for it anytime and I can totally just scrub through the footage the cool thing about that go to time and seconds function as well is that you can go anywhere in the recording if you want to skip to the end of the recording you could technically do that as well you could scrub through the footage using some UI as well and you have it that's how to use the recording system in C++ with Unreal Engine sorry for the short video guys I was a little bit strapped for time but I wanted to put some sort of video out so anyways thank you for watching and I'll see you in the next one
Info
Channel: reubs
Views: 23,145
Rating: undefined out of 5
Keywords: Unreal, Engine, C++, Tutorial, Beginner, Game, Programming
Id: 8fEGDY7mg3s
Channel Id: undefined
Length: 10min 58sec (658 seconds)
Published: Tue Mar 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.