Part 2 - Basic Blueprint Coding in Unreal 4 , Switching Light on/off

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the second tutorial in this series and after importing our scene into unreal 4 the next thing I want to show you is how to create a very simple interaction in our scene because the strength of Unreal 4 is not only in a walkthrough of the scene in real time for the realistic a walkthrough of an environment it also is in the interactions so if for instance I want to switch lights on or off in real time I can do that if I want to change furniture types in real time I can do that to change floor types and materials and open closed doors when I come near them everything that is an interaction can be done in unreal for all of these interactions or animations are done in code now the beauty of unreal 4 is that the code is a visual one so it's not a written code it's a code which is the nodes that connect to one another so it's not unlike the material editor in 3d studio max that you connect nodes to in order to create a material I will show you this in the continuation of this tutorial but for now just know that everything that is interaction or animation is done by code first thing we need to do is create an actor that wants to apply the code to so all these objects here in the scene are not actors there are just objects in the scene there are mesh actors there are not blueprint actors so there's no code on them so what I need to do in - in order to create an actor that and I can manipulate is go in this menu here which is all the objects in the scene all the objects in memory in the file and hit the right button blueprint class an actor so I'm creating an actor which is a blueprint which is a coded actor that does what I want it to do let's call it lamp and let's double click it in order to go into this actors world so this world is where the actual lives I need to import a lamp itself inside I need to do all the coding inside of this actor and just just drop it in the scene and it will just work so first thing I want to do is import this lamp here into the actor in order to do this I need to find the mesh that reprises this lamp that's this lamp is made of so in order to do that I'm going to do to go to the folder that was created when I imported the scene in my case is exp which is export that's the name of the file I gave in 3d studio max if you export it using a different name that name will be the name of the folder so if we go into here you have geometry where all the meshes are you have materials all the materials you exported here and you have the textures that you exported so we need to go to geometry and these two are our lamp so I'm going to click one and ctrl click the other and drag them inside the actor to this component list here so I'm going just to leave them here and as you can see the objects is inside of our actor so now let's make it bigger and I want to place the lamp correctly so let's go to the front viewport now this red line represents zero which is the floor so I need to take this one up to almost zero like so and this one up here now if when you go up with these objects they snap like this to grid what you need to do and you don't want to snap what you need to do is click here enable a disable snapping so just disable them and move it freely so let's place them here let's go to the perspective viewport and the lamp is placed correctly let's compile which is a save the code in other words and save the actor so let's close it up let's go back and that is our actor so now I can just drag and drop it wherever I want in the scene so let's say here next thing I want to do is I want to delete this scene in this lamp that is not a blueprint actor from the scene because I can't animate it so I'm going to choose this one and this one and delete from the scene next what you delete from the scene is not does not disappear it still stays here in this menu so you can go inside and you still see the lamp but it disappeared from this menu so which are the objects that in are in the world right now so you can have objects that are not inside the world right now that you can use in a later time there are not in the world so these are the objects that are in the world and these are the objects that are in memory let's say in the file itself all right so this is our actor I will move it to about the same place that our lamp was so let's go to the top viewport and place it here all right so the lamp is in place let's go back to the our lamp actor and this is the coding section we still don't need that we will get to it in a minute we need the viewport so let's go to the viewport and you can see the lamp shade and the lamp in place everything is fine next thing I want to make sure is if I click on this object here a move ability you have three options here you have static stationary and moveable every object light and everything in a unreal it has these three options that you need to choose from so moveable is an object that will move physically now our lamp will not move physically it will only turn on and off so moveable is not what we want here stationary is something that is stationary but will change let's say color or brightness or things like that it can if we're talking about the lights the light will come on and off you need to set it to stationary static is something that is static that will not move not change at all so you can change its color and you can change its texture but you cannot move it and you cannot flip it on and off in the case of a lamp so most of the objects and lights in our scene will be static because when they are set to static they affect the global illumination which is what we want we want photorealistic global illumination like in viewing so only if we don't have a choice we will set it to stationary or movable so now we will set our lamp to static and our shade also to static all right so both of them are static the next thing I want to add to this actor is the light itself I want there to be a light that will shine on the wall so I'm going to add a component here and I'm going to write light and I have a couple of lights here a directional light which is like a Sun I have point light which what we need a rectangle light which is like a viewer light which is like a plane skylight and spotlights so we need a point light it's a lamp so I'm going to take a point light and as you can see it is now a child of the lampshade we don't want it to be a child of the lampshade so let's just take it and drag it on top of the default scene root so now it's another child of anything all right let's take it up a little bit let's place it in the center and let's go to the front place it in the center of the light bulb so here and let's go to the left it's fine and top it's fine all right so now it's in place if we minimize this and look in the scene you can see that the light is shining brightly on the wall so it has a strong very strong effect in order to control the brightness of the light I will choose the light in the viewport and I will go to the intensity here it's 5,000 a unitless so I wanted to be with units so I'm going to choose candles 5,000 candles is crazy bright so I'm going to take it down to something like 2 candles it's a bit dark so let's say 5 all right so five candles is on our light is now on ok next thing I want to set in the viewport which is the last thing I want to set in the viewport is a perimeter so if I walk in the scene let's say let's close this actor down let's play if I walking the scene I want a when I walk a certain distance from the floor to have a text that tells me hit F in order to lamp for the lamp to turn on and also if I'm too far away from the lamp if I hit F nothing will happen so I need to set a parameter where all these functions will happen and if I'm as a character outside of the parameter nothing will happen all right in order to do that let's go back into our actor back to the viewport and add the last thing called collision box so let's right box you have box collision so this is the parameter where when the actor when the player comes inside the perimeter these whatever you code will happen so let's go to the front viewport and make it bigger this is the scale portion so let's scale it up scale it up here but this is the move let's move it up top scale this way and let's see in our world so if I go inside this square here the functions will start working okay so let's make it a bit bigger let's say like this and like this and a bit taller so it will intersect with the ceiling here like so alright so now we have a parameter when we go inside the parameter we can start hitting the F key in order to it set the light on or off when we are outside of the parameter we cannot do anything with this code okay so let's go back into our blueprint actor let's go to the perspective now we have everything we need in the viewports next is start coding don't be alarmed it's very very simple and intuitive so let's go to the event graph this is the coding section of this blueprint so now what I want to do is when I go inside this perimeter here all right I want there to be a text that says hit F in order to turn on the light all right and when I go outside this parameter I want the text to disappear so first things first I forgot I need to add text in the viewport so let's add the text here add component text text render and here you have the text let's rotate it like so 90 degrees let's see in the world how it looks throw that it back like so 90 degrees okay so this is way too big and it's not in place so let's go to the front viewport and take it up even to the left you port yeah here's the text stick it up on top of the lamp let's make it much smaller so the size is world size here let's make it 5 and let's write what we want so press F to switch light on all right so you have the whole text here now the text intersects with the wall here so I need to move it a bit inside like so let's make it even smaller for okay so now we have the text now if I compile and save let's try playing this the text is always on alright never mind if I'm inside the parameter or outside the text is on now I need to code it let's go back to the actor and go to the event graph and now I want a for this code here to tell the software when I'm inside the parameter the text will be on when I'm outside the parameter the text will be off so let's go back to the viewport and turn this text off for the beginning because I'm starting the level out side of the perimeter so the text is off and now let's start coding when I want to a certain object to do something I need to tell it here in the blueprint so I'm going to take the text render which is the object I want to do something and drag it inside okay so we have the text render this is our object that we want to turn on on off now let's click and drag and I want to set it to on so we'll just hit set visibility to on new visibility is on so the text render will become on now this isn't connected to anything so we don't have anything to connect it to that will tell it to become visible so this will essentially do nothing for now but there are three nodes here there are by defaults inside this event graph one of them is called event actor begin overlap so what this does is when we as an actor overlap this collision box that were created this event will start firing so I want to connect this what we created here let's move it down let's move these two back want to connect event actor begin overlap of this event when an actor comes inside of our a collision box we want to set the visibility of the text to on let's compile save and try this very very simple code let's go to play no text when I go inside text appears go outside the text is still on but it appeared it wasn't there when we started to play now in order for it to disappear when we go back we need to continue coding so let's go to the lamp it's very intuitive event actor begin overlap so the other event we want is event actor and overlap so when the actor goes outside ends overlap with this collision box something will happen what will happen set visibility to off so the visibility will become off so let's take this node here control C control V is copy and paste like any other software the target so visibility of what will become on and off of the text render of the text so we need to connect it to the targets if we don't connect it to the target the set visibility will not know what to turn off visibility of what to turn off so next thing we want to do here is the visibilities on when we begin overlap we only end overlapped visibility is off so now we need an event actor event actor and overlap all right so it's the same as this one but end now let's connect it so when we end the overlap the event of ending of the lap starts the visibility of the text will be set to off compile save let's see what we have here when we go inside our collision box text on outside text off and we coded our first code in unreal not that bad is it now let's go inside outside all right next thing is the light itself we want the lights to turn on and off when we hit the F key so let's call that let's go back to the actor and create a new event so we had an event that begins overlap and ends overlap now the event we want is when I press the F key so let's hit the right key a right mouse button and press F key and it will continue and you will see here keyboard events when I press F I can make it every button I want all buttons are here you can make it i FG whatever you want so I'm going to go with F so just hit it here the if it's red it's an event so when F is pressed what will happen is one one second before that let's go back to the viewport and turn this light oh one more thing the text render is a child of the box collision it doesn't really matter but I like everything to be in the same hierarchy no a child of anything to just take the text drag it to the defaults in route so now nothing is a child of nothing now let's select the point lights and let's turn it off so in the viewport you have the light intensity on five I want to set it to zero no lights all right so we set it to zero because when I begin the scene I want there to be no light only when I press F the light will turn on so let's go to the back to the event graph when I hit the F key now what we want to do is to affect the point light so like we took the text to effect it we need to take the point light drag it in and from it drag out and set intensity so we want to set the intensity of the point light to five when do we want to set the intensity to 0.5 when the event of the F key pressed is active so going to connect when I press the F key set the point light to 5 intensity it is very simple let's compile save and try this now if I play the game I go inside you have text outside no text hit F key nothing happens so let's see why not let's go back here by default unreal doesn't recognize keyboard shortcuts that are created inside a blueprint actors so honored for a software to recognize our inputs from the keyboard we need to add an input so I won't want to do when event actor begin overlap sets the visibility of the text to on and then continues to inputs to enable input now whose inputs do you want to enable so let's say we have a multiplayer game there are like a hundred of different actors or players in the scene so you don't want every player to be able to open a light or close the light so you need to sell the software which player a his input will be affecting this actor so the player is us in the architectural visualization field it's always us we won't have two players in the scene so when player controller you have this node player controller it will always be let's drag it out utility you think it's a ring no it's game okay it's always get player controller so when we set player controller player is us player index zero is the main character which is us so always when you have in architectural visualization player controller always connect a get player controller player index 0 to it if you don't connect it it will not know a whose input to enable so it won't work ok so what we have here when we intersect when we begin overlap of the collision object of this field a collision box here the visibility of the text will become on text will become on and then it will continue and enable the input of the player so now when I hit F inside it will work let's go inside text on F turns on the lights okay so this is working next thing if I hit F again nothing happens the light will not turn off and why is that let's go back to the coding that is because we didn't call that we only have when you press F in flight intensity becomes 5 that's it we don't have light intensity becomes zero again so we need to make a switch out of it a flip-flop switch so every time you hit F it becomes either 5 or 0 so in order to do that let's drag from this pressed F press button drag out and hit flip second flip-flop you see it connected automatically this one and the a to the set intensity so this flip-flop is when you press the F button first time a will play so set intensity to 5 for pointlight when you press the F key again B will play so let's copy this copy and paste ctrl C control V and set intensity to zero whose intensity do you want to set to zero the point lights intensity and when we want this intensity to be set to zero in B so the second time we press the F key and the third time you press the F key it's a again fourth time be a B a B so it's a switch all right so now let's compile save and start playing let's go inside text on switches on switches off on off now last thing we want to do if I go outside of this parameter and I hit F it will still switch on and off we don't want that because let's say we have like five lights in the scene or you want to do a different internet interaction for the F key for changing the floor let's say okay so if I hit F again it will just change the light also so it will change the floor but it will also switch the light on and off so I want to set this a light switching to be a only effective when I'm inside the box collision in order to do that I'm going to go back to the lamp vector blueprint and I'm going to do one less thing so we have a whene actor begin overlap the collision box itself the visibility of the text to on and it sets enable input for our player 2 also on the the input is enabled now I didn't disable the input when we the actor ends overlap when it goes when I go outside of this collision box so in order to do this it is very simple just drag it out and like we did the enable just hit disable so you have disable input same thing as this just disables the input disable input for which player for our zero player for us so player controller and compile save and now if I try playing this go inside hit F light turns on and off when I go outside hit F nothing happens so go inside turns on and off and text also turns on and off so this is a very basic understanding of the unreal for a blueprints coding languages it's very simple very intuitive don't be afraid if you didn't 100% understand it it takes a couple of days but I promise you it is very intuitive ok so in the next tutorial we'll try a couple of more interactions let's say changing the color of the floor if I had a key or changing the type of sofa all right and then we'll get to more graphical things like you have a very dirty like a splotchy wall here so we can fix that and AH make a higher resolution global illumination so the scene will look much more photorealistic and their sets better reflections and things like that we'll do that after we learn basic blueprint interactions so thanks for watching and I'll see you in the next tutorial
Info
Channel: 4dvisual
Views: 29,516
Rating: undefined out of 5
Keywords: unreal 4, blueprints, unreal blueprints, unreal4 lights on off, unreal studio, datasmith, blueprint actor, blueprint, unreal 4 blueprint
Id: amqrh6DGda8
Channel Id: undefined
Length: 25min 43sec (1543 seconds)
Published: Mon Oct 08 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.