How To Set Up A Replicated Weapons System For Multiplayer - Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys mizo frizzo here and in this tutorial I'm going to be showing you how to set up a replicated weapon system with simulated physics pickups so as you can see here we've got two players one of them simulating a listen server and if I go over to this weapon and pick it up you can see on both instances this player has this weapon and he can also pick up a secondary we can switch between primary and secondary so what we are setting up today is a kind of basic two weapon system that works quite nicely but what you'll notice is if I go over to a weapon that I already have and pick it up it will drop a pickup for all players so it's spawning a simulated physics pickup for the other player who can also go over to it and pick it up uh you know we can switch between weapons and being unarmed uh we can even switch the skin of the weapon so you can see when I press P here the skin of the weapon is changing and if I go over to a weapon with a different skin and pick it up it will pick up a weapon with that skin and drop a pickup with that skin and the other player can even go over to that and pick that up and they've picked up that weapon with that skin on it we'll also be setting up some very basic firing logic so you can see this is replicated um replicated animations and particle effects and replicated procedural vertical aim offset this is quite a big tutorial but I'm quite proud of what I've come up with so without further Ado guys let me show you how to do this all righty guys I'm going to create a new project using the third person template but just for the record it doesn't matter what kind of game you're making if it's third person or first person or true first person it doesn't matter everything will work the same so I'm going to create this new project and leave everything as default and hit create and straight away the first thing I'm going to do when the editor fires up is head on over to the unreal Marketplace to download some free weapon packs so the first one I'm going to search for is military weapons and you can filter for free and I'm going to grab this military weapons Dark now I have heard that this is not free everywhere for example I've heard it's not free in Australia um if it isn't free where you are I'm going to give you an alternative in a moment but just for now I'm going to add this to my project and you'll see it says no compatible user projects found but you can search for your project and click show all projects select your project and then select the most recent compatible version which is probably going to be 4.27 and hit add to project now the alternative is something called the FPS weapon bundle so if I search for that oops typo there and filter for free you'll see this FPS weapon bundle right here now this comes with some very nice skeletal measures but it does not come with any animations or particle effects so any part of this tutorial where we set up animations and particle effects for our weapons you will need to Source those from somewhere else um but with that in mind I'm going to add this to the project anyway because very nice skeletal measures here um you can search for your project click show all projects select your project and then select the most recent compatible version which again is probably going to be 4.27 and hit add to project once that's downloaded and added you'll notice a couple of new folders here FPS weapon bundle and Military web dark I'm going to be working with the military weapons Dark today so I'm going to head into that one and weapons and what I'm going to be using is this assault rifle B and this pistols B and the first thing we need to do is create some static measures for our simulated physics pickups uh so I'm going to open up assault rifle B but first i'm just going to contrl C the name open that up and then up the top here I'm going to click make static mesh I'm going to find this same exact folder that that one was in so military web dark and weapons and I'm just going to call this assault rifle B static mesh and hit save and close this and straight away open up that static mesh that we created and I'm going to click Collision here and just add box simplified Collision so this is now a static mesh of our weapon and it has very basic box Collision which is all we need so I'm going to close that and now I'm going to do the same for pistols B so contrl C the name open it up click make static mesh find the same folder let's call it pistols B static mesh hit save close that open up the static mesh collision and add box simplified Collision now this one has Collision very nice save and close that and now we can get started on creating our blueprints so just to stay organized I'm going to create a new folder within content I'm going to call it underscore Main open upore main make another folder call it interfaces this is going to be for our blueprint interfaces and also one called props and within props create one called pickups and one called weapons like so head into weapons and I'm going to create the parent class for all of our weapons which is going to be a skeletal mesh actor component so I can right click and create a blueprint class down here I can click all classes and search for skeletal mesh component here under actor component so it's going to be an actor component of type skeletal mesh skeletal mesh component hit select and I'm just going to call this bpor uh weapon component Master you can call this whatever you like weapon component parent weapon comp component base um but the principle is the same so I'm just going to call this weapon component master and I'm going to leave that one for now but I'm also going to create a couple of enumerations so right click and go to blueprint and enumeration I'm going to call the first one enum uncore fire mode I'm also going to duplicate that and call the other one enum uncore weapon name can open up enamore weapon name and and straight away add a few enumerators this is where we're going to store the name of our weapons but we're not actually going to give them proper names we're going to give them integers and the reason is that this is just going to be slightly easier to uh pass over to our animation blueprint to tell our animation blueprint what animations we should be playing so I'm going to create three enumerators call them 0o 1 and two and then instead of naming them here I'm going to keep the name over here in the description so the first one is going to be unarmed second one is going to be pistol oops and the third one is going to be Assa assault rifle save and close that and open up enom fire mode I'm going to add two enumerators to enom fire mode I'm going to call the first one semi- Auto and the second one full auto save and close that and we're not going to do anything in BP weapon component Master just yet we are going to head over to our props and Pickups folder and I'm now going to create the parent class of all of the pickups so I'm going to right click and go to blueprint class let's just be of type actor and call it bpor pickup Master open up BP pickup master and straight away in the components tab here I'm going to add a static mesh component let's call it pickup object drag that onto the scene rout to make it the root I'm also going to add a sphere collision and you can call that pickup radius and make sure that pickup radius is parented to the pickup object like so you can tell it's it's parented because it'll be slightly indented here and part of this little drop- down menu so I'm going to select pickup radius and change the sphere radius to 100 that's a size that I found to be quite good I also going to select the pickup object and I'm going to change the static mesh to just any old mesh just the first thing I see which is this sphere here and the reason we're going to select a static mesh here is so that we can give it some attributes that will be inherited in the child classes so with the static mesh and sphere selected we can search for tick and uncheck start with tick enabled we don't need tick we can also search for physics and check simulate physics I'm also going to check mass and change the mass to about 60 and the linear damping and angular damping I'm going to change to 0.25 like so we can also search for replicates and um what we need is replicate movement so that's not there class defaults yeah so click class defaults search for rep and find replicates and check replicates and also replicate movement so this is what's going to make our replicated uh simulated physics pickups work for all the players so check replicates and check replicate movement and close that and we also want to just add a variable here and we'll call it weapon to spawn and you can find your weapon component Master Class reference so it's going to be a class reference to our weapon component Master like so we can save and compile that and close that and then right click on that and create a child blueprint class and I am going to call this one uh bpor primary weapon pickup like so primary weapon pickup so we're going to have a class for our uh pickups that will spawn a primary weapon and a class for our pickups that will spawn our secondary weapon there are different ways of doing this you could do it with an enumeration for example um but for now uh this this is how we're going to do it so we're going to have a class for our primary weapon pickups and a class for our secondary weapon pickups uh one mistake I did make here is in pickup Master weapon to spawn we're not actually I'm just going to contrl C that name and then delete that variable we don't actually want that in our pickup Master we want that in these other ones so um get rid of that and open up BP primary Ary weapon pickup and then in BP primary weapon pickup we're going to add that variable weapon to spawn and it's going to be a class reference to our BP weapon component Master Class reference nice um now we are going to set up some logic in here for when we interact with these and spawn our weapons um but we are going to do that in a moment for now we're just going to add this variable and I'm also going to duplicate this one and call the other one secondary weapon pickup oops so these are both children of pickup Master which is important to know and if we open up BP secondary uh weapon pickup it has everything the same it's it's just duplicated the only thing that's changed so far is the name name so that's fine that's all we want to do in here um we can we can create the pickups for our weapons now so we can right click on primary weapon pickup and create a child blueprint class and I'm going to just call it bpor assault rifle pickup like so you can open that up and you can change the pickup object to the static mesh of the assault rifle that we created assault rifle B static mesh like so and that's all we're going to do in here for now so compile and save and close that and we're also going to right click on secondary weapon pickup and create a child blueprint class of that and that's going to be our pistol pickup can open that up and also change the pickup object to the pistol pistols B static mesh that we created like so and again that's all we're going to do in here for now nice so assault rifle pickup is a child of primary weapon pickup and Pistol pickup is a child of secondary weapon pickup and if you wanted to create more pickups for uh say more Primary Weapons you could right click here and create a child blueprint class or you can simply duplicate another primary weapon pickup and change the skeletal mesh so you could just duplicate this one change the skeletal mesh um just going to open full blueprint editor here change the pickup object to a static mesh of a different weapon here you would also need to change the weapon to spawn in the class defaults which we haven't set up yet so um I'm just going to to delete that for now we are just creating the pistol and the assault rifle pickups so head back over to the weapons folder here and now we are going to open up our BP weapon component master and we are going to change a few things in here we're going to start adding some variables so we can add a variable call it fire mode and that's going to be the numeration fire mode that we created we can duplicate that one and call the other one weapon name and that's going to be our enum weapon name the next one is very important it's our pickup class so we call this one pickup class this is the one that we're going to use to spawn the pickups of the weapon when we drop it and it is going to be a class reference to our our pickup master so BP pickup Master Class reference like so um we'll we'll create a few more variables that will just sort of be um used in some of the bonus content I'm including so I'm going to create one and call it firing animation and this is going to be an anom sequence ANM sequence object reference now I'm just going to check if that needs to be replicated it won't need to be replicated because actually we're going to replicate the entire weapon component to pass any of these over so we don't need to change any of these variables to replicated or instance editable or anything like that let's uh create another one and call it socket name and that is going to be of type name let's duplicate that one and let's call it uh holster socket name like so very good and let's create one more and call it skin and this one um this one will need to be replicated I believe yes this one will need to be replicated because it it's uh it's something that we're kind of trying to pass over to other players so this is kind of why I've included the skin changing mechanic um just to show you that an example of something that does need to be replicated so this one is going to be a material instance oops material instance object reference and if you select this one over here in the details panel find replication and change this one to replicated so skin is a very good example of something that needs to be replicated um you know we we are replicating the entire weapon component um so that might work actually without that being replicated but yeah we're going to change this one to replicated uh we are also going to go to the class defaults and search for replicated and we're going to check component replicates this is very very important uh this will be added as a component of our character um and our character will be replicated so if you didn't check this a lot of stuff would work but then as you start to add functionality such as holstering the weapon on your back or something like that and you're trying to pass the holster socket name through to the server so that the server knows where to holster it on the other players instances um you know you will start to find problems so you need to make sure that in BP weapon component class defaults you search for replicated and check component replicates like so very good okay we can compile and save and close that and we're now going to right click on that and create a child blueprint class and I'm just going to call it BP assault rifle can open this up and over here in the class defaults change the skeletal mesh asset to the skeletal mesh of the weapon that you want this to be like so and can compile and save and close that and ah actually while we're there let's uh change the class defaults so BP assault rifle um in the class defaults you know if you're looking at the this version of the editor it will be down here in default right here and if you were in the full blueprint editor class defaults you'll find this stuff over here so the fire mode for the assault rifle I'm going to change to full auto the weapon name as you remember for the assault rifle was two the pickup class will be the pickup of the assault rifle BP assault rifle pickup and the firing animation let's find fire rifle W I'm just going to browse to that to make sure that it is the correct animation for that weapon so yeah that is the correct animation for that assault rifle very good um because if you select for example if you downloaded military weapons silver as well and you selected the fire animation for the different assault rifle which may have the same name you you might notice some freaky stuff going on uh we can name our sockets while we're here so I'm just going to call this assault rifle arore bcore socket okay and I'm going to contrl C that for holster socket name and call this ARB underscore holster socket like so and the skin we're not going to mess with the skin just now I'm going to show you a very quick and hacky way of changing the skin uh towards the end of the tutorial so that's just a bit of a bonus we're going to leave that for now we can compile and save and close that and now in our weapons folder we could right click on BP weapon component master and create another child of that but because BP assault rifle is a child of weapon component Master we can also just duplicate that one and I'm just going to call it bpor pistol open up BP pistol straight away you can change the skeletal MH asset to pistols B skeletal mesh and we're going to change the uh class defaults right here so when something has a parent class like we've created here it will often open up in this editor mode you can open the full blueprint editor but the beauty of this is that if you wanted to set up a new weapon like we are doing now we duplicated the assault rifle blueprint we can literally just change a few things in here and the whole weapon is set up so let's just change the skeletal mesh asset to pistols B let's change the fire mode semi-auto the weapon name for the pistol is one uh the pickup class will be the BP pistol pickup and let's find fire pistol W quickly browse to that one and open it up to make sure that that's the correct firing animation for that skeletal mesh and it is and let's change the socket name to Pistol socket and pistol holster socket and once again I'm just going to leave that skin blank compile and save and close that and now we can start looking at the logic to spawn these weapons on our character now uh this is not I mean this is going to be replicated so if you've seen my other weapon system with simulated physics pickups tutorial this is going to be somewhat more comp complicated than that because it's replicated but also because we're doing primary and secondary weapons a kind of a two weapon system so um the very first thing we're going to do is set up an input action so let's find our inputs folder which for me is going to be in third person and input and actions right here I'm going to right click and find input input action and I'm going to create a new input action called iore interact I'm going to open that up I'm going to add a trigger I'm going to change it to hold I'm going to drop down this index change the threshold to 0.5 for half a second and check is one shot now I noticed in my previous weapon system tutorial a lot of people forgot that we did this even though I mentioned several times to hold f uh when we pick up a weapon we are setting this up so you need to hold it for half a second to pick up so just keep that in mind I'm going to save that and close that and then back out to my input folder and open up my IMC default drop down the mappings here add a new mapping and it's going to be our IIA interact going to drop down IA interact and click this little keyboard icon and then I'm going to press the F key to set this as F and that's all we need to do in here so we need to hold f for half a second for IIA interact to fire off I'm now going to open my third person and blueprints BP third person character and I'm going to find my IIA interact right here enhanced action events IIA interact and this is where we're going to be setting up our spawning logic or our interact logic so this is going to use a blueprint interface so we're going to set that up right now we can go to our underscore Main and interfaces folder and right click and go to blueprint blueprint interface and I'm going to call this BPI uncore interact I'm going to open up BPI interact and rename the function interact you'll see right away it will ask you to rename the function so you're going to rename the function and we are going to create an input here and I'm going to call it character picking up and it's going to be an object reference to our character so for me bp3 person character object reference so we're going to pass through when we call this function on our pickups we're going to pass through a reference to which character is picking it it up so we now need to add that blueprint interface to our pickups so we can go to pickups and pickup master and class settings right here and over here interfaces we can add the BPI interact interface and over here and interfaces you'll see that it's created this little uh interact interface right here now we're not going to do the logic in here because this is for all of our pickup items we're going to do special logic for um our for each weapon um our primary weapon and our secondary weapon so what we can do is we can start in our third person character and just uh first get overlapping actors and the class filter here is going to be our pickup up Master Pi BP pickup Master we can get a copy so it's going to get the first overlapping actor of type pickup master and then just to do an additional filter here we're going to um check does implement interface and the interface will be our BPI interact put this on a branch and now if we hold F and we're overlapping with an actor that doesn't isn't BP pickup master and doesn't implement the interface BPI interact this will ring false and this won't fire off if it does um what I want to do now is just check um check is it a primary or is it a secondary weapon actually we might we might just add this in a moment um no no we'll we'll add it now so I'm going to off of this get a copy I'm going to get the class so I'm getting the class of the overlapping object and then I'm going to check um class is child of class is child of and this is going to be our primary weapon pickup BP primary weapon pickup so I'm checking if it's a primary put it on a branch like so and if it is not a primary I'm going to check if it's a secondary so I'm going to duplicate these two nodes right here and plug the false down into this one make sure you plug this get class into the test class right here and this is going to be our BP secondary weapon pickup so if it is not a primary going to check if it's a secondary it's got to be one or the other now before we continue let's um let's add these components into our variables right here the uh BP weapon component that we've created so what we can do is check um add a variable call it primary weapon and that's going to be an object reference to our weapon component Master BP weapon component Master object reference like so and you'll see this will automatically put it into its own little subsection here called components very good I'm going to duplicate this two times these are all going to be object reference to BP weapon component the second one I'm going to call it secondary weapon and the third one I'm going to call it current weapon or equipped weapon would probably be more accurate equipped weapon like so and these will all need to be replicated so select primary weapon and change replication to replicated change secondary weapon to replicated I probably should have done this before I duplicated it but that's okay change all of these to replicated like so very nice okay so now that we've checked if what we're overlapping is a primary or a secondary we need to set up this logic here what I'm first going to do is check if I already have a primary so I can get my primary weapon here and then right click and convert to validated get plug this in here and if if we do have one we're going to drop a weapon so we need to set that up but uh if we don't we're going to just straight up spawn it and we're going to spawn it with a um custom event called interact um so we need to run this on the server and multicast it to all um all players so what we can do just below here is is create a couple of custom events I'm going to call this one Ros for run on server interact I'm going to select it I'm going to change replicates to run on server and reliable I am also going to create a custom event called MC for multicast uncore interact and this one I'm going to multicast and reliable and and on uh This MC interact what I'm going to do is call our interact message so I'm going to send this out and we can take the Target and plug it in here and we can also take character picking up and plug it in here and then on the runon server interact we're going to call the um multicast interact and then again we can take the Target and plug it in here and the character picking up and plug it in here and now when we call run on server interact it will we'll be able to pass through the Target and the a reference to the character that's picking it up so that's what we're going to do up here so if we don't have the weapon uh we are going to just interact um call the runon server interact like so the character picking up this is very important will be a reference to self so when we call this on the server it's going to call it on all of the clients and it's going to pass through a reference to the self which is this instance of the third person character that's picking the weapon up Um this can be pretty complicated stuff we're going to drag off of this get a copy here and that's going to be the target going to add a reroute node here and just put this straight down through the middle here and we're going to do the same down here so uh exactly the same we're going to duplicate these two nodes right here and if it is a secondary what we first want to do is grab our secondary weapon here right cck click convert to validated get and if we do not have one we can straight up call that interact off the is valid here we're going to drop the weapon um but we'll set that up in a moment so just going to double check here that everything is set up we just need to plug in the Target right here so this is going to interact with the pickup and that's going to spawn our weapon um yeah this is like I said a bit more complicated than my my other uh my other tutorial but um follow along and everything should be absolutely fine so right now what we need to do is um go over to our our pickups and Implement that logic um that spawns the weapon so it's actually going to be two different events within our character right here um one is going to be spawn primary weapon and one is going to be spawn secondary weapon so let's start by creating custom event uh call it spawn primary weapon and let's also create one called spawn secondary weapon nice we're just creating these so that we can call them from our pickups so what we're now going to do is um so we're calling this Roos interact we're calling the interact message right here um and the target is the overlapping actor so we need to go to our so we'll start with our primary weapon pickup and open this up and over here you'll find the interact you can double click that and we've got this very Nifty interact with the character picking up right here so this uh this because our pickups are replicated um we only need to run this on the server we do not need to multicast um because replicated um replicated actors are automatically spawned on every client so we just need to run this on the server so we're going to call a custom event in here called uh run on server spawn weapon and because this is our primary weapon pickup what we're going to do is spawn our um primary weapon event so we can drag off this character picking up and Spa spawn primary weapon but we're not actually going to do it straight off of here we're going to do it on the server um so before I forget need to select this one and change it to replicates run on server and reliable and what we can do is plug the target here into here can also grab the weapon to spawn and plug it into no we need to create that input right here so um ah yeah we will need to create that in in our character so we can go back to our character right here so one thing I forgot to do with these is create an input and call it weapon to spawn and that's going to be our class reference to it our weapon component master so weapon component Master Class reference like so I'm just going to contrl C this name because also on this one we want to create that same input weapon to spawn BP weapon component class reference so now over here we have this one when we call this we can plug this into our Ros spawn weapon and then off of event interact here we're going to call our Ros spawn weapon and the target is going to be the character picking up and the weapon to spawn is going to be the weapon to spawn like so um that's all we have to do in here but we now have to do the same for our secondary weapon pickup and call the spawn secondary weapon in instead so we can compile and save and close that and open up our secondary weapon pickup call this interact function right here and off a character picking up we can spawn secondary weapon call that custom event but we're not going to do that off of there we're going to do it off of a RPC so call Custom event uh call it r spawn weapon change it to run on server and reliable plug this in here plug in the Target and the weapon to spawn off of event interact call the r spawn weapon Target will be character picking up weapon to spawn will be weapon to spawn like so so here once again guys very complicated stuff um but we're calling this interact which is running on the server which is calling the multicast interact so on every client it's calling this uh BP interact and when it interacts with the item the item is going to uh run an event on the server that calls the spawn weapon in our character it's passing over the character so it knows what Target this custom event is in and it's also passing over this weapon to spawn which we set in each of our pickup items so now when we interact with a pickup uh if it's a primary pickup it's going to trigger this uh spawn primary weapon if it's a secondary weapon it's going to spawn uh trigger this custom event spawn secondary weapon all righty so the first thing we want to do when we spawn a primary weapon is just check if we already have one so we can get our primary weapon right click convert to validated get and if we don't already have one we want to just go ahead and add that weapon component so you can drag off of this weapon to spawn and um sorry it'll be add component by class add component by class off of the is not valid add a reroute node here keep things neat going to drag this out a bit because um if it is valid we want to destroy that component destroy component and the target will be that primary weapon and then once it's destroyed we can then add the new one like so uh the next thing we want to to do is set this as our new primary weapon so we can set primary weapon and since we're going to automatically equip it we are also going to set that as our equipped weapon like so um now just before we attach it I'm also going to set a new variable which we're going to use to uh change the animations that our character plays so we're going to set that up a bit later but right now I'm just going to add a variable here I'm going to call it current weapon int for integer change it to type integer and also make sure that this one the replication is set to replicated otherwise you will not be able to replicate the animations to all the other players so current weapon int of type integer set to replicate it and what we're going to do off of the equipped weapon here is get the weapon name and then I'm going to convert that to an integer two integer whoops and then I'm going to use that to set current weapon int like so so anytime we pick up a weapon it's going to set current weapon int to the weapon name which as you remember in the enumeration we just made an integer now what we need to do is attach component to component mesh actually no mesh is going to be the parent not the target the target is going to be our primary weapon so we can get the primary weapon here plug it into Target and then the socket name you remember we stored within that weapon so we can get the socket name get socket name is this one plug that into the socket name right here and we want to change all of these to snap to Target like so um we'll set up the holster weapon stuff uh later um it's it's not too difficult um but we'll we'll just try and attach these weapons for now so to test if this works we just need to set up the sockets on our skeleton so let's do that right now um what you can do if you unsure of where your skeleton is you can click on your characters mesh here and browse to the skeletal mesh asset you can open that up as you can see here the skeletons right here but I'm just going to show you a way of double checking what skeleton your character is using so you can find the skeletal mesh and then you can click this skeleton button up here if you highlight it it'll show you the path if you click it it will just open up the skeleton so here we have the skeleton of our character and to make sure you name these sockets correctly what you want to do it in your weapons folder excuse me is fine so we're going to do the assault rifle first cuz we set up the primary logic I'm going to open up BP assault rifle and in the class D defaults here find the socket name ARB socket I'm going to contrl C that go back to my skeleton find the right hand and on the right hand I'm going to right click and add a socket rename it to our ARB socket right click on the socket and add the preview asset assault rifle B like so and I'm going to turn off grid snapping and rotation snapping and just put this in just about the right place like so while we're here guys we might um download some animations um to to switch to when we pick up the different weapons actually we'll do that in a moment we'll just uh we'll just do the sockets for now so you can come back here later and you can adjust the socket but I'm just going to put it in just about the right place place in their hand there and I'm going to change the scale cuz the scale is absolutely nuts I'm going to change this to 0.8 on the X the Y and the Z and make it a little bit more compact like so nice so we've added the socket and uh for the record this scale here if you change it on the socket it will change it anytime you attach something to this socket that's going to be the scale so you don't need to do it anywhere else for your actual weapon component um you might want to change the scale on your pickups if you wanted the pickups to be the same size as this one you want to change the scale on the pickups to 0.8 but for the record I'm just going to change it here that's all I need to do um now that I've added this socket I should be able to pick up a weapon so uh a primary weapon I will not be able to pick up a secondary weapon yet but if I put my BP assault rifle picker up out here and I'm just going to rotate this a bit so that we see the simulated physics in action I should be able to press play walk over to this and hold F and it has not worked why hasn't it worked we just need to set the weapon to spawn in the pickup so let's open BP assault rifle pickup and change the weapon to spawn to BP assault rifle and let's also do the same for the pistol pickup change the weapon to spawn to the BP pistol sorry about that guys that's very important step indeed and now if I hit play and I go and grab that it will spawn the weapon in my hand like so um one thing I forgot to add as well is just to destroy the pickup so in my primary pickup primary weapon pickup uh once I've spawned the weapon I also want to to just destroy actor Target is the self like so so if I go and pick that up it will destroy the pickup like so nice let's do the same with the secondary weapon so the logic is pretty much identical but we're doing it with the secondary weapon so I could just copy this and change a few of the things but just to make sure I don't miss anything I'm going to set it all up uh from scratch again so I'm going to get my secondary weapon right click convert validated get and then if we don't have a weapon we want to add component by class whoops if we do already have a weapon we want to destroy component destroy component plug in that Target and then after destroying add and now we're going to set the secondary weapon and the equipped weapon as that new spawned weapon uh we can actually just duplicate these three like so plug in the Target here so we set the weapon int as the weapon name let's duplicate this attached component to component so that all of these are set to snap to Target and we want to get our secondary weapon that's going to be the target the mesh is going to be the parent and the socket name will be the socket name whoops of the secondary weapon like so nice and that should be all there is to that if I drop the secondary weapon into the level as well ah we need to create the socket of course we also need to create the socket so in the BP it'll be the actual BP pistol check if I have that open BP pistol we created the socket name here so let's contrl C that socket name go back to our skeleton and I'm just going to right click on the AR socket and remove all attached assets I'm going to right click on the hand and add a socket it's going to be the pistol socket cuz you might want your different weapons to socket slightly differently if your skeletal measures are different or the scales different or whatever um so this is a very handy way of storing the socket names in the actual weapons and then each time you equip it um it attaches to that specific socket so we can add the preview asset pistols B and just going to put this more or less into the right position like so the scale is way too big so again let's do 0.8 and that looks nice to me put this more or less into the right position like so again you can come back and adjust this in your own time but that'll do for now and now that we've added that socket we should be able to pick up that weapon now we need to set up the holstering and the dropping of the weapons because if I pick up this assault rifle and then I pick up this one you see if you look very closely that it's actually spawned uh both both weapon meshes in there and they're kind of overlapping so the pistol's in there somewhere um so we need to holster um our other weapon onto our back and we also need to set up the dropping of the weapons so the holstering of the weapons is not too hard let's set that up right now basically uh what we're going to do on the end of the primary spawn is check if we have a secondary and on the end of our secondary spawn we're going to check if we have a primary like so so once again when we spawn a primary at the end we're checking if we have a secondary and if we have a secondary we want to holster it we're going to do this on the server and on all clients so we need to create a custom event let's call the first one run on server underscore holster weapon something like that and I'm just going to contrl C this and create another custom event and call it MC for multicore holster weapon this one is going to to be multicast and reliable this one is going to be run on server and reliable and here we are going to call the multicast holster weapon and then on the multicast we are going to attach component to component so we can grab one of these and duplicate it and the Target and the um holster name need to be passed through so what we can do is just put the target onto here and then this target onto here and then if we call here off of is valid we call uh run on server holster weapon this target will be the secondary weapon and then down here we can also call it here and primary weapon will be the Target right there let going to move these out a little bit and then off of Target right here we can get the holster name what did we call it do we call it holster socket get holster socket name right here ah that will ah it's because uh we didn't um I need to specify this as an object reference to the character so on the MC holster weapon on the inputs here that were automatically created when you drag this pin on I'm going to um change that to an object reference to our weapon component so weapon component Master object reference like so and I'm going to do the same on the holster weapon change this to weapon component Master object reference and now we should be able to get the The holster socket name like so my mistake plug that into the socket name like so and also just grab your mesh and plug that into the parent like so and nice we now need to create those sockets on our skeleton as well so once again we are going to need the socket name that we wrote into our class defaults let's do the pistol first so p P Hol pistol holster socket I'm going to contrl C that go to my skeleton and then where you want to create this socket is up to you I am going to parent my pistol holster to spine one so I'm going to right click on spine one and add a socket call it pistol holster socket paste in that name make sure it's exactly correct right click add preview asset and that's going to be our pistols B skeletal mesh and then I'm going to change this to World space move this out to the side rotate this a little bit like so and put it where I want it so I'm going to put it right here like so and let's change the scale so it's the same scale as the equipped weapon very nice that is all we need to do with the pistol socket we now need to do the assault rifle so going to BP assault rifle class defaults find your holster socket name for the assault rifle go back to the mannequin and this one I'm going to parent to spine five and put it on her back so I'm going to right click on spine five add a socket rename it to the holster socket for the assault rifle add preview asset assault rifle B skeletal mesh and straight away change the scale to match the scale of the equipped socket rotate this and move it into position and I'm just going to put it on her back like so make sure it's not clipping through too much just place it right there like so nice and now if I hit play I should be able to pick this one up go into a hand if I pick this one up it should holster that other one and pick up the new one and as we can see here I forgot to destroy the secondary weapon pickup so we also need to go into the secondary weapon pickup and just here just add destroy actor nice we're getting there we got most of the hard stuff out of the way guys nice very nice um and if I set this to uh replication so if I go to net mode play as listen server and change the number of players to two and I grab this player start and duplicate it over here rotate it around so they're facing each other and also select new editor window for the pi got the server and the client right here and this should all be replicating so you want to make sure that both instances this person is holding an assault rifle and in both instances they're holding the correct weapon and the correct weapon is Holsted so that's all replicating that's working very nicely if I just um if I just duplicate these a couple of times like so we can now just double check that everything is replicating very nice very good both characters are holding the correct weapon at any given time and the correct weapon is Holsted at any given time like so ah we have not set up um to drop a weapon or if we already have a weapon so we can do that now if I try to pick up a weapon that I've already got got it's not going to do anything so we need to start adding that functionality here where we interact if we already have a weapon we want to drop a weapon and then interact so just going to make a little bit more space here and the drop weapon is going to be a custom event and then we're going to spawn a pickup on the server so um I'm just going to put these interact ones over here cuz they're a little bit smaller they can fit there and down here I'm going to add a custom event and it's going to be drop weapon I am also going to control C that name and I'm going to create another custom event and I'm going to call it run on server uncore drop weapon this one is going to be run on server and reliable and this is going to on the server spawn actor from class like so and the weapon to drop we will pass through um we will also need to pass through a spawn transform from our character um so that that is replicated in the right in the right place so on drop weapon right here we can call our run on server drop weapon and drop weapon here let's add a couple of inputs um the first one will be a spawn transform spawn trans offer transform there we go and and that's going to be a transform obviously and the weapon to drop will be an object reference so one more and weapon to drop will be an object reference to our um weapon component Master weapon component Master because within our weapon component if you remember correctly um I don't have it open here in our weapon component Master we created this pickup Class A Class reference to the pickup that um should be dropped if we want to drop a weapon so we want to make sure that we've set this in each of our weapons so in BP assault rifle um we did we set up pickup class as the assault rifle pickup and in BP pistol we set the pickup class as BP pistol pickup so that's all set up there we just need to pass that through so let's um let's call this drop weapon uh let's also add the weapon to drop actually um in this custom event so we'll add that input as well and I'm just going to contrl C weapon to drop paste that in there and that's a BP weapon component Master object reference we can call this over here we're just going to call drop weapon so if we already have a weapon drop weapon and that's weird that's called it new Pam um and sometimes that will that will cause errors so sometimes when you I've noticed this lately if I call a custom event the um the type is correct but sometimes you'll hit compile and you'll get errors and and the name of that input is not passed through so maybe make sure you compile before you call this um drop weapon and now it's saying weapon to drop um so yeah if you get errors sometimes it's the engine can be a bit buggy you just have to compile before you call the custom event and then this is going to plug right in here and we're also going to need this down here the secondary it's going to if we do already have a secondary we want to drop it and then interact with the item like so so a couple more things to set up down here is um the spawn transform we can plug that right in there and the weapon to drop we can get pickup class and the pickup class is going to be spawned like so and we just need to set up the spawn transform and pass it through here so we can get actor rotation actually um just get actor transform and right click and split that struct pin and um we can also split the struct pin here on the spawn transform on the call of this event and we can plug that um actually no yeah so we'll drag off of the rotation and get the forward vector and multiply that by a float and I'm going to make this 35 so this is going to be spawned 35 cm in front of our player and the thing here is um the radius of your capsule component really matters here see the caps radius is 35 so this will be overlapping with our capsule component and it will force the pickup to be pushed out of the capsule component so it will sort of create it's um it's kind of an unintended mechanic at first it will it will the pickup that drops in front of you will sort of be pushed out in front of you um if you set this to 100 it would just spawn one meter in front of your player and just drop to the ground um so you can set this up you like I'm going to set it up like this I am going to add the location of my actor to the forward Vector multiplied by 35 plug that into the uh location you could also plug the rotation of your actor into the rotation here but what I'm actually going to do is get the world rotation of the weapon to drop so going to get the world rotation and and I'm going to plug that into the spawn transform rotation so the pickup will be spawned with the same rotation of the weapon that you're dropping I'm also going to plug weapon to drop into weapon to drop like so nice uh if we compile we should not get an error anymore very good and that should be that all set up so now if if I pick up a weapon that I already had it's going to drop a pickup and it's spawning that for every player if I pick up a weapon that I didn't already have it's going to holster the other one like so dropping pickups just double check that this works on the client CU that is often the real test just double check that all of this is working correctly droping a pickup nice drop a pick up of the pistol and equip the pistol very nice very good that is all working very well okay what is next let's go ahead and add some animations so that uh the current weapon int is passed over to our animation blueprint and changing the animation that our character is playing so first we're going to need some animations let's head back to the unreal Marketplace and we're going to find something called the anom starter pack so search for anim starter and you can filter by free here and find this animation starter pack right here add that to your project and let's uh let's find what do I call this one rep weapon system so search for your project show all projects select your project and select the most recent compatible version which is probably going to be 5.2 and hit add to project now this will create this anom starter pack folder and if you are using the third person template it will come with a retargeter that you can use to retarget these onto your ue5 skeleton if you are not using the third person template I recommend you add an add feature content or pack and add the third person content pack to your project uh but you can then delete basically everything except for the characters folder because this characters folder does include um the retargeter for the animations I believe I can't remember exactly where it is uh rigs in rigs this ik retargeter um so what we're going to do is go into our atom starter pack here and I'm going to create a new folder going to call it ue5 and then here I am going to filter for animation sequence so if you do not have this just click this little filters menu here and search for animation sequence and filter by animation sequence and then control a to select everything right click retarget animation assets duplicate and retarget animation assets select the retargeter RTG ue4 Manu to ue5 menu I'm going to change the folder that these are going to be duplicated into to my anom starter pack and ue5 folder that I've just created and I'm just going to add a prefix U 5core and hit retarget this may take a few moments and once this is completed it should take you to the folder and show you your retargeted animation assets very nice um let's look for uh u5 idle rifle iron sights right here so this is the one that we're going to use when we equip the rifle and let's also look for idle pistol and we'll use this one when we equip the pistol very nice okay so we created the current weapon int earlier and changed it to replicated we now need to pass this over to our animation blueprint so we're going to make a few changes to our animation blueprint right now you can select the mesh and over here you'll see the animation blueprint and the anim class right here if I hit browse to that I'm currently using ABP Quinn but no matter which one you're using you want to make all of the changes to ABP Manny because ABP Quinn is actually a child of ABP Manny so we're going to go in into ABP Manu and make all of the changes in here the very first thing we're going to do is fix up this cast because it is not a direct cast to our third person character so we won't be able to get um the different variables from our character that we've set up so we can delete this cast and instead drag off of get owning actor and cast to our third person character like so plug these in and also select this character right here this variable and change the variable type to an object reference to our third person character so BP third person character object reference change variable type and now when we drag off of the character we can get stuff such as the current weapon int right here I'm going to tidy this up a little bit so I'm going to select this box and I'm going to contrl C the first few words here set veloc vity and ground speed going to contrl C that and then I'm going to select all of this and right click and collapse nodes and then I'm going to contrl + V in and name that set velocity and ground speed and I'm going to do this with all of these so contrl C set should move then select all of this right click collapse nodes paste in that name set should move and set is falling contrl C collapse nodes contrl V set is falling nice much neater let's add a pin here let's get down here in variables and references get our character reference and let's get current weapon int drag off a current weapon int and promotes a variable I'm going to leave it called current weapon int plug this in here select all of these and right click collapse nodes and and just call this Set current weapon like so so now we have access to our current weapon in in our animation blueprint what we can do is head on over to the anom graph and we're going to make quite a few changes here let's first disconnect this right here and going to move this stuff out a little bit and off of Main States let's find a layered blend per bone and I'm going to select the layered blend per bone node and under layer setup drop this down and add a new Branch filter drop that down change the bone name to spine 01 and I'm going to check mesh space rotation blend and mesh space scale blend and now we have a blend from our first spine bone up and we can use that to create some upper body animation so the first thing I am going to do is um I'm going to blend by our current weapon int so up here I'm going to find some empty space and uh blend poses by int I'm going to right click and add a blend pin so that I've got 0 1 and two you can add as many blend pins as you need I'm going to add current weapon int as the Active Child Index right here and on blend poses zero all I'm going to do is find the saved cached pose Locomotion use cached pose Locomotion and plug this into zero like so that's just this one here that comes you know as standard it has all of the blend spaces and everything in there so it's just the normal animations plug that into into there I am for the pistol and the assault rifle I'm going to cach these poses as well so just up here I'm going to create a new state machine I'm going to call it pistol enms I'm going to contrl C that name and I'm also going to create a new save cached pose and I'm going to rename name that one as well pistol enems and I'm going to do the same thing for my assault rifle enems new state machine call it assault rifle anoms contrl C that name new save cache to pose and rename that one assault rifle enems like so and then I'm going to call these plug them in here so use cache to pose pistol enems that's going to be blend pose one use cach pose assault rifle enems that's going to be blend pose to and then these we are going to Cache new save cached pose and we're going to call it upper body enims like so and then I'm going to call this down here in front of this default slot um use cach to pose upper body atoms plug that in there nice now this is all set up but these are empty so we need to go in here into pistol enims and off of Entry add a new state I'm just going to call it base I'm not going to set up any aiming States or firing montages or anything anything like that um well I might set up a firing Montage just to show you how to make that replicated at the end but uh let's just go into base here on the pistol poses and I'm going to find my idle pistol ue5 idle pistol plug that in here oops and I'm going to make that looped so I'm going to check Loop animation I'm going to back out to the anom graph and go into assault rifle and also create a state called base and in base I'm going to find my idle rifle iron sites like so if you'd like to know how to tidy these animations up oh I'm also going to check Loop animation on that one if you'd like to know how to tidy these animations up and uh make them look a bit better I highly recommend you check out my leftand I okay procedural recoil um and realistic third person poses tutorial it's quite a big tutorial but there's some really good stuff in there okay so if we go back to the enm graph and take a look that should be all set up I do believe that is everything in there so if I pick up an assault rifle it's going to do the assault rifle animation and that is replicated so the other player can see the same pose and if I pick up a pistol it's holstering my weapon and doing the pistol pose like so very nice let's add some vertical procedural aim offset and let's also set this up to uh look and play a bit more like a third person shooter so just to change the setup very quickly I'm going to go to my third person character and I'm going to search for Orient and uncheck Orient rotation to movement I am also going to search for y and check use controller rotation y I'm also going to select the camera boom here and here I'm going to change the target arm length to say 300 and the socket offset I'm going to make this 100 and 50 so that our camera is over to the right and up a little bit and now if we hit play yeah we've got a bit of over the shoulder action and our character is rotating with us so it's a bit more like a third person shooter very nice okay let's add this uh procedural vertical aim offset what we need to do is head into the animation blueprint and I'm going to add a pen to the sequence right here and grab my character reference and off of the character reference let's get base aim rotation can right click this and split the struct pin we're going to get the Y pitch value right here and we are going to subtract it off of 360 we are also going to negate it so we're going to multiply it by -1 and we're going to check if it's greater than 180° and use that to [Music] to select float plug these ones in here like so and then we need to divide it by the number of bones we're going to apply it to so I'm going to apply this to the five spine bones so I'm going to divide it by five and then I am going to make a rotator leave that plugged into X that's the one we need and then we can promote this to a variable uh call it pitch Rotator and plug it into the execution pin like so and I'm just going to highlight all of this and right click and collapse nodes and we'll just call this uh set pitch rotation head on over to the enim graph and I'm going to grab this stuff here and move it way out to the left I'm going to right click and search mod and find transform modify bone plug this one in here and over here I'm going to find translation pin here and uncheck expose as pin we don't need translation I'm going to leave rotation but I'm going to change the rotation mode to add to existing and then scale unexposed that as a pin Alpha unexposed that as a pin so we've just got this one pin rotation and the rotation mode is set to add to existing now that we've done that we can duplicate this four times so that we have five of them plug them all in and grab your pitch Rotator and plug that in into the rotation pins on all five of these nodes so this is why we divided it by five because we're applying it to five bones so we are now effectively multiplying it by five um and the last thing we need to do here is just change the bone to modify so select the first one change the bone to modify to spine one change the second one to spine two so on and so forth spine 3 spine 4 and spine five and the brilliant thing about this um base aim rotation is it is as far as I can tell it is uh automatically replicated as part of how Unreal Engine replicates movement so this should be totally replicated you see our server is looking up and down that's working nicely and our client that is also working nicely very good okay let's uh Implement some basic firing logic and spawn some particle effects so let's head over to the third person character and I'm just going to find some empty space down here and find the left Mouse button um I'm not going to set up an enhanced input on this one but for the record if you're making a project making an actual game here you would want to set up an enhanced input not just you know do this quick and hacky way where I put it on the left Mouse button this is just a basic you know basic thing we're going to do for fun as a little bit of a bonus um let's find the equipped weapon and pull that out and right click and get a validated get because if we don't have a weapon we don't want to be firing and then let's create a variable and just call it firing question mark type Boolean and we're going to set that twice off of pressed as being true and off of released as being false um we did set up the firing modes I believe let's just double check BP assault rifle has the firing mode full auto and BP pistol has the firing mode semi-auto very good so what we can do is drag off of the equipped weapon here and get fire mode and then we can do a switch on enum fire mode and um after this we're going to call a custom event so that we can uh run this on the server so uh let's just down here create a custom event call it run on server fire speaking of fire the fire alarm in my neighborhood is going off right now so apologies if you can hear that I'm going to change this to run on server and reliable and I'm going to create another custom event call it multicore fire and this one is going to be multicast and reliable and then off a semi-auto what we can do is just call the run on server fire and on fully auto what we're going to do is check this is firing uh Boolean so we can get that make a branch and we're going to make a very simple fully automatic firing Loop here um we can duplicate this one so we're going to fire and then we are going to delay by 02 of a second that's fine and loop back to this Branch so if we're still holding the left Mouse button it will just continue to Loop now this is very basic stuff this is not actually how you'd want to set it up in the long run uh if you do want want to check out some proper firing modes and firing logic I highly check out I highly recommend you check out my um Advanced firing modes uh tutorial it is a really good one um but we're not going to go into how to set that up for replication today we're just doing this very very basic setup so um if we one thing we would want to add on the server is an authority check um so so we can switch has Authority and this means that um if the player didn't have authority to fire they wouldn't be able to Fire and then if we do have authority we can uh call that multicast fire and on multicast fire there's another check we can do here because this is basically going to be all of our particle effects and um you know animation montages and stuff like that um so we wouldn't want these to play if it were a dedicated server like on the actual server so we can check is dedicated server and put a branch on that and then we would play this on false right here so we can get the equipped weapon and get firing animation which we set up earlier and we can play animation on false right here and the target will be the equipped weapon playing an animation on the equipped weapon and the animation we're playing is the firing animation that's contained within that equipped weapon so you can see why these variables inside of the weapons are a great idea um all righty well that should work those animations should be replicated across all clients grab a weapon that's playing for both and this one is semi-auto that's also playing for both and the real check is that it's working for my client and it is very nice all righty um let's look at switching weapons actually first let's just add a quick animation Montage um so that they're not so static when they're firing so let's find our atom starter pack folder and ui5 and Let's find let's find these firing animation fire rifle iron sights and is there fire pistol huh there must be a pistol still firing one um let's have a look at this one yeah very good okay let's right click on that and create an anim montage and I'm just going to leave it named as default open this up and go to the asset details here let's change the blend out Trigger Time to 0.05 and let's also drop down the blend in and change the blend in to0 .05 it's a very fast animation um that should be all we really need to do in here we can close that and then go to our third person character and down here um might have to Let's uh let's store that within our weapon so we can go to our weapon component master and we can add a new variable um call it firing montage that will be an animation Montage might be anim Montage anim Montage object reference compile and save that and then head to your assault rifle and the firing Montage right here will be the one that we've just created like so so now we can just drag out of the equipped weapon get the firing montage and we can also grab the mesh PL montage and the Montage to play will be the firing Montage like so and I'm not going to set this up for the pistol as well um but you would just need to go into your pistol and select you need to create them on montage and then go into the pistol and um select the Montage let's just test that out for the assault rifle and make sure that it's replicated you can see the shoulders moving a little bit there that's replicated for that client and the client to the server yep that's working very nice okay let's have a look at some weapon switching to switch weapons let's head into our third person characters blueprint and find some more empty space and again I'm not going to set up enhanced uh inputs for this I'm just going to find the zero key the one key and the two key and I'm actually going to um I'm actually going to call functions from these in case you want to set it up uh on the mouse on the mouse wheel as well so you'll also you know add a mouse wheel input and then a multigate um which we can we can look at doing in a moment so I will just uh create a custom event here and call it unequip weapon basically holster weapon but we'll call it equip weapon um this one will be equip um equip primary and this one will be equip secondary and I'm just going to call these here so unequipped weapon equip primary and equip secondary so we'll set up all the logic off of these ones here let's start with the primary um so the first thing we want to do is check if we do have a primary um so a validated get is a very good way to start off logic sometimes we get the validated get then we check if there even is one if there isn't then we won't do anything um and the next thing we want to do is check if we already have it equipped cuz we also don't want to execute this code if we've already got our primary equipped so we can get our equipped weapon and what I'm going to do is check if these are not equal because we want the bulk of our code to be running off the true execution pin not the false so we check if they are not equal and if they are not equal um we we also want to check if we have a weapon equipped and this is just to stop some potential errors so we can also get our equipped weapon and convert that to a validated get and if we do have a weapon we want to holster it so we can call our runon server holster weapon like so and if we don't uh we can go ahead and set it as our our primary as our current weapon and attach it so we can get that logic from right here um where we spawn our primary weapon and basically what we want to get is everything from the set equipped weapon to the attached component to component we can grab all that and duplicate it down here and what we want to set the equipped weapon as is the primary weapon we set it as the primary weapon and then we attach it like so and and that's that's that pretty much um oh we just need to skip the holster weapon if we don't have an equipped weapon this is basically in case you are unarmed and you're switching to your primary you don't have an equipped weapon um so we don't have anything to holster and the holster uh function will return an error um you know there is no equipped weapon to holster so yeah bypass that one there let's do this again for the secondary so we can get a secondary weapon right click convert to validated get check if this is not equal to our equipped weapon and if it is not equal to the equipped weapon we then just want to check do we have do we have an equipped weapon if we do call our holster weapon function on the server and if we don't we can go ahead and grab this stuff from the spawn secondary weapon again everything from set equipped weapon to attach component to component duplic at that down here plug in our secondary weapon into equipped weapon and remember to bypass that holster weapon if we do not have an equipped weapon whoops and now we just need to do the unequip um the unequip is a little bit more simple uh we first want to see if we have an equip equ weapon so we can get our equipped weapon convert to validated get and if we do we want to holster it holster weapon run on server holster weapon and then we want to set our equipped weapon and our current weapon int as nothing and zero and obviously if we didn't have a weapon um this will not even fire off so this effectively works as our Branch we don't even need to skip this step right here so that should all be working let's have a look pick up this weapon pick up that weapon now if I press one switch to my primary two switch to my secondary and zero unequip very nice and there should be absolutely no reason why that doesn't work on the client perfectly well oh it is not ah okay so this needs to be run on the server this needs to be run on the server and multicast so let's do that now um what we can do is we can um I'm just going to rename these to run on server so this will be run on server unequipped weapon I'll contrl C that and I'll change all of these to run on server and if there's ever any doubt whether you need to multicast anything uh you can just try to run on the server see if it works um and then go from there so we can just try run on server on all of these run on server I suspect this will need to be multicasted because of this you know this attach like we there might be a problem here because this runs on the server as well um so we might might want to make this check before we call the run on server I haven't actually gone ahead and worked this out ahead of time so I apologize here we're doing some some live debugging um that works fine because he's the server and that seems to be working fine as well yeah so we don't need to multicast this we are multicasting the holster so I suppose that is that is why that works because we're uh attaching the um the weapons on the server and the The holster is working on the server as well we're not getting any errors there so that's all good that is working nicely let's see if we can um get this working on the mouse wheel as well so we've got these these calls already set up so we can just find mouse wheel up and mouse wheel down and this is going to go to a multigate can add a pin and duplicate this and this is actually a bit tricky you know what I'm not going to go through this on on this tutorial because I've already done this on another one if you want to go find my weapon switching tutorial it does contain a bit more content on on how this works but it is it is a little bit tricky to wrap your head around because of where the index is and and you know you've got a different input here and a different multi-gate so um you know you need to like store the index so that you you know what your current weapon is so what the next uh what the next output should be um it's a little bit counterintuitive um so I'm not going to run through it today this this is already the bulk of the code done it's basically just wrapping your head around um you know that stuff so if you if you want to set this up on a mouse wheel check out my weapon switching tutorial okay let's um go ahead and change this skin on our weapon I'm just going to put this on the P key so I'm just going to find some empty space and find the P key and the first thing we going to do is check if we have our primary weapon equipped I'm not going to do this for both weapon classes I'm just going to do it for one so that you can kind of get an idea of how it works so I'm first just going to check if uh my primary weapon is equal to my equipped weapon so if I have my my primary weapon equipped put that on a branch and then we're going to call a custom event within our primary weapon so let's set that up but first let's just create a couple of different materials for our assault rifle so I'm going to go to my BP assault rifle and browse to the material right here and I'm going to duplicate it which I've already done so I've just duplicated it I I've opened up the duplicate and I will browse to the texture and it will browse to this um the original texture I've then duplicated that and then open up the duplicate and I've just cranked the brightness up like so so that you can distinguish it from the other one I'll save that and close that and then I will assign that new uh texture the brighten texture to this base color of the um duplicator material so if I go back to my materials I should have two materials basically identical but this one's brighter than the other one so that you can differentiate it okay um so as I said we're going to call a custom event in our primary weapon so we're going to go into our weapon component here and I will start by creating a custom event let's call it run on server uncore switch skin and I will also create one called multicast uncore switch skin this one will be multicast and reliable this one will be run on server and reliable and if you haven't already um you should have this skin variable that we created earlier in the BP weapon component component of type material instance object reference and we are going to make that replicated make sure that's replicated and instance editable and expose on spawn like so um and what we're going to do here is I'm just going to put this on a flip-flop so this is obviously not how you'd want to set this up you wouldn't want to be able to um you know change your skin just by pressing a button or may maybe you do want that but um I'm just basically doing this to to give you an idea of how this setup is going to work and what's required to make everything replicated for all clients on the pickups and when you pick up a weapon um so what we're going to do here is just on a we're going to set this as our new one here so select this new one and use asset browser selection so add it to this one and then the other one right here select that and put that on this one here and then we're going to set material and the material is going to be as that skin variable duplicate this one do the same down here so the target here is the self this is a skeletal mesh uh component so it's setting the material of the skeletal mesh at element index zero so if you go to for example your BP assault rifle and look at the material here you'll see it's at element zero so we're changing this one right here very good and then on the run on server switch skin we can call the uh multicast switch skin like so and then we just need to in our character call the run on server switch skin like so got an error add component by class ah this is something from earlier because I was testing uh just ignore that we're going to go into that in a moment setting the uh color of the weapon when we pick it up so now if I hit play this should work if I pick this up I've crashed the editor very good sorry about that guys I think that was just um you know failing to compile and save everything and I still had some some warnings there um but that is working so if I just bring this up I can pick up a weapon and if I press P it's going to change that skin and you can see it's changing for both instances here and just to double check that it's working on the client as well that's changing on both instances very good so that is working but what happens if I change the skin to to this white one and then I go and pick up an item here it's obviously it's dropping a pickup that doesn't have that skin on it so we're going to Now set it up so that it spawns that pickup with the same skin and when you pick up that pickup it will um you will pick up a weapon with that same skin you will notice here that the boxes are out of sync so um they're not the same on both instances here I'm bumping into a box on my left but there's no box there um and that is because these static meshes have to have replicated movement so uh what you can do is select your little boxes here and then search for rep and just check static mesh replicates movement and that has to be for each each single one because these are just instances of static measures they're not a blueprint or anything like that um so now these boxes will be replicated nice okay so let's set this up so that it will spawn a pickup with the same skin on it um we are going to need so we're just going to do this for primary for now so in our primary weapon pickup we can create a variable and call it skin object reference to a material instance and this will be instance editable and expose on spawn like so and then in our primary weapon pickup what we want to do is set the material as the skin uh when when this uh pickup is created so let's create a couple of custom events we'll call this one runon server set skin and another one oops another one called multicast set skin Sor forgotten how to type this morning um and change this one to multicast and reliable this one to run on server and reliable I got to be honest guys this tutorial has gassed me a little bit because it is a lot to wrap your head around uh replication is no joke so we're going to call runon server set skin on begin play and then on run on server set skin we'll call the multicast set skin and on multicast set skin we will set the material of the pickup object as Skin like so and make sure skin is replicated replicated instance editable and exposed on spawn nice so now when this is created um it will automatically set its material as whatever the skin variable is set to and because we made this instance editable and exposed on spawn if you go to your third person character now and your drop weapon logic and you right click on this spawn actor node and just hit refresh node skin should pop up down down here and it doesn't why doesn't it we might need to just add it to um we might need to add it to the pickup Master for this to work um yeah it's not it's not coming up here which is odd CU this uh uh this is just any pickup I guess weapon to drop primary weapon secondary weapon okay so um we might just need to add this instead of adding this skin here to the primary weapon pickup we might just have to add it to the pickup master and this is probably a good moment to point out that I have made a slight mistake here it's probably not a good idea to have primary weapon pickup and SE secondary weapon pickup be Children of the pickup Master because if you were going to have just normal pickups in your game they would be children of this one um so I probably should have made a I probably should have just duplicated this one and made a weapon pickup master and then made these two children of that um but that's okay uh this is a learning process so I'm going to add in the pickup master I'm going to add the skin variable material instance object reference instance editable Exposed on spawn and replicated and now if I go back here to this spawn actor node and I refresh it it will say skin right here so uh instance editable and expose on spawn exposes this as a pin on spawn so that when we spawn this pickup we can set the skin um and we just have to check that yes we've got skin in our weapon component so this is the skin of our weapon component right here and then here all we have to do is drag off weapon to drop and get skin plug that in here and now when we drw drop that uh weapon because on event begin play it's going to set the um ah we just need to get skin here and this is the skin that we just created in the pickup master so that's passed on down even though you can't see it here it's inherited it'll be in the class defaults here skin which is set when uh on when we create it here here the skin is set right here and then um it's been created it will run this code and it will set the material as the skin like so so now if I pick up a weapon press P pick up another weapon it'll drop a weapon with that skin and now the next thing we have to set up is if you go over to this weapon and pick it up it will just pick up a black one it's not going to be white uh so we need to set that up so that and just double check that uh we're spawning the correct skin on the client as well for the pickup okay so to uh set the weapon skin when we pick it up we are going to um basically call another function and now this is getting a little bit messy you know you probably figure out a way of tidying this up but in our weapon component I'm basically just going to create one more um set of rpcs to set the material so I'm going to create a custom event multicast set skin just double check that this one isn't called set skin yep it's called switch skin I am going to contrl C that name make our run on server set skin this one will be run on server and reliable this one will be multicast and reliable and we are also going to add an input to these call it skin object reference to a material instance same on this one skin set the material as the skin call multicast set skin set the skin as this skin variable that we will pass through and we are going to call this on our spawn logic right here when we pick up the weapon and spawn it um but we will also have to pass it through when we um when we pick up the weapon so we need to add another input to our spawn primary weapon here and it's going to be skin object reference to a material instance and then obviously in our primary weapon pickup where we call this spawn primary weapon that's where this skin will be passed through um and we should I like to pass these back through so that they're passed through from the um from the initial call and then we can just get skin oops get skin pass that through so this is the skin of the uh pickup which we set obviously when we drop we just tested that so we're passing that through to our spawn primary pickup and on our primary pickup right here we have made that uh skin on the weapon component instance edable editable and exposed on spawn and obviously replicated and that has exposed it as a pin right here so we can just plug that in there but I have noticed that this isn't really spawning an actor so this doesn't um you know if you if you go into the weapon component and you do this off of begin play like I call run on server set skin on begin play here it doesn't really work um I guess it's because it's not an actor that's being spawned um so we need to call this after we create it so we've set the skin of the component here the skin variable but we haven't yet applied it we haven't set the material so what I'm going to do is just grab this stuff here and move it out a little bit and then off of primary weapon here I am going to set the skin the run on server set skin like so and what I'm going to set it as I just pass this underneath here what I'm going to set it as is the skin of that component so we did set it in here we just haven't applied it and so so we can go off of here and get skin plug that in here and then that will be passed through to this which will pass through to the multicast event and set the skin like so and that should be everything oh we got an error ah sometimes it does this when you when you call an event it doesn't what is that new parameter that didn't even exist that should be working um I'm just going to double check that every relevant blueprint has been compiled because I crashed my editor earlier um let's test this out so I picked this weapon up Chang the skin it's changed on both instances I drop a pickup the pickup has that skin on both instances and if the client goes and picks it up oh I've picked up the other one there we go he's picked up the weapon with that skin so you can pick up a black one he's got a black one on both instances and the pickup is spawned with the white skin on both instances and you can switch them like so very nice that is working very well one thing I did notice if I press zero here and switch to to my unarmed that when I jump uh he just has the basic Locomotion on the upper body um I think what we can do here is just plug in main states in our animation blueprint so if I go to the ANM graph and my unarmed uh anims right here I actually want to use main states um that's a state machine so let's just cas main states whoops and I'll just um I'll just double check that main states isn't already cached no it isn't so cach that pose whoops new save cached pose call it main states oops use cach to PO main state States plug that into your base pose there on the layer blend per bone and instead of using Locomotion here we will use cached pose main states and now when we're unarmed yep we should have the normal jumping animations like so guys I do believe that is everything we set out to do and just a little bit more uh so once again sorry that it got a little bit messy there at the end if this tutorial has been of any use or value to you whatsoever please hit like And subscribe and if you are so inclined please do consider making a small donation to the channel via the YouTube super thanks function uh I have added a link to my subreddit and the Facebook group which are just so that you guys can discuss any bugs that you might be encountering or you know share some exciting stuff that you made so uh yeah there's not really many people in the subreddit or the Facebook group at the moment I do not have a Discord I do not have the time to manage a Discord um but those are some forums there where you guys can discuss things and share you know what you've made uh so yeah thanks for watching and I will see you on the next one
Info
Channel: MizzoFrizzo
Views: 6,427
Rating: undefined out of 5
Keywords: ue5, video game development, blueprint, unreal engine 5, tute, true first person, shooter, shooting game, realistic, unreal engine 5.2 first person shooter tutorial, how to make a fps game, fps tutorial, TFPS, ue5 weapon system, unreal engine 5 weapon system, unreal engine gun system, unreal engine 5 fps tutorial, unreal engine fps tutorial, ue5 first person shooter, how to add a gun, multiplayer, two weapons, network, RPC, remote procedure call, pick up weapon
Id: s6cItbr8zL0
Channel Id: undefined
Length: 126min 14sec (7574 seconds)
Published: Tue Mar 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.