How To Make An Interactable Ceiling Fan in Unreal Engine 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna be learning how to set up a ceiling fan that will work both in multiplayer and single player scenarios you'll be able to have a light switch that both players can interact with and it will turn and turn off the fan let's get started first off you're gonna be needing these two models they're they're free right now so you don't have to pay for them or anything we have both the ceiling fan we'll be using the fbx and we have this light switch model we'll be using the model when you download them you'll get the ceiling fan as such and you'll also get a zip file of the light switch which we can just drag out here which it's hiding it went to my other window there it is okay before we use the ceiling fan in unreal we will need to modify it however i'll put the modified version of it on youtube down below but i'm going to go ahead and show you how i'm editing it i want to be able to make it to where we can change the height of the cable that particular fan first going to delete everything here i'm going to import the fbx file that we downloaded go to my desktop i believe and it's going to import fairly small i'm going to click this scale size over here click in the middle left drag totally mess it up try it again there we go just scale it up a bit it's fine and it doesn't need to be perfect or anything but i do want the origin of the model to be the top of this base right here and that should be just about fine now this fan is almost perfect as far as how it's separated because if you look here it's separated in three separate models the wings which are actually separated into three separate wings one two and three and the cable which is actually the cable and the base smushed together we're going to split that and the body which is just fine for the wings however we are going to select each one shift click each one and then ctrl j that will merge them into one model file this way when we rotate it and unreal doesn't have to rotate each one it just rotates the one bottle so leave that there and the base will leave alone however we need to split the cable off from the body the or the base and so to do that we need to go to object edit mode and we are actually going to select up here in the top left the face select and we're just going to select the faces of the the wire so i'm holding shift to select all of them making sure this is selected i'm also going down here i'm shift middle clicking to drag the screen around i'm going to select the vertices or oops shift select the edges that's a bit too much just select all the edges shift select all the edges and vertices just making sure i get everything okay so now that we have all that selected we can right click it and click separate by selection so now if we go back to object mode we'll see that the wire is a separate selection from the base we're going to rename that we'll name that base and name that wire cable doesn't matter it's fine uh so we have those two those four different objects which is great and now that we've finished here i'm going to go ahead and export as an fbx i'm going to not make any animations we don't have any animations here to bake it's just a plain model and then name it scene fan fixed because we fixed it and export okay we're finished here don't need to save that and here's the new one i'm going to delete this old one so i don't import the wrong one so we have this light switch and the ceiling fan so let's go ahead and open up a new unreal project so let's go ahead and open up a new unreal project i'm going to go ahead and use unreal 4.27 anything that we do today will work in 4.26 and pretty much anything earlier um as it's pretty basic we'll be setting up today we're also going to be using the first person template however this will work pretty much in any template that you choose i'm mostly just using first person for how we're going to be interacting with it we're going to use it as a ceiling fan tutorial project so the base project we're not going to change anything really is we're going to be creating different things um but we do need to make a ceiling for this so i'm just going to zoom in here i'm going to hold alt i'm going to drag up to create a duplicate of that particular thing here going to press r squish it down for a roof panel press w drag it over and i'm gonna press r here raise it up too why not now we have a roof to use for the ceiling fan i'm going to drag this up higher too there we go should be just fine now we have this set up and ctrl shift s to save everything and we need to go ahead and import our files here so first off i'm going to go back to content and we're going to create a new folder let's call this the tutorial folder and it's going to open it here we're working in here this is kind of the directory of a first person project doesn't really matter but first off we're going to create meshes folder because we're going to import these meshes that we have now we have these meshes right here these two we're going to go ahead and import these meshes one by one we'll drag the ceiling fan first it's gonna have all these default settings doesn't really matter um this might matter because of the size of the fan we didn't model this fan we kind of just pulled it from the internet and so the uniform scale of one might be okay but we can change that after we import it it doesn't really matter you'll notice that it has each of the four sections that we had before there's no textures applied to it because we just import it as an fpx and i don't think it came with any textures from the website anyway that's fine i'm going to control shift s to save all of those and now it's in the project and i'm actually going to move these to ceiling fan folder just for organization's sake i'm also going to right click create new folder and name this light switch open that folder up drag the switch object in here or leave everything the same it's fine we'll do a little scale later it did pull in some materials i can maximize this now and we're probably not going to use any of those materials which is fine oh also if you don't have your content browser here and that's kind of i guess my default setup you can go to window and open up one of your content browsers or you can straight up hit this content button here it should bring it up usually it's a different tab or a whole different window altogether i just like embedding it up here for myself and from doing little things here which is fine so at this point we need to go ahead and create the actor of the ceiling fan as a blueprint because not only is it going to combine all these meshes together so that they're one coherent object but it's also going to add some logic to it as far as turning the fan on having it rotate everything so we're gonna go back to the tutorial tutorial folder create new folder called blueprints this is just for organization's sake and we're going to create a new blueprint class the fan will just be an actor because it will be an object that is going to be placed or spawned in the world that we're going to be using we'll just name it bp for blueprint underscore ceiling fan control shift f to save it just a good habit to have i double clicked it to open it up it opened up in a separate window which is fine but i'm going to drag that in to your into here so i have these two windows now your layout may look a little different it's okay for now though i'm going to drag this up here so that our viewport is the main thing squish this over to the side i also have a content browser over here you can add that by going to content browser clicking on one of the extra ones and just drag it down to here if you like that oops so i need to add all those meshes that we just imported so we have them in the tutorial meshes folder we'll do the ceiling fan first i'm going to shift collect select all these click first one shift click the other one drag this into the scene and we are actually going to make the scene root we're going to make a new scene would be the base so that this is like the core the the the base of the object so we have the fan here it's a little big i bet you if we compile this save this and if we dragged this fan into here that's you know that's actually pretty good size if your fan is way too big and you're noticing that this is just a really weird size you can adjust that by coming into the ceiling fan and you can modify each one i would modify them uniformly uniformly so if you do you double click on one it'll open up the fan or whatever object whatever model you selected and if you go to the build settings and then build scale you can change the scale here if it's too small and make it bigger like maybe two on each one oops two two hit enter click apply changes you notice it'll make it bigger it's not actually cut off it's just hidden beneath this plane that they have there but mine was pretty pretty good imported so i'm going to go ahead and leave that as the same and not change it or touch it this will be important because later on we're going to do some logic to change this wire to change the length of it and kind of have you be able to customize the fan but we have the fan imported which is good we also want to let's go ahead and add the light switch as well and we're going to add this to the actor itself because we want it to kind of be associated with the fan too so i'm going to on this blueprint i'm going to go back a folder back to meshes over the light switch shift click all of the models that were imported along with the light switch these big circles are materials we don't need those we'll override that later let's go ahead and drag it in now we do want one of the light switches to be kind of the the main one and we want it to be the base which is kind of the panel can't really see it but it's really teeny tiny and that's okay we can adjust the scale oh goodness later if you're thinking the camera is moving too quickly you can click up here in the top right corner and adjust that camera speed i'm gonna make it two it's pretty good it's not too fast but i do want this bass to be the the parent object of the light switch so i'm going to control click these other three drag them and attach it to that particular object so now if i move i have this object selected it's compiled save that and i move that and moves all of them together now it's really tiny so i'm going to increase the size we can do it uniformly by dragging this if i clicked r when i have the selected and drag this white square it will increase the size of all of them [Music] and you'll notice that both the light switch and the two screws scaled appropriately textures are kind of weird but it's fine it's a free model we didn't have to work to to make it it works just dandy i might actually make it a little bigger it's fine okay so now that we have the light switch and the fan both in the same blueprint project we can go ahead and do some logic to this first we're going to do the logic of having the actual fan blade spin um on button press so i'm going to get into that part for now so if you when you're in the blueprint you might see the construction script event graph and viewport if you go ahead and click on event graph you'll see these basic ones here beginplay these three you can select all those press the delete key and get rid of those we don't we won't be needing those we're not going to be doing the rotation of the fan on tick because it's actually better to not have tons of things running on tick and have it run on like a timeline instead um so we have that here let's go ahead and start working on this so our first function is just going to be the the base function of turning the fan on or off so i'm going to right click and type in custom event hit enter and we're going to call this turn fan on slash off zoom in so you can see this a bit better and we're going to compile save that and we're going to have this be kind of a a branch so i'm going to drag off type in branch and this is going to be based off of a boolean value of whether it's on or whether it's off so i want to promote this to a local variable so i just dragged off i'm going to click promote a variable and oops this condition is going to be called i'm going to hit f2 oh i can't hit 2 f2 over here and just call this on off that's fine now from here if it is true or if it is on i wanted to turn it off right and if it is off i want to turn it on so technically i need to do a knot of this a not pullian so if it's on then do the opposite to the false tree the branch if it's off to the true branch so we want to turn it on so here's where i will then say okay well we're turning it on i need to set it to on and if we're turning off i need to set it to off uncheck that i just ctrl c and ctrl v to copy that right there now we needed to also call the function to say okay we'll activate the fan and then flip the switch you know at the same time so what we need to do is to create another custom event so we're going to call or so right click to type in custom event hit enter and we're going to call this one just activate fan it's good enough and we're also going to create another one touch them event and call this one stopping so now that we've created those two custom events if we compile and save we can then call them here activate fan i just typed in active and it was able to find it and stop right it's kind of bothering me we're going to call this one deactivate bam there we go compile save now we have it doing those two now when it activates the fan that's going to make the the fan blades spin but we need that light switch to also flip so we need to create in the event graph other functions too to flip the light switch so custom event and we're going to label this one flip switch i hit f2 and that allowed me to re-edit that one there okay so we need to call that function to load switch now this one needs to be called both times because we're going to flip the switch to activate it and to deactivate it right so we're going to pass this value to this one so flip switch needs to be able to know whether it needs to turn on turn off so we're going to add an input to this particular function because this function needs to have an input of what it's going what it needs to know before performing at the action so we're gonna add add a new parameter this parameter will be a boolean luckily it was already selected but it's like boolean if it wasn't and type in on slash so that we know which one it is you'll notice that as we did that it put it up here and we need to pass this particular boolean to there so that the function knows what to perform this will be when it's off this will be what's on policy so that's kind of the core of how the fan will be turning on and turning off and now we need to actually start defining these guys so when the fan activates logically you want it to start spinning we want the blades to to rotate right and they need to rotate as a fan would do and that would be in the blue or the z axis you'll notice how z is blue and it's going to be rotating along the axis as if the fan's active let me undo that rotation and so so we wanted to add rotation to the z-axis that is the uh desired effect so what we need to do is we need to select the wings so this is the wing mind you the wing model and we want this model to drag it down here to be honest rename that because this is gonna bother me we'll call this fan blades cable i'm pressing f2 to rename the thing uh body and bass much better and we're just going to call this switch oh handle and i think the switch button yeah that's the actual light switch so we'll call that light switch that's fun the screws i don't really care about we're gonna leave those that way whenever we're messing with these things it'll actually make sense so our desired effect is to add a rotation add a rotation to the fan blades because we want it to rotate right and we're going to add relative rotation because we don't want to have it it if you did let's see these other ones that rotation if you added local rotation that could work as well but the relative location is better in this scenario because we're wrote it's not the actor that's rotating it's just the the component of the actor so relative rotation relative to the actor makes more sense than local rotation to it all right so we're gonna be using that one now we need to have something that can slowly add rotation to it not just rotate it immediately because if i was trying to rotate this i mean a fan generally rotates slowly right it doesn't just instantly go from that to to that that doesn't make any sense so the proper term for that in unreal is lurping so we're going to add another we're going to search for a blueprint or an action rather and look for lerp and we're going to learn a rotator and this should look like this for you it's actually really useful and we're going to split this particular structure click right click on b we're going to split the structure pin and we want to rotate the yaw the yaw is the z-axis and that is the the axis the blue that we're wanting to rotate so we want to have this value slowly over time kind of spin and when the fan turns on it it starts spinning um slowly and then it gets to its max speed right it kind of gradually grows up to it and so we need some kind of timeline to determine that since we're not running this on tick unreal needs to know over what amount of time do you want it to spin up and start rotating and so unreal has a cool action here called timeline add timeline that we can actually create one so we're going to start this fan start and we want whenever we activate the fan to start from the beginning of this animation and as we do this let's double click this we need to define a timeline here this is kind of cool you'll be using these a lot for when a door opens or when a window's opening or you want to just slowly alert an animation this is really useful anyway so you start off by clicking this add float track and we're going to start this fan start it really doesn't matter this one and we need to add a keyframe this is kind of like an animation and we're going to have this be at 0 and 0. so it's perfectly at the zero part i'm right clicking the drag and i'm using the middle mouse wheel to zoom in and zoom out we're gonna make the length of it the fan will start up we'll say it takes three seconds to start the fan going to max speed right and we need to add another keyframe and this one's gonna be at value of one because 100 of the speed that we're going to give it and it's going to take three seconds to get to there so i'm right clicking to zoom out or right clicking the drag middle mouse wheel to zoom out and we want it to be smooth this is just a very linear very boring kind of gradual ascension to speed so we're going to right click and click auto on the first key point and we're going to right click that's probably fine you could click auto here as well but it seemed like that kind of messed it up let's undo that ctrl z let's leave it like that and so now we kind of have this gradual a bit speeding up over time the the amount of rotation it's going to be adding will gradually grow larger and so if we look at this we now have a a timeline that will provide a growing percent value of what to do and we want that since this is like a percentage value of how much speed it needs to give this will go into the alpha of the lerp which is how much it needs to provide of the value that we give it and the resulting rotation we want it to be added to the fan and so we will return that to this add relative rotation and we want this to update for every second every millisecond at this end that this timeline is going on so we're going to have it on the update function now the amount of speed that we want to give this that we want this fan to reach we're going to have that be kind of customizable so we're going to take this yaw the z value remember we we have this because we split remember normally this looks like uh well i can't combine it but normally it was it was like this blue but we split it by right clicking clicks oh there we go we right clicked it and split the struck spin pin and we're going to promote the z value to a variable and we're going to call this um where is it over here we're going to call it fan speed now we want this compile and save that it luckily it's a float already because we dragged it off of there and promoted it to a variable so it's already a float so it could be like 1.2 or 20 or 400 or whatever and we want this to be editable whenever we drag it into the map and so let me close that and so what we need to do for that is we need to add click this little eyeball and if you do that it will be added the variable is public and is editable on each instance of this blueprint super useful and to kind of categorize this we're going to add it into a different category we're going to come over here on the right side set the category instead of default we're going to put this under the setup category and so whenever we drag this into the world you'll notice that you have this setup category over here and the fan speed so we can like set it to 30 or whatever and that can vary maybe you put four different fans you want one to be faster or slower than the other one kind of gives you power um over customizing the blueprint that you created so we now have this thing this timeline that will go for three seconds and then stop now if we started this now you'll see the fan will rotate for three seconds spinning up and then just immediately stop and that's not how fans work right they keep going so when that animation is finished we needed to do a different animation we need to do a different rotation addition if you will so what we're going to do is we're going to kind of create another timeline because this timeline we want the speed to be constant and consistent over time so we're going to right click type in timeline add timeline and we're going to call this fan running whatever because it's just continually going and we're going to whenever this animation is finished we're going to play from start on this guy and we're going to double click this do the exact same thing we're going to create a float track at a curve now we don't we only need one keyframe because we want this to be consistent so even at zero we want the value to be one and this have it always be one the length can be one second that's fine and it will always be a hundred percent of the speed that we give it now the unique thing here is we want it to loop that's the key point here save that compile it and we come back we'll notice that it has this little um repeating symbol that's what we want now we need to repeatedly do this the same exact thing but since it's already at 100 whenever we give it this alpha value it's gonna just always give it the max speed that we provided so we're gonna actually select all this control c to copy control v to paste i'm right clicking to drag this window around right and then on every frame or every second millisecond would ever update that and it will keep updating it with the fan speed that we've already given it at full speed now i could say just one here or whatever it's the same thing but just for consistency's sake we'll have it like this so compile and save so now we have the activate fan function set so we need to have an option for it to deactivate the fan this once again whenever you turn off a fan it doesn't just stop immediately it's it slows down over time and so what we're going to do is we're going to create another timeline so right click timeline and this is fan stop and we want this one to start play from the start i'm going to double-click fan stop and add a float track we'll make it three seconds again as well oops that was an accident make it three seconds again as well and we're going to add a key now this one will be at time zero but value one because it's starting at its current speed and add another key and this one will be at time three three seconds long at a value of zero i right click to drag middle mouse wheel to zoom out now i'm going to right-click this particular keyframe and click auto and i'm going to drag it up because it's going to kind of be the opposite where it'll slow down gradually and then reach that speed file save we don't want it to loop just a one-time animation go back to the event graph and send this is ready to receive the stuff again you'll notice that this is kind of the same thing over and over that's fine so we're adding relative rotation every frame and playing from start got the fan speed for it to start at and stop oh we gotta check the alpha track because that's the amount of speed that it needs to give to the rotator so starting at whatever fancy we give it and it's slowly going down to zero and the [Music] this should be good now one thing here remember how we have this animation continuously running even though we apply this to it it's going to continuously be running this we need to stop this animation at some point so the way we can do that is we're going to put one step right before here and this step is going to be to stop the animation so we're going to drag off we're going to type in sequence and we're going to have it first we're going to alt we're going to control left click to drag that and we're going to have step zero the first step be to stop now to make this cleaner we're going to kind of double click here on the line to kind of clean this up again double click again there just looks a little cleaner so we know kind of what logically what's happening here we'll drag this up there we go so first it will stop that infinitely running animation of the fan running and then it will play the animation of it slowly slowing down from the from the speed that we've given it and that should be good for the logic behind activating and deactivating the fan now one more thing when we flip the switch we need it to kind of turn the the switch upside down right so there's a few ways you could do it we're actually just going to use rotation and we're going to rotate it on this green or this y-axis and we will rotate it i guess 180 degrees yeah just flip it right upside down whenever we turn it on so let's just save that that means we're going to take this light switch drag it down from the top left and we're going to add relative rotation just like we did with the fan and depending on whether we're switching it on or off we wanted to change the rotation right so with that we need a branch and then we're going to drag this into here and so if we're wanting to turn it on we wanted to do one thing which we'll have up here we're going to copy and paste this if you want to turn it off we're going to do this now here's the tricky part we're not actually adding rotation we want to set the rotation with a fan we're constantly adding rotation because the fan only ever spins one way right whenever the fan blades spin they don't spin the other way unless you like invert it by clicking the little button on the top of the fan whatever but the fan blades only spin one way however the light switch will flip two different ways so instead of adding rotation we're actually going to set relative low row rotation set relative rotation so i'm going to delete these two and this will be the one we're going to do we're going to set the relative rotation both the target is once again the light switch in both scenarios and the rotation whenever it's on we want it to let's see the current state it's rotation [Music] zero zero okay it's current state if there's rotation of zero zero zero it's set to off because the light switch is pointing down right so we want the off to stay all right so that'll that's good however for the rotation if it's turned on i believe we said the y axis should be just 180 degrees and we can test that by going to the viewport clicking on that little light switch and changing the rotation of y to 180 degrees and yeah it turns on or it flips up rather so that was correct so we're gonna go ahead and leave that there we have that set here that'll that will now based upon whether on what direction we flip the switch on or off it will uh adjust the rotation of this particular component which is great so we have activate the activate flip switch all set up and now we need to actually set up how the player will perform this action because it's not going to do it by itself so the way we're going to do it is when the player walks up to the switch he can perform an action now let me put this on the wall we whenever we added the switch to the blueprint some people might have those beings two different actors but really we want it to be in the same actor and we can just adjust its position in the editor scene so if we so we click on the actor you'll see both things are highlighted but if you double click on the light switch you'll have the switch it shows it's selected the parent object and you'll see this under your under your details panel how there we go the details of this actor you have what we had in the blueprint editor you'll notice how the same with the same um component structure and so we're gonna the parent you can also select it so right click it there we want this to be against the wall so we can just drag it reposition it so i'm going to click e to rotate rotate it 90 degrees and kind of drag it so that it's over here against the wall now it's not perfectly against the wall it's fine but if we turn off the grid we can drag it and have it just be on the wall now for visual sake i think our character is actually really small right yeah he's pretty small here and save that so now if we click play or over here we see the fan i see the switch but nothing happens we haven't programmed anything so click escape and let's go ahead put that logic kit so we have all that ready and set up but we need to have something to give us some kind of event that gives us the ability to click on the button so what we need to do is we need to put it's very common to use a collision box so we're going to click add component type in box collision it should show up and box is fine we don't have to rename that it's probably fine and we need to size this it's up there at the core but we want it to be here so we're actually going to drag this as a child of the switch panel boom and oh it was a child to switch panel it already was because that's how we created it but make sure it is and you can zero out the location by clicking reset to default and we should scale it default too it's probably way too big so we're we're going to rescale that by here you can also press r and scale it here which is fine it's scaling perfectly though which is kind of annoying we're going to scale it over here we want it to kind of be in front of the switch more or less so that when the players standing in front of the switch we're cool and that should be good enough file save we can test and see how big that box is so whenever you're standing in that box you click a button and do it now this box needs to react only to players so it's a collision box and collision boxes can do all sorts of stuff with physics but we want only to collide with players so in the details when you have the box selected go down under collision and you'll see overlap dynamics set that to overlap only pawn because pawns are what players are they're player characters so that'll only have a trigger occur whenever a pawn overlaps it and we need to add a component begin overlap function so we have this component begin overlap and we need to have it to where whenever a player overlaps with that box they're given the ability to have input on this be able to click a button to interact with it if you will and so what we wanted to do [Music] is enable input so right click type enable input and this is the um action that we want to be performed we wanted to be able to have this actor have input enabled to a player controller that's playing the game now the way we're going to do this is we're going to cast this to the player pawn and we're going to since we're using the first person project we're going to cast to first person character if you're using the third person project to be the third person character it's whatever your pawn class is in the game if you're unsure what that is if you go to your world settings this is accessed by going to window world settings you'll open the world settings tab and you open up your game mode you'll see the default pawn class is whatever you have set here maybe you have a different game and whatever your pawn class is that's fine so we're casting this actor which is the actor that overlapped with the collision box that we have and we're casting it to the first person character or the pawn now we need to get the controller get controller who's controlling that pawn which is a player in the game and we're going to cast that because this actually won't accept um a controller object reference it needs a player controller reference because anyone could technically control it so we're going to cast this to the player controller some people in your game will have a player controller reference in their pawn and you could use that too but since the default template doesn't have that this is how we're going about doing it but now that we have access to the player controller of the player who uh interacted or collided with this collision box we can now point the input to the right character to the right controller so now once the player essentially runs into this box they have control over the actor they can press a button to interact with it so we need to make a function for a button that can press so we're going to use the right mouse button so if we type in right mouse button you'll have an option here so when this key is pressed we want it to turn fan on or off to call this initial function that we had way up here right and at this point we should be able to perform the action now as of right now this will work in single player i believe let me make this big old screen so we walk up click the button it doesn't work oh no it didn't okay i wasn't standing in it and you see how it starts the animation up it's spinning i gave it the speed of i think 30 or something if you're noticing it's not spinning you need to make sure that when you place the the actor here under [Music] the details of the actor remember we had the setup thing that we created the fan speed needs to be set to a variable the default was zero and it won't spin you'll see the light switch flip up nothing will happen though but so i just set it to 30 or you can set the default over here under the blueprint to set it to 30 or whatever and it'll it'll have a default value to spin but i mean let's say we set it to 15 or something just to have this be a slow fan in general and we click play let me come over the fan oh i'm not standing there i made the box too small it spins a little slower so so it works oh now that stopped immediately that's not what we wanted right it just boom immediately stopped when i pressed the button that is because i made a booboo and we needed it to ah i did finished that's where it's supposed to be that was my error there what's the tutorial without errors right so come over here click the button starts stop it and it slowly slows down just as it should cool so now we want to see if this works in multiplayer as of right now you have a working fan in single player but we don't have one in multiplayer so first off i'm gonna make this just a little bigger it's too small and it keeps messing up that's fine probably huge now but whatever you don't have to look at it as long as you're inside of this box the way we set it up you can turn it on um if you wanted to make it to where you only turned on if you looked at the actual object you'd have to do a line trace uh we can cover that in different tutorials this one is just really for the option there so we want to be able to do this in multiplayer the way multiplayer works is that this particular action won't really work because it needs to the server needs to be the the one telling the fan to spin not the player and at this point we have nothing telling the server to perform that action it's all just a custom event we haven't told anything to make a call for the server or anything and we're going to use something called an interface to have the client be able to tell the server to perform this action here so what we need to do first is we need to create that interface so we're going to come over here to our content browser i'm just going to bring open a content browser for here we're in the tutorial still we're in blueprints well i'll just make it out here it doesn't really matter um however you want to organize it we're going to create a blueprint interface this is important whenever you're creating uh we'll probably put it in the blueprints folder that's fine but whenever you're creating things that uh actions that need to interact with other blueprints and you need them to be able to kind of make calls and interact with events and stuff like that super useful i have only barely started tapping into interfaces but they're really useful for multiplayer interaction and other things as well we're just going to call this player interaction and we'll put it in blueprints why not now we have this in player interface we need to create a function here that can be called by one blueprint to another and we're gonna there's already one here we're gonna rename it and we'll call this um interact object object object and we want it to tell or share the information of what actor it's interacting with let's go and switch the boolean to an actor object type after object reference and we also wanted to give it uh that's probably fine just just the actor whatever actor it's interacting with and then the blueprint of the actor can define what stuff it needs to do so now we have this interaction that this interface created and we need to apply it to the pawn because whenever you're doing multiplayer actions a pawn is the only thing that can communicate to the server because a pawn is controlled by another uh live player another connection that somebody on their computer is connected to the server and they're playing with other clients on the server so the pawn needs to be the one to make the call to the server to perform an action so the way we do that is we need to actually modify our pawn class um the default pawn class in the first person character is as i told you before in the world settings thing and we can just click this little magnifying glass to browse to it it's this little gun guy so we can double click that it'll bring it up here and we're not actually going to mess with anything that they have we're going to drag this viewport up top i'm going to squish this over there because we don't really need that and this is all the default stuff i'm just going to leave that there you you probably have your own stuff here but we are going to create a new graph this is kind of for cleanliness we're going to call this interact object yeah object and it's just so that we have a clean slate to work with i mean we could work on the event graph and kind of go over here or whatever and just work over here but just to kind of isolate everything that we're doing we're going to put it in a new graph a new space and we want it to be able to perform an action whenever this interface function is called somewhere in the game so to do that the pawn needs to be able to listen for that particular interface interfaces are added under class settings under pawns in any blueprint really if you go to class settings implemented interface we don't have any really added apart from the default ones but if we add the one we just created which we did player interaction it will now listen for any events that are called any functions that are called from that interface so whenever another blueprint calls this function it will tell our pawn to do something so we need to do an event and it's going to be what do we call that function we call it interact object interact object you notice it has this little icon on here because it's saying that this is an event that was called from that interface and we want it to call a function to interact with the server now the pawn is the only thing that can interact with the server and since this particular call came from a controller which was the player walking over and pressing a button the because remember the button remember the player controller gained the ability to have input on this actor and if we press the right mouse button it's this particular blueprint pressing the button this action needs to that the action is originating from the client the action the server call to make this fan spin on the server needs to originate from something that the server can identify with the player and so what we need to do on the pawn is not only have this interface call but also has a custom event that is run by this server we're going to call this server underscore interact object and we're going to have this replicate because this is multiplayer we need to replicate and only run on the server this is important that way people just can't really nearly have the the fan spin or have perform an action on the server and we need it to receive the input of an actor remember because that's how we're going to know what action to perform object type actor there we go i'll save and we need it to since we're specifically doing it with the um vp ceiling fan i'm just going to cast it to this now you can always have some kind of filter set up if you wanted to do it uh like some kind of master hierarchy of interactable objects and have it cast an object with a particular tag to a function or override certain functions every time an interact action is called you can do that too this tutorial is a straight example of how to perform it with just the ceiling fan though and we want it to perform that turn off turn fan on off and this is the server executing executes on server custom event now so whenever this interface is called we want to call this function so we're going to call this particular function and we want it to pass the actor that it is referencing whenever that call is made so we have the pawn interaction set up now we need to make the blueprint properly call that right now it's just having it call the function itself but you notice that that function way up here isn't run of the server this is just running the blueprint we need to actually call the server who needs to know what pawn is making this action and needs to make that interface call and according with the pawn so we're going to alt left click to disconnect that we don't need that anymore and instead we want to get the player pawn so that we know who is making this action who is pressing this button and we need to have a call to that interface that we created so we have interact object you'll notice it has this little mail envelope thing because it's sending this request to wherever it's being held at i'm gonna put that over here with pressed and the actor well that's that's itself that's this thing this uh this blueprint itself so we get a reference to self so now when the player inherits the ability to have input on this object and they press the right mouse button it's going to take the player pawn it's going to make a interface call to this function and it's going to pass the actor which is the ceiling fan itself and it's going to go to the pawn it will make okay i have the actor i'm going to run this function which is executed on the server and the pawn can make multiplayer executions on the server and it will cast the ceiling fan which is back to the blueprint and run this function as as a server as a server now in order for this to work properly we need some things to replicate because when this actor is moving in multiplayer we want all the players to be able to see the fan blades move we want all the players to be able to see this this switch go uh the switch go up and down because that needs to be replicated that needs to be repeated over all the clients so those things need to be set to replicated so if you click on the light switch just the light switch and scroll down to component replication say component replicates and we click on the fan blades and we go down to component replication when it replicates and the actor itself as well we need to make sure that if we click on the ceiling fan actor the replication right here under replicates is check marked we don't need to replicate movement because we're not applying physics to it and it's not bouncing around it's the rotation is manually being set so we don't need to check mark that work just this one right here and compile save now that we have all that set and under the event graph uh see we have everything set up properly so if we control s control shift s to save all and we click here this little button here we're going to switch this to client number players to 2 and we're going to click play i'm gonna put up one player here in the middle let me shrink this a little bit and another player window down here the weird fov going anyway so we have one player here i'm gonna walk over where's the fan walk over here and i'm gonna shift f1 use the other window so you see that we're two different players in the same game if i right click oh oh and the other thing we need to also replicate is this on off value because in multiplayer everyone needs to know if it's on or off because right here that is being called so we need to be able to replicate that as well so if you click on that variable or click over here and click replication replicated i noticed that i had an issue here we need to have this fan turn off as default that's what was messing me up let's go back over here try it again that was weird so we have in view both things and we bring up this guy walk over and click the button see how it turns on with both and we now have a fan that's interactable and replicated in multiplayer now if you wanted to have a sound play at the same time you just you use these same functions that we have here and whenever you flip the switch to the rotation you can have it play sound you actually have to multicast that because sounds have to sound aren't run by the server um you need to sounds can be spawned by server but sounds need to be multicasted to all clients so that all clients healer hear the sound so technically you'd have to make another function custom event play sound make sure that this is multicasted and have it play sound at location and get past the location if you wanted to play sound you have the location come out here get world location i have that there i don't have a sounds i'm going to use the default gunfire sound that they have here and if you press play whenever the light switch is hit it plays a sound you might not be able to hear it because i have my sound turned off me there it's super loud but essentially every time the light switches it plays that sound and both clients are the ones hearing it too so but yeah that is how to set up a fan that works in multiplayer and single player because we set it up you actually i mean we can switch back to single player and it will still work because of how we did the interface calls because the pawn is the one calling the action but it is that is the proper way to set a multiplayer as well is have the action be performed by the pawn necessarily the other actor hope you liked it if you have any questions just call me down below but thanks [Music] you
Info
Channel: Arthur Streeter
Views: 148
Rating: undefined out of 5
Keywords: unreal engine, unreal engine 4, ue4.27, ue 4.27, ue 5, ceiling fan, interactable, multiplayer, replicated, replication, singleplayer, light switch, interface, basic tutorial, basic, tutorial, beginner, toggleable fan, rotate fan off tick, rotate object with timeline, timeline, off tick, event tick rotation
Id: KFm301ug_oI
Channel Id: undefined
Length: 56min 57sec (3417 seconds)
Published: Sat Sep 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.