HOW TO USE EVENT DISPATCHERS FOR PUZZLES IN UNREAL ENGINE 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody it's Andy here from am media games in today's video I'm going to be showing you how to set up a puzzle system in uh using event dispatches inside Unreal Engine I'm using 5.1.1 so if you're on a different version some of the code might be slightly different but should work if it doesn't I do have a Discord Channel where you can reach out and I'll try and help you fix it um to the best of my ability so without further Ado let me jump in and I'll show you how it's done so the first thing we're going to do we're going to need to set up some kind of um puzzle keys or pieces now if you think of games like uh Resident Evil where you have to find like a ruby or a diamond or an object and you place that in a slot uh on like a statue and it'll unlock a door um or you need a a set of keys maybe multiple two or three or even four keys to open a specific door or different types of objects so that's the kind of thing that I'm going to be showing you how to do but it's going to be physics based so if you can imagine you've got a maze so you're running around a level in a maze and there's uh four or three different puzzle pieces scattered around you have to pick them up and hold them while you run back to the door and place them in the the corresponding slots or or places that they would need to be in order to unlock that specific part of the door once you have all of those pieces together you can then unlock that door and then you can progress through the level so the first thing we're going to do I'm going to create a folder I'm going to call this one um puzzle tutorial um event dispatches excuse me now in here uh I'm going to place my fre actors that are going to act as the puzzle keys or the pieces that we need to open the door I'm going to be using a blueprint um interface for my interactions and I'm also going to be using line traces in my character blueprint in order for me to pick those object uh objects up if you don't know how to set up a blueprint interface I will link a video um in the description or I'll pin it as a top comment in the comment section uh like I said if you need any help on it I will happily help anybody that asks so I'm going to be using I'm assuming you know how to use a line Trace system so we're going to right click and we're going to go to um blueprint class and we're going to add an actor we're going to duplicate this two more times so there's going to be three I'm going to name this one to p puzzle piece um for door one and then we're going to copy this and paste it two more times so this is going to be Piece One Piece two and piece three we can have as many as we want but I'm just going to use free for the tutorial so you can imagine you could place these around the map so you could have these in different places and you could have a huge maze you're being chased by a couple of AIS and you've got to try and get to the door and find those pieces without dying it's a simple game process quite fun it's done in things like um what's that game uh Piggy on Roblox and it's done on uh granny where you have to find different objects so it is it is a tried and used and tested means of fun gameplay but it's all about how you set it up with the Ambiance the lighting the sound Etc so we got puzzle piece one in we need to set some code up in here here so we're going to go to uh let's go to puzzle piece one let's open that up and we'll pop that there and we're going to add in a component so we'll do a let's just get a cube and we'll shrink this down so it's going to press R and Shrink this down maybe .2 let's see how that looks in the world it's probably a bit small uh we'll do a little table we'll add a a table to this uh just so it's got something to sit on and then we can go back to the folder that was on so we got puzzle piece one and we're going to place that on top of the object just like so and we need to give this physics so we're going to enable simulate physics and mass press play so we can see there that it has physics enabled let's play from here and let's test that y so I can pick that up so I do have code set up to pick the object up this is the code more for the puzzle side um in my first person character I do have a line Trace setup for interactions and a line Trace setup with uh a physics handle in order to be able to pick up and interact with objects um and I do have both of those tutorials as well so now that we have our puzzle piece one we also need to do the same for the other two pieces so I'm going to create another one of these and another one of these so we'll do that place that one there and we'll place so actually we'll delete those two because we can copy this one so then we don't have to add the key cub in that makes more sense and would also do this one so we have those three set up now that's done so now that we have those set up we need to create a door that we will need these puzzle pieces for so we've got three puzzle keys or pieces for our door that we'll need to unlock it and what we're going to do now is we're going to create the door and add um an animation to that door to open so we'll go to let's go and add a let's add we'll do modeling and we'll place a block should we do Cube grid no we'll place a block in and we can duplicate these so we'll make a little bit of a doorway and we will place so we'll make this the door and then we'll Place another one here slightly thicker and then another one on that side and then against my better judgment oops I'll also Place one above the door not a big fan of doing it like this but you know just for the tutorial so we've got the door frame and somewhere for that door to go to there we go okay so we have a door section here that we can animate to open or close we can have it going up or we can have it slide out to the right um so now that we've done that we can then add in the slots that we need um in order to uh place these objects on two to then open this door so we're going to make the let's have a look why have we gone out of that folder uh so we're going to make the um position or the pedestals that these are going to go on or we could just do slots on the wall to be honest we could do yeah we'll do color slots so we'll make three slots here so we'll do this one right so actually was it. 2 it was 2 wasn't it yeah but it wasn't it's not going to be the same size okay so we need it roughly to be around the same size as this one and then we'll place that into the wall so we're going to have one there one here and one here just like so let's move those out a little bit what we could do actually is if we make copies of these let's move these out make copies of them place these in the wall and then we can go and create this into a Boolean object so we've got actual slots so we go modeling uh select this and this and then we'll do um Bing mesh ball can see there we've cut into the wall let's move it in a little bit until we know so about maybe here got slot one um and then we'll do accept actually what we'll do is we'll move all of them into position not this one just these so we get an even depth on each one okay so move them over select this one and then this one and then we'll do mesh ball and then we can do accept and then this one again and this one and then select mesh ball click accept this one again and then this one mesh ball then click accept Okay so we have three slots and we have three puzzle pieces so these pieces need to match really these sizes which they should because they were the same ones they just need to move over a little bit so they're going to go in here like so but we're going to set them to Invisible at first that's going to go all the way back like so okay so you can do this many other ways um but this is just the way I'm doing it for the tutorial so we have the wall set up we've got our slot set up we have our puzzle pieces that we need to find in the world and we're going to set these to um not rendered so visible why am I typing in the outline uh visible not visible these three and do that and then we'll do Cube and then move to create folder these are our puzzle wall pieces and now that they're done we can then go into our level blueprint so let's open up our level blueprint and we need event begin play but we don't need event tick and what we're going to do out of event begin play shortly is set up our bind event system for our event dispatchers so we're going to create um a couple of triggers so we need three overlaps or three trigger boxes and we can do that with these so our little trigger boxes and we can adjust these sizes down so we're going to make it so it fits that slot perfectly so when the player places the piece on the wall in that specific area if he has the correct piece in hand it will overlap fire off the code and set up the series of events that we need for that specific piece so we have that in place we can copy this and make another uh two more times so we have one two and three let move them out a little bit once they're all in place we need to make some codes so in our level blueprint we're going to right click so make sure you select one of them um and then right click and then add it's for trigger box one Collision pick an overlap you're going to do the same for the other two right click add Collision overlap and then the last piece add collision and then uh overlap so you've got three overlap boxes or triggers for your puzzle pieces and this is going to detect the uh blueprint at that we've created so what we want to do now is we go cast to puzzle piece one excuse me we're going to select we want to select these in our um level blueprint so we're going to cast to so this is going to be um puzzle piece one so we're going to go and get the name so the name was puzzle piece one so if we do F2 contrl C and then go to level blueprint and cast two and then paste so we can see here we got one two and three so let's do one and then cast two paste pzle piece number two and then do the same again cast two paste it and then number three so we have one two and three set up so they're all done they're all ready and the next thing we're going to do is destroy ATA because we don't need them anymore so we're going to destroy ATA once we've placed them in place destroy that and Destroy haor so we have that set up that's one two and three okay let's move this up now that we've done that we can set the visibility for piece one to visible which is on the wall so if we go into our level and remember we created let me move this down make a bit of space we cre created a folder earlier Cube 4 which is this one Cube five which is this one and Cube six now we created a folder because they're invisible currently and because they're invisible we won't be able to select them because we can't see them so if we create folders and keep things tardy in the outliner we can now select those meshes go into our um level blueprint and we're going to right click and get a reference going to do the same for the other two so we're going to grab these two right click and then scroll down reference two selected actors pop that one there that one there and that one there so this is for number one this is for number two and this is for number three we're going to set visibility static mesh component and we can copy this paste it and paste it just like so plug that into there and then I'm going to plug this one into there this one into there that one in there oops and then we have that set up it's ready to go and this one going to set visibility to true on all three once we've done that give it a bit of a tidy probably do something like this actually it's a little bit tidier okay so we have our first trigger box set up second and third so we'll continue the code in each one of these we're going to play a sound at the location where we plug in our um puzzle pieces so we're going to get a play sound at location we're going to grab a reference to this we're going to grab this control and control uh contrl c contrl v to paste uh play sound and location and we want to get World location disconnect that place that there there we go uh delete that actually we don't need to do that just oh yeah we do need root component my bad um so we got the root component which is the location of our static mesh and I'm going to place that just like this we're going to copy those free contrl C and paste move these down in a second make a bit more space so move this down move this down and then we're going to get this one paste that there plug that in get this one paste that there and plug that in so this is what you should have so far um trigger blots to detect the overlap so when the player places the piece on the wall this is going to detect that trigger or that that overlap event does the actor overlap the trigger box if true destroy the actor that overlapped it which will be Cube six uh not Cube six sorry puzzle piece three and set the visibility for the one that's hidden currently to visible and then we're going to play sound at location so we're going to go uh click not too sure if we have a button for noise for it uh switch no see if we can find one okay just just like that turn on see got anything nope [Music] switch place H have I got a sound for interacts maybe let's a I mean that'll do just some kind of audio notification that we've placed it in the right place okay so that's done now we just need to set up a variable and we're going to do that by using the variables panel on our left left just here we're going to click that three times technically six and this one is going to be um press f2 to rename so this one's going to be has puzzle piece oh actually we'll do puzzle piece one in place this one's going to be the same so let's copy that let's paste it change the one to two F2 paste change the one to a three we have those set up so this is going to go here this one is going to go here and this one is going to go down here so now they have those set up it's very repetitive but it's nearly done we're nearly there um we have that set up so now we're going to do the uh event dispatcher so we're going to go and tap this six times so when you press it once it will place it then goes past the rename stage so we're going to call this one uh we don't need to go into the graph we're going to call this one um call puzzle piece one and then we're going to copy that so copy rename it and that's going to be two and then rename this one and that's going to be three so we got one two and three so this is going to be using event dispatchers now technically this is an event dispatcher so is this and usually when you have um an object like if I go into my puzzle piece and then scroll down in the panel you can see these here these are actually event dispatchers the only difference is these are coded in C++ they're background they're already in engine pre-built ones and when you use an event dispatcher here you are stipulating the process of the code you are setting it up yourself so that basically is what an event dispatcher is it's just an event that dispatches code based on a specific scenario or or specific factors so um we're going to set this to true on each one of these so make sure you set these to true so this is what it should have so far trigger box cast to the puzzle piece and then you got your Cube which is set in as visible and then we've got to play the sound which is a notification to players slotted that in so you can get like a nice kind of um Place uh slot an object in um and then you have some kind of an audio signal to the player you've done that correctly it's done so the next thing I'm going to do is call these events on their specific point so we're going to call here so you're going to drag call connect Up drag call and then connect it up so when this event happens this is going to send out a message and we need something to receive that message and we also need to bind the event to the object so what we're going to do now is set up our a uh dispatcher in our event begin play and we're going to enable that the ability to receive signal or receive a message um on begin play so we're going to bind event so drag out of our event begin play bind event uh two and then we'll go puzzle so we have bind event call puzzle piece one and we're going to do the same for this so we can do that by bind and then this one bind so you can type or you can drag out and drop now we need to create a custom event and this is going to be our Open Door event so our custom event let's add custom event this is going to be our Open Door puzzle event and we need to connect so every time we place a puzzle piece in the wall we want this to be called off and when we have all of the puzzle pieces in place then it can fire the final piece of code which will open that door so open door puzzle piece event needs to go in here so this is our delegate pin so this is going to receive the signal on these binded events I'm going to send that into there and actually we can do it a bit cleaner than that place that there right so place that there right so it's a little bit tidier a little bit cleaner that's going to go like this press Q to tidy that up and straighten it out there you go so we got all of these just like that so now that we have that done we need to create three branches so hold B for Bravo or B for branch on your keyboard and left click to create fre branches you can place one then copy and paste it if you like and we're going to set these up just like this and these are going to give us the ability to stop the code so if we let's say we place puzzle piece one and we have that the condition's true it's now in place but we don't want the door to open straight away we want it to see and also check to make sure we have the other two pieces in in place so nothing comes out of false true goes into true uh true goes into the input on our execute input pin there nothing comes out of false and then true goes in so you're just going to connect them in series just like so so we got true and then true nothing out of false the condition is going to be these variables so get and place get and place and then the last one get and place so now we have these set up let's uh let's do a bit of a tidy straighten these out and this goes into here so we got one got two we got three and then we want the code to fire off but we only want it to happen once so we're going to get a do once not a delay o for Oscar left click do want true into the UT and then after we've done that we then can add in a delay so let's pop that into there and our delay is going to make it so it happens a little bit after we place the third one in so it's not instantaneous so we'll go and add a a 4C delay and then we need to set up our level sequencer so our level sequencer will be um our door animation so let's select our door let's go to add level sequence call this one our door puzzle open anim and then we're going to select our door hit track at oops add box uh we get rid of scale and rotation location is going to be on the z-axis cuz we want it to move up if you want it to move another Direction that's fine you just change specifically which one you want it to be it's going to delete them to keep it nice and simple so we just got a nice clean graph press zero and then we're going to add a pin by clicking the little plus button and then we're going to move this to let's say the 5c Mark let's do five and then 5c Mark we're going to move this door all the way up just like so so then if we go back we can see that it's closed if we go forwards we see that it's open so that's done click save come out of that select our uh we want our door but we actually want our there it is we want our little uh level sequencer move that in position so we can see that and we're going to set on this let's move this up we want to pause end and that's it so now that that's done we're going to go back into our level blueprint and we're going to right click and get a reference to our puzzle our animation we just created on our in our level sequencer um we're going to get play play sequence player and we're going to plug that in after the delay and then what we want to do is we want to then for performance reasons we want to unbind all of these so if we know that this has now been successfully called the door is now open we add a small delay at the end set this to 1 second and then we're going to select this unbind select this unbind and this one unbind place that in there place that in there and then that in there Q to straighten move them up and there we have the code for a puzzle door so this is going to look out for the event we're going to have the variables in place to make sure the door can't open unless we have all three these are the individual puzle piece triggers so if we go into the level compile what have I missed let's have a look oh yeah uh I need to do I need to do it differently uh I need to set this unbind um all events from puzzle unbind all events from puzzle piece one and then we'll do unbind all events from puzzle piece two and then we'll do unbind all events from puzzle piece three yeah my bad my mistake selected the wrong one okay so compile that uh so this is what it should look like this is what you should have so we'll quickly run through it we got event begin play we got the binds this is going to kind of like listen out kind of like a fire Watchman or somebody waiting for a text message so these are all waiting for a signal they've been binded to those events when it receives a signal from from these it will fire this code it will check to make sure we have all three pieces and then once we do the door can open so let's compile it and let's go in let's play so we have puzzle piece one it's three sorry place that there so we have one in number two I forgot to put a sound and then number the first one and then the door should open just like so so obviously you can prettyy this make it look better is there any questions leave them in the comment section um if you need any help with this leave a comment in the comment section or you can reach out to me over on Discord um if you like the video like it if you dislike it for any reason I'm not sure why um then dislike it and if you want to help support the channel hit the subscribe subribe button um and share it with anybody else that might need help so I appreciate everybody watching um if you need any help with the line trace or the physics interaction then let me know there is a slight difference between 5.1.1 and physics handles compared to like 5.2 and three there's been a few people with issues with that but it's easily resolved just let me know and I'll help you with that problem but yeah so hopefully you like the video if you like it like I said give it a thumbs up up helps with the YouTube algorithm that's it for this video I'll be releasing some more soon so stay tuned for those thanks very much for watching take care and I'll see you in the next one bye-bye for now
Info
Channel: AMMediaGames
Views: 360
Rating: undefined out of 5
Keywords:
Id: FrNPhq7khMQ
Channel Id: undefined
Length: 34min 11sec (2051 seconds)
Published: Fri Feb 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.