Replication | Spawning Items - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another imagine 4 tutorial today we're going to be going over another method of replication last time we did animation montages today we're going to be spawning in items or actors or anything along those lines and the method is very similar but slightly different and i'm going to go over two different methods here today as well so again we're going to be spawning in something which will replicate it so let me hit play and show you what we're going to make so i have my two characters in here i'm just going to walk over to be in front of this one and if i press right mouse button i'm going to spawn in a ball like so so i've spawned in an actor and you can see it happened on both screens and we can push it around like that and it'll be fully replicated like so you can see you can do this as many times as we want and we can do this on both of them as well the replication will work for both like this so they can both spawn them in move them about anything that you'd like so this is what we make today again just another replication of spawning in a ball or anything that you choose so without further ado let me do this code and i'll show you how i've done it so the first thing you want is you obviously want to have your blueprint actor which you're going to spawn in so for me that's just a blueprint actor with a sphere in here like so and i've just named this ball and now because this is a ball i want it to be able to move so i've selected the sphere and i've simulated the physics like so and then i've selected ball self up in the top left and i'm going to tick replicate movement and so that is just going to automatically make sure that the movement is replicated so whenever it is rolling using the simulated physics that will work and be replicated for everybody playing the game so make sure you've done that if that's what you want but if you don't want it to move you don't need to tick replicate movement we're going to compile save that minimize it and now we need to open the blueprint which you want to use to spawn something in in my example that's the third person character so i'm going to go to third person bp blueprints third person character and i'm simply finding space in the event graph and again i just want to spawn this in when i press right mouse button so i'm going to right click and get the right mouse button like so but again you can do this wherever you want and on any action mapping you choose as well i'm going to come back to that in a second because what i'm going to do first is underneath this we're going to right click and add a custom event and i'm going to name this one spawn item on server like so and i'm going to change the replicates from not replicated to run on server because this is spawning the item on the server and then underneath this i'm going to right click and add another custom event naming this one spawn item on client because this is going to spawn on the client and i'm going to change replicates from not replicated to multicast which is going to run it everywhere so run on all clients and servers so you can also change that to be spawn item on all instead if that makes more sense for you and the spawn item on client is where we're going to actually spawn in the item so i'm going to drag out this and get spawn actor from class like so and i'm going to drag the class onto the spawn item on client like so and the reason i'm doing that is because then we can use the same custom event to make it dynamic to spawn in anything that we want and you can do the same with the spawn transform as well but i'm not going to bother because i'm going to want this to be the same for each one although actually just to show you i will do it so spawn transform i'm also going to drag into there as well and add pin to node like so so now spawn item on client is going to go into a spawn actor with the class and spawn transform coming from this custom event as you can see there and i will go above that and the spawn item on server we're going to call function spawn item on client like so and again i'm going to drag these in so class we're going to add pin to node spawn transform there as well so they're going through there and i'll go over all this again in a second and go back up to right mouse button or wherever you want this to spawn press is going to be call function spawn item on server like so and now this is where we're going to actually set up what we want to spawn and where we want to spawn it so i'm going to give myself some nice room like this so again this can be somewhere else if you want this could be enough function so you can set this up however you like maybe you equip something for example a book which allows you to place different things down kind of like the forest did always in your inventory or anything on those lines again just call the custom event of spawn item on server when you want to spawn something in so you can set the class and the spawn transform as well but for me in this example the class is going to simply just be my ball actor as you can see there and the spawn transform and the spawn transform what i'm going to do is i'm going to make transform like that the location is just going to simply be directly in front of the player so to do that i'm going to right click and get the actor location like so underneath this i'm going to right click and get the actor rotation and the reason i've got the rotation is so i get the forward vector so the return value of the rotation i'm going to get forward vector retail value of that i'm going to get a vector multiplied by a float and i'm going to multiply this by 5. so it's going to spawn 5 units in front of me maybe even 10. and then return value of the getax location it's going to go into a vector plus a vector connecting that in there like so and then that is going to go into the location so now that it's going to spawn the ball 10 units in front of the player and it's going to spawn the ball like so then compile and save and that should be the code done for us so like i say what's going to do is when we press the right mouse button it's going to call at the event of spawn item on server inputting the class as the ball and the spawn transform as 10 units in front of the player and then it's going to go to the spawn item on server and then call the spawn item on client which is now also running on the server because we've done this here it's going to run on the server again transferring over the class and spawn transform and then it'll go to the client and run this on all the clients and just all systems connected and again can input the class and transform into the actual spawn actor so this is where it's going to actually happen so i'm going to compile save and we can hit play to test this out so what i'm going to do is again just move these over and then if i go in front of this and i click the right mouse button it's going to spawn in the ball replicated on both ones and we can move it like so and again this will work for both of them like this this does work perfectly for how we want what i'm also going to do is show you another method now so what we can do is the right mouse button we can just put straight into a spawn actor so what i'm going to do is just duplicate this and disconnect that there get a spawn actor up here and then connect in the right things so we press right mouse button it's just going to spawn it in now ordinarily what this would do is it would just only work for the player or for the character which has tried to spawn it in however what we can do is once you've got that as we see there like so compile we should see that's working perfectly we can go over to the ball untick replicates movement and just take replicates and that will do this all automatically for us so if we hit play we can right click and it's going to do it for us like so let me again get the big screen up like this as you can see it's doing this all automatically for us so you might wonder what was the point of doing it the other way for the first time what's the point in doing all of this other code here well really the thing is you might not want it to be replicated all the time so what this will do is it will just constantly be replicated you might not want that for example if you might only want one player to be able to see it or in certain cases you might want to change it so one player sees it one player doesn't or anything along those lines really and it's just different methods as well so you might want to do this you might not this kind of gives you more freedom as well to just change it how you want to but again really it's your choice to do whichever way you want but i thought i'd just show you both options and both ways of doing it instead just so you have both options and you can choose which one to do again this way also works perfectly fine and that's the way that i'm going to go with just because i prefer it do whichever one you choose just make sure replicate movement is ticked but i think that'd be this video which definitely want to do we've set it up so we can spawn in an item and it'll be replicated perfectly on both the clients and the servers as you can see here like so and again we've set up a dynamic system so you can just call the same custom event to do whatever you like and it can be reused over and over again without having to remake it each and every time so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one [Music]
Info
Channel: Matt Aspland
Views: 22,511
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, ue5, unreal engine 5, replication, replicating, replicate, spawn, spawning, item, items, actor, actors, from, class, custom events, event, custom, custom event, server, client, run, on, all, multicast, animation, ue4 replication, how to replicate, online, multiplayer, multi, player, multi-player, events, cast, play, players, see, not working, working, not, movement, rolling, roll, physics, simulate, can, why
Id: sbfcBJd9gk4
Channel Id: undefined
Length: 8min 50sec (530 seconds)
Published: Sat Jun 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.