How to Use SIGNALS Correctly in Godot 4 (everything to know)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello so signals are very important if you want to have a smooth running game most of the time a lot of people don't set them up correctly and think about it signals are basically nodes talking to each other this process happens all throughout the game so you can kind of see that if it isn't set up correctly that it could get pretty messy fast like do you want to inform the apple tree that has been picked and to reset the growing process then use signals or maybe you want to inform the arrow that it's been shot or maybe the enemy that it's been hit now these are all really basic examples of signals and they can get so much more complex and in this video I want to explain how signals work and how to best use them as we go through three different in-game examples in example one we'll quickly cover the basics and how to use different types of signals then in example two we'll create a world with different plates that send signals to the background to change the color to match the color of that plate and in example three we'll go over how variables work with signals and how you can pass variables through signals as we create this enemy attack and the updating player health bar but quickly before we get started if you could please subscribe and leave a like on this video so more aspiring game developers can also learn to make their own good do games as well then it would mean the absolute world to me okay so this is going to be example one world inside of our project we have example two world and example 3 world so if you want to download this this exact file for all these different example worlds with the existing code then that's going to be linked below so you can follow alongside as we go through and we create changes and make changes to connect signals to different things to learn all about how signals work so there'll be this project will be linked completely for free in the description below so you can go and download it and follow alongside with me okay so in example one we're going to go over two of the most basic signals either signals from an existing node or signals that we create in our own script so signals from an existing node are very very simple if we go to our world and we click on the add node and we add a timer node and we can also go and we can add a button node right and we're going to connect both of these for existing nodes signals but I want to go over one thing so button if we go up here to node and we click on node and we see the signals you're going to see all of these different signals that the button has but if we go to the timer you're going to see that it has different signals every single node in the entire game is going to have different signals from each other like the Sprite 2D is going to have its own signals and the node 2D is going to have its own signals right right so they're all going to have their own signals and these are events that happen so for example we have a button and an event that could happen with a button is that the button is pressed if the button is pressed then we're going to emit a signal and it's going to send that signal to whatever node we want it to send to so if we raise our button here make it a little bigger so we can actually click it we can go to our button and we can click on this press signal we can double click it and it's going to pop up a connect a signal to a method we're going to connect this signal and it's going to create a function method inside of our script and that is going to be our Sprite 2D script so if we click connect you're going to see that is going to create a function inside of our script basically whenever we click the button cuz that's the event it's going to send this signal and it's going to run this function when we click the button what function are we going to want to run well we're going to run want run the toggle function right so it's going to run our button press function then we'll call the toggle function and it will toggle our node spinning on and off so if we save save that we go and we click play you're going to see that it's spinning if we click the button it's not spinning if we click it again it is spinning if we click it it's not spinning but if we go over here to the button and we right click this is how you disconnect the method and we disconnect it and then we go and play well you're going to see that the button no longer works because the signal is no longer connected right so that's how we know the signal is working so if we go to the button and we click pressed and we connect it back to Sprite 2D as long as it has the same receiver method down here then it's going to match up perfectly with it right and it's right here it shows that it's connected to this but if we go to the timer you're going to see it's going to have different nodes and just real quick we can create a timeout node we can send it to the Sprite and this is the same thing an event happens the timer hits the end of its timer and it's going to call this function right so maybe we can say something like um self. visible is going to equal to the opposite of self. visible every single time that the timer goes off so if we go to our timer and we go to inspector and we make the weight time maybe 0 2 and we click it to auto start well you're going to see that if we play the world it's going to be flashing because that's the signal that the timer is calling and if we pause it it's still going to pause and it's still going to move and it's still going to flash right based on how the signals are and that's going to get pretty annoying pretty quick so we're going to go and we're going to go to the timer we're going to go to the node we're going to disconnect the method we're going to go to the button we're going to disconnect the method as well and then we're going to remove both of these nodes because we're no longer going to use them and now we're going to go over creating a custom signal right so creating a custom signal in side of um a script so to do that we're going to go to the example One World up here we're going to create a script on this example world and in this world the only reason we're going to create a new script is so we can create a signal right so to create a custom signal all we're going to do is we're going to type in Signal it's like variable so if you want to create a variable right if you want to create a variable you're going to say VAR um whatever your variable name is going to be so devor right bar devor is equal to one right our V devor is equal to subscribe whatever it may be equal to right so that's how you create a variable if you want to create a signal you're just going to type out signal and you're going to put the name of the signal here so the name of our signal we can go for something like just toggle um icon spin right so toggle icon Spin and then basically what are we going to want to have to happen in our process function we can kind of check for something like if a button is pressed we can say if input do is action just pressed UI accept and then all we can do is emit signal but how are we going to connect this signal to what we want it to be connected to well to do that if you go to the node you're going to see that it's going to have created a brand new signal these were the existing signals on the Node already but now we have a new signal that is inside of the script right so this is a example one world. GD signal that means it's a custom signal that we created if we double click it it's going to connect to whatever we want to the exact same way that we connected the other one from an existing uh signal so if we click on Sprite 2D we click connect and we can just go here and we can make the event to toggle we click play and you're going to see that if we click the enter button it's going to call it based on the signal that is being emitted right so that is how you are going to do custom signals and existing signals okay so now that we're done with example one we're gonna move to example two and in example two we're going to have a script that's GNA have different area 2DS which are going to be plates and if we walk on top it's going to send a signal to that script that's then going to send a signal to the main script that which is going to go and send a signal to the background which is going to change the background color based on whatever color that the plate is equal to right so this is going to actually it looks complicated but it's actually going to be very very easy so if we hit Area 2D and we make an area 2D we can name this area to the plate one right so this will be plate one and plate one is going to consist of a color red and this color wct is going to be we'll just move it right here and this color wct the color on this color wct is going to be equal to whatever the background of color rect is equal to so we can just copy that color we can come back and we can paste it here and you can see that it's going to be the exact same color because this one's just going to set the color to this right it's going to set it back to default then we can add a collision shape 2D in this Collision shape 2D we want to be equal to a rectangle that I did not mean to make that a a car or a child of color W I meant that to make that a child of the plate one so it accidentally put a child on the wrong uh node but if we make it you know somewhat even well you're going to see that that's going to be our plate and I'll go over exactly what it's going to do here in just a second but if we go and we add another area 2D we can name this area 2D our plate two because that's what it's going to be so plate two and just like for the last one we can add a color wct and this color wct is going to be equal to I guess we can go with pink right so it doesn't really matter any color will work for this example so we'll have pink and then we'll go to our plate 2D and we'll say Collision shape 2D and we want this to be equal to a square and then we can move this over here to our color rect and we can line it up somewhat decent right somewhere where it makes sense and basically we're going to go into we're going to minimize all this for now we're going to see that plate one is going to go into these signals which remember these are existing signals so we don't actually have a script so we want to go to example two world we want to create an example two World script and that's going to create a new script and then our plates you're going to see that we have body entered existing node signals and from plate one we'll say body entered and we'll send this to the world and plate two will send a body entered uh signal as well and body entered remember these are events that happen so if a body enters a basically anything that has like a character body 2D or something something like that enters the area well it's going to call this signal and in this signal it's going to give us a variable as you can see last time we haven't got a variable now we have a variable so this is going to basically be the body this is going to be whatever body that walks into the area and with this body well we can kind of print it out so let's say we walk into the area on plate one well if we say print body you're going to see what this is going to print this is going to print whatever walks into the area so up here you're going to see see it's going to print what it is it's a character body and that's an example two player that's the name of the node so we know exactly what enters it and if we go to our player you're going to see that we have a function player so if we go back to plate one I mean if we go back to example two script we can say if body because this is now a variable that is basically a variable that is equal to the the path to our player we can say if body and we can check if this body has a method and if this body has method player we know for a fact that it is the player right so if body. has method is player then we're going to want to change the uh the color so first we're going to want to have a variable for color and this color is going to be equal to that of plate one so VAR color is going to be equal to Plate one. color. color right so now we're going to get the color hex code and actually I want to go the other way cuz we don't really have to make sure that's the player I mean we could I it doesn't really matter we can do this as well and then we're going to have to go up here to the top and we're going to create a new node and this node up here is going to just be a variable and this variable is going to be background I said create new node I meant create a new variable and this is going to be an onready variable because we're going to be getting a a item from our scene tree we're going to say on ready VAR and this will be the background that we're going to be changing the color of background is going to equal to land. color wct and we're going to want to change this so so land. color rect we can come down here you can see that if the so if we enter basically this is going to get the the land this is going to get the background so we're can end up changing the color of it down here in this function but you can see so if we enter the area if the body that enters right because we're going to get the body from the signal if that body has the method player which our player does so basically the only thing that can enter or the only thing that can go past this if function is the player so if the body is equal to the player then we'll go in here and we'll set the variable color to equal the color of plate one and then we'll change the background which is the variable we just made so the background color will now equal the color that we want it to equal just like this and we can basically copy this and put this down here because these are going to be almost the exact same two except we want to change the link here to be plate two color so it's going to be pink right so the background. color is going to equal pink and if we play you're going to see that we can either walk on pink it's going to change the background or we can walk on Blue it's going to change the background based on whatever we walk on top of and I'm going to kind of explain this here one more time and you can see the player goes over that is a quick fix just like this but yeah so quickly basically how this is working right we have plate one and plate one has a event if a body enters the character body 2D is a body so if that enters well it's going to call this signal if the body that enters the area has method player which our player does then it's going to set the color VAR a local color variable to equal that of plate one color wct and if the plate one color wct equals do color then we're going to change the background do color equals to whatever color we just set which is whatever color that the plate that we stepped on is equal to right so I'm going to time-lapse and I'm going to create one more plate just real fast so we can go over and have three so it looks a little bit better and it looks a little more complex even though it's going to be the exact same code right so I'll be right back [Music] okay so now that we have a third plate if we go click play you can kind of see that we're going to have three different plates and all of them are going to work based on whatever color they have because of the signals that are being passed through as you saw we go we created and we win over but that is how you're going to create uh signals in an in-game example in an in-game environment that's kind of how signals can work but in example three I want to go over how enemies are going to attack the player because if we go to example three now and we click play you can kind of see that we have an enemy that follows the player but the enemy does not attack the player and the en the player actually has Health that is never taken away okay so not sure if I mentioned it yet but the what you unique about example three is going to be that we're going to pass a variable through a signal and how I want to go over this is I want to First enter into the uh the enemy scene right if we go to the enemy scene we can kind of see that we just have a Sprite and a collision shape and a script that is going to cause it to follow the player a very very very basic script but I want to go over how to create a little so first of all we're going to need it to attack the player and we're going to need to detect if that player is in the enemy's attack zone so we can say something like damage detection and damage detection is going to consist of just a collision shape 2D and this Collision shape not sure exactly where it's at oh we haven't created it yet but it's going to be a circle and it's going to be a little bit bigger than the Collision just like this and you're going to see that we have uh signals and the signal that we're going to want is a body entered so we're going to check just like in the last one we're going to check what body has entered the signal so if the body that has entered the signal has the method of player right well we're going to want to say whatever the variable that we want to pass through so we're going to emit the signal right which we haven't created yet but we'll we'll call this signal something like damage to player and so up here if we have damage player I want to go how how are we going to pass a variable through that signal that we just created right so signal let's create this the actual signal first so damage to player just like that so we have emit signal but we want we want to pass some damage we want to pass some signal through so we know what signal we're going to have because we're going to need some type of signal to pass through that is going to tell the player how much damage the enemy is trying to deal so if we go up here and we create a variable and we're going to name this variable damage we can set this to equal to whatever we want so 20 right so let's say I guess an ingame example would be if you have the enemy and it's attacking and it you want it to do 10 damage on this enemy or you want it to do 20 damage on the player then down here you can say if body do has method slime damage 20 right or damage 10 so it does different damage to different uh items in the game but emit signal damage to player and then the how we will pass that that damage variable through to our player script is very very easy all we have to do is a comma and and we can just type that uh variable out just like this just like damage just like that right so now it's going to pass that variable 20 through to whatever this signal goes to so just like in the last one the way that we can do this is we can go to our world we can say enemy we can say the uh node signal and damage the player you can see it creates a custom signal and we can connect this to the player script now in the player script we're going to have to go over here and we're going to have to say damage because so we have that variable so now that variable is going to pass through and when it passes through it's going to be equal to damage right so it's going to keep the same name because we're going to keep it the same name but we can rename this to absolutely anything right so we can rename this to uh well okay just just to make it easy we'll say Dam damage amount just so you know that we can rename it to anything it's going to be a different variable name so damage amount now inside of this function we're going to say take damage and then we're going to pass that damage variable through to a new function and this function is going to be created because we don't have it yet so we're going to get an air but we can say take damage and it's going to bring in damage amount and inside of this function we're going to want to basically change our health so we want our health to minus equal whatever uh damage amount is and then we're going to want to check if the player's dead or or not so we're going to say if health and I'll go over this here in just a second how it works but if Health if health is less than or equal to zero then we're going to want to set Health to make sure it's equal to zero so we're not under zero CU you don't want to be under and then we're going to set the just to keep it simple this is where you would call your death uh this is where you call your death function but we don't really have a death function so we'll just set physics process to false so we stop moving and then we'll emit a signal which is going to be to update the player's Health which is going to be inside of a UI right so we'll go over this here in just a second but for now I want to go up to the top create a signal this is going to be the signal to update our UI which we'll go and like I said make in just a minute but we'll say signal um update player Health right so that will be update our player Health UI and we can say emit signal update player health and then we want to pass through Health just like this right so we're going to have we're going to emit the signal update player health and we're going to pass through health so it's going to be kind of the same thing but let me explain how this works just real quick if we go to our player I mean we go to our enemy if a body enters the detection area well it's going to set the damage variable to whatever we want it to so we'll set it to 20 and then we're going to emit the signal damage to player that signal is connected to our player right so it's connected to this function here so it's going to call this function and we're going to bring in that damage amount and damage amount is going to be equal to Dam or the damage from the enemy scene is going to be equal to damage amount and then we're going to call another function take damage so we can tell our player to take damage and then that's going to be remember this is going to be our damage amount we're going to go into this take damage function we're going to pass through that variable again and we're going to say health is minus equal whatever damage amount is and then we're going to say if health is less than zero this is where you you would call your so you'll say health is going to be equal to zero just to make sure you don't have any bugs and to make sure it is equal to zero and then here is where you would call death function so this is where you would do all your death stuff after player death and then we'll emit signal to update the player's Health in the UI and you're going to want to be emitting this throughout the game so it's always updating so we'll put that into the physics process as well and we'll go over how to create UI in just a second but if we click play you're going to see well if we click play on the world you're going to see that our player freezes but now we're going to go through and we're going to create more signals for player UI see look we can no longer move because we hit the player four times but now we're going to go through and we're going to create signals for the UI so we can have updating UI and that's going to again be done with all signals okay so to create the UI now we'll go to our node 2D and we're going to add a control node and this control node can just be equal to UI for now and inside of here we're going to want to add a progress ress bar and this progress bar is going to be our players health bar so we can say something like the players the player health bar just like that and in the UI we're going to want to create a new script and this is going to be ui. GD for now and inside of this we're going to want to have a onready VAR and this onready VAR is just going to be for our health bar so we can say onready VAR and player or on ready VAR health bar just like this is going to be equal to that of the player health bar and we can save the scene and we can go to our health bar real quick and we can create it to actually look halfway decent so we can say something like whoops something like that that's our health bar we can come down here to visibility and we can make it into some sort of green which I guess that that's going to work nice right so that's that's our help health bar there and then in our health bar we can go to our um our player here and remember we created that signal in the last just a minute ago we'll say update player health bar we can send this signal to our UI script we can connect it and we can say we can pass through that Health variable right and then down here we can basically say that health bar do value is going to be equal to health so we can print Health just so we can see that it passes the correct variable through and then we can say something like player um or I don't I don't know I I was trying to copy the uh the name we had over here which is not the right thing to do right so you got to get the variable name that we created so health bar dot text or do value uh is equal to that of whatever health is equal to right so if we are to play everything should technically be working you can see up in the top it says 100 now it says 80 now it says 60 and every time we get hit it's going to go down boom and it's going to go down even again and now we can't move because the health is equal to zero but you can see it doesn't go under zero which is exactly what we want so that is going to be how you create a player with an enemy with an updating health bar just like that and I hope this tutorial was able to help you out and if it was then please drop a like on this video to so you know YouTube knows that this is a helpful tutorial that other in aspiring game developers will get some good out of and I if there was anything that you needed let me know then please let me know in the comments anything that you need in Goa let me know in the comments as well but I hope this you know was able to provide some type of value about signals to your go do Journey and make it a little bit easier than it would have been without this video but until next time I hope to see you again and stay safe have a wonderful wonderful rest of your day [Music] bye-bye
Info
Channel: DevWorm
Views: 12,568
Rating: undefined out of 5
Keywords: godot, godot engine, godot 4, godot tutorial, signal in godot, godot how to use signals, how do signals work in godot, godot 4 how to use signals, signals in godot, how to use signals correctly, tips for using signals in godot 4, everything there is to know about signals in godot 4, what to know about signals in godot, game development, how to godot 4, best way to use signals in godot, beginners guide to using signals in godot
Id: Q1w02E4H-as
Channel Id: undefined
Length: 24min 5sec (1445 seconds)
Published: Wed Jan 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.