How To Turn On And Off Lights | Single Light And A Room Of Lights - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another um tutorial in today's video what i'm going to be going over is creating some light switches which will turn on and off the lights which we have in our game as well so this will work for a single light so one light switch will turn on and off one light and also work for multiple lights ie a room so this light switch will turn on this set of lights here and what we can also do is make sure the lights are either on or off by default when we first create them as well so let me hit play and show you what's gonna make today so let me actually go out full screen because it doesn't look as good uh what we're going to do is let's do this one first you see it's on by default we go up to it press e it's going to be turning on and off and that's only affecting this light here if i just go over to this one it is going to turn on and off these three so you'll see they're off by default if i press e it's going to turn them all on and off like so so this is what we're going over and creating today something nice and simple but something which you might want to have in your game so again this is what i'm going over and cranking today so without further ado let me leave this code and i'll show you how i've done it so the first thing we want to do is we want to create a blueprint interface so that we can actually interact between the light switch and the lights so we now have a way to communicate between those blueprints and this is also a nice and efficient way of doing it so let's right click go to blueprints and we're going to get a blueprint interface and i'm just going to call this lights interface that makes most sense for me you can whatever you want but this is the blueprint interface for our lights i'm going to open it up straight away now this is a read-only blueprint so all we need to do in here is create the start of the function which me i'm going to name toggle lights and that's all we need to do in here so we're going to access this via a different blueprint if you want a more detailed explanation of what blueprint interfaces are i do have a video we'll link to the description down below where i explain in more detail but we can compile save close that and that's all we need to do with this we're going to access it later on now we want to actually make our light blueprint so we're going to right click go to blueprint class create an actor and i'm going to name this one light bp opening it up straight away as well in here what i'm going to do is basically just add my static mesh for my light which for me is going to simply be a cube like this now you probably have your own static mesh which will look a lot better but this is a nice simple one which i'm doing for this obviously customize it to look how you want and i'm also going to add in my actual lights now i'm going to be using spotlights and i'm going to have three of them just so it takes up this whole kind of cube which i've got here as my static mesh again i just think that's what's going to look best for me but obviously do this however you want i'm also going to up the intensity to about 30 000 just so it's nice and visible and easy to see in the tutorial as my place is actually quite light already so again just customize this to get it to look how you want so i think that is going to be good for me compile and save i've now got my light in here with the static mesh and the three lights on here again this is just a customization of how it looks you can change it a lot but what you do need to do though is we do need to create some variables so i'm going to hit the plus variable here and we'll name this one light index and this is going to be an integer which is also instance editable so this means we can now set this light to have a specific index which will also set in the light switch so that the two lights and light switches with the same index are compatible and they will communicate with each other and because it's instance editable we can change this to be different for each instance of this light so one light can have an index of one another can have an index of two and three and so on and so forth and we're also going to create another variable called is on question mark this is going to be a boolean so it's true or false and again instance editable compile and save so what we'll do first is we're going to set up turning it on and off by default and that is the main use of this is on variable here so to do this we're actually going to go to the construction script and very simply off here all we need to do is get is on so you can hold ctrl and drag in the variable and this is going to set the visibility of our light so that's how we're turning them on and off is via the visibility so if you select all of your lights and just drag them in you can do them all at the same time like that drag out of one of them and set visibility we can also drag all of these into the same target of the set visibility then this will go into the construction script and what we can simply do is we don't need to have two of these and a branch to see if it's true or false we can just connect the is on straight into the new visibility there because if is on is true that will set the new visibility to true which means we can see them if it's false that means it's off so this will set this to false which means we can't see them so we don't need to do a branch to see if it's true or false then set it by just ticking it or leaving it false we can actually just input this and it'll do it for us automatically because this value will be the same anyway so we compile save that and that's nice and efficient now we can have the light either be on or off by default and what we're going to do is now actually have it so that the blueprint will turn it on and off i.e the light switch so we can actually copy all of this code we just created obviously other than construction script so select it hit ctrl c go back to the event graph and i'm going to delete these three nodes here and i'm going to go to class settings at the top under interfaces i'm going to add and we're going to add the interface we created earlier which i named lights interface like so compile and save and you'll notice on the left we know how we now have an interfaces tab if you open that up we have the toggle lights function which created i'm going to right click that implement function and now we have the event toggle lights function here so when this is called from the interface it's going to fire off this event here which will make more sense later on when we actually call it what i'm going to do is control v to paste in the set visibility here actually we'll delete the get variable though so we now just have to set visibility like so but what i need to do is we also need to actually change this is on variable so we need to toggle this so what i'm going to do is get is on out of this i'm going to get a not boolean and that will then set is on again like so which will then go into the set visibility like this so you should have something which looks like this now the reason we're doing this is when we call this function we're going to set is on to the not of what it currently is so essentially the opposite so if is on is true not of that is false so we're setting it to false which will then set the lights visibility to be false as well if it's false this will be false not means it's true we're setting it to true setting the visibility is true so we can see it so i hope that makes sense basically when we call this function we're inverting or flipping setting the opposite of what the current value is for is on and then also setting the lights to be the same value as well so again if it's on it turns off if it's off it turns on hope that makes sense so compile save and that's really all we need to do within the light bp we've now got that setup working perfectly what we need to do now is actually create the light switch bp to actually call that function so we can close it and i'm now going to create my light switch bp so i'm going to right click go to blueprint class create an actor and call this one light switch bp opening that up straight away as well now again in here in the viewport what i'm going to do is make a very very simple visual for it so again you can use your own static mesh if you wanted it'll make it look a lot nicer i'm just doing a simple cube shapes like this for the moment and actually that's what i'm going to do for it so again if you have your own satin mesh you can make it look a lot nicer what you do what you need to do though is add a component and add a box collision just so the player has a place they can be to actually interact with this light switch now if you have your own interaction blueprint interface setup which would be a lot more efficient obviously use that and then set this up for how you would normally use that and again if you want to do that as it is a lot more efficient then i'll leave a link to that description down below as well but if you want to just do it how i'm doing in this video there's no problem with that either so we'll compile and save so now we have a way in which we can actually overlap and interact with this light switch so what we need to do is create a variable in here naming this one light index the same how we did it before making this an integer and instance editable compile and save so again you'll notice earlier i said we have to make sure the light indexes are the same so they're compatible and can communicate with each other this is how we do it we create the index in here as well so now we're going to go with the event graph of this blueprint delete event tick and event act to begin overlap but use event begin play what we're going to do is get all actors of class with the act class being our light bp which we just created out actors is going to go into a for each loop going into there like so and then what we're going to do here is the array element we're going to right click promote a variable naming this one lights array or light array and then i'm actually going to delete that and select the variable again changing it to an array so the three by three grid there then we're going to drag this in and get out of this we're going to add so just the top add there connecting that to loot body and connecting the array element from a for each loop into the ad there like so so what this is gonna do now is when we begin the game it's gonna get all of the lights we have inside of our game and then add them into our own array of the lights as you can see here now the reason we're creating our own array instead of just doing ghetto likes of class every time is this is a lot more efficient we don't want to be constantly getting all of the actors every time we want to turn on or off the lights we just want to do it once at the beginning of the game and then just access that array whenever we need it so we'll compile and save that so that is a nice and efficient way of doing that now we just need to actually interact with the light switch to turn on and off the lights so that's also nice and simple what we're going to do is right click our box collision add event and add on component begin overlap right click it again add event add a component end overlap now what you can do is out of other actor you can cast your player character and what that will do is it will mean that this will only work if your player character is the one that overlaps it i'm not going to bother doing that but you might want to do that if you have ais in the game for example if the ai can overlap this you don't want to be able to interact with it when they're there other than that what we're going to do is drag out of begin overlap and enable input then drag out of end overlap and disable the input so disable input there so it's only going to be looking for the input key for our interaction when we can interact with it so when we're close enough to it we also want to drag out the player controller and get player controller for both enable and disable like so and i'm just going to move these down slightly like this and above the beginning of that what i'm going to do is right click and get my interaction key which if you've got an action mapping put that in there but i don't so i'm just going to do an e keyboard event like this and what all of this wants to go into is a gate so if we hold down g left click and get a gate like so enter will be depressed of the e open will be enable input close will be disable input so again i do have a video where i'm explaining the gate in more detail if you want that but essentially what this means is the gate will open when we are close enough to it and close when we're too far away from it it's being the light switch and once it's open we can enter it by pressing e but if it's closed and we try to enter it we won't be able to exit because the gate is closed so it's essentially like a gate in real life you can't enter it unless it's open so out of the exit this is where we'll be toggling our lights so now what we need to do is be able to actually interact with the correct lights that's nice and simple we're going to get our lights right which we created earlier out of this we're going to go into a for each loop connecting that into the exit like so and we want to now be checking the light index so out of array element we're going to get light index that we created earlier this will go into an equal equal integer and we're going to be checking it against the light index of the light switch blueprint to the one we have here so get the light index from the light loop and get the light index from the light switch this will return a boolean which we need to put into a branch so hold on b left click to get the branch with that being the condition and connect it into loop body so it's going to check to see if the light indexes match if they do so true we want to toggle the lights if they don't so false we don't do anything so out of the array element again we're going to just get toggle lights and we have class lights interface toggle lights message that's the one which we want so if you don't have that just do whatever you actually named your function in the interface so toggle lights message there connect that into true and we're going to compile and save that this should now be the code working perfectly for us so we've done is we've again managed to just set this up so it's going to perfectly interact and communicate with the lights that are the same as this light switch so if this light switch is two it will only toggle the lights off two so i hope that makes sense if i close this i'll show you how to now set this up in your level as well so we're gonna drag in our lights so i'm gonna get light pp here rotate this around a bit and just move this up just so it looks like an actual light and i'm going to leave these ones off by default so you'll notice they are actually off already and that's because the default value of our boolean is false so what i might do is change it to true so if we just select it hit true compile close it you'll notice these are now on but i do want to be off i just want it to be on by default but this specific one i went off so i'm gonna untick is on there i'm gonna put the light index at one so this is the one light now i'm gonna hold left alt and drag out to duplicate it and do that again so i now have three lights here all of them being off with a light index of one so these will all be activated by the same light switch bp and i'm going to add in the light switch now so i'm just going to simply put that onto this wall here rotate it around so it's facing the correct way and that's my light switch light index i'm going to set this one because i want this light switch to interact with these lights so you'll notice the light index is one on all of these so they now match and i'm also going to add one over here just to show that it works with just one as well and for multiple in your level so let me get another light in here actually i'll just duplicate this so it's the same level but i want to now set the light index to two and i'm going to take is on so it's on by default just to again show you working differently then i'm going to get the light switch here and rotate it around and the light index for this one will be 2 because the light index for this is 2 so these will now interact with each other so that's how you'd link them all together and i hope that makes sense as well so let's hit player tester out this one's on if i go over here press e it turns off turns on and it's only interacting with this one and if i go over to this light switch it should be turning on and off these ones as you see they're off by default and e turns them all on and off perfectly like so so i think that'll be it for this video as we've done everything we want to do we've set it up so we now have a light switch which will interact with lights which we have set up in our level and it'll work for just a single one or a row as you seen there which again would be good for a room full of lights and you can have the lights be on or off by default and it won't mess up with the code at all so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like and subscribe down below so thanks so much for watching and i'll see in the next one [Music]
Info
Channel: Matt Aspland
Views: 28,724
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, lights, light, switch, lamp, turn, on, off, single, all, row, room, full, blueprint, blueprints, interface, ue4 light, lights and switches, light switch, ue4 lights and switches, matt aspland lights, toggle
Id: vtqDWTIJITU
Channel Id: undefined
Length: 16min 22sec (982 seconds)
Published: Tue Feb 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.