How to Use Godot's Signals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will learn to use godo's signals as we saw in a previous lesson this feature allows nodes to communicate with one another for example i can click a button to toggle the character's motion we'll connect this signal using the editor's interface which is the first method we can use to connect signals then we'll use another approach through code they are complementary and you need to learn both this will allow us to make the character blink like you can see on the screen finally we'll dive into another scene that i've prepared for you where we have a character hitting another and we want to update the live bar to do so we'll define a custom signal and emit it via code this video is part of a complete free series to get started with godot in 2021 to watch it from the start you can find a link in the description there are two more complete step-by-step game creation tutorials coming in the series so to not miss anything be sure to subscribe and turn on the notification bell with that let's get started to follow along you will need to download a starter project i put a link to it in the description below you have to click on using signals.zip to download the archive then you head to the folder where you downloaded it i'm going to right click and extract the archive here something i can do to unzip it we then want to import this project in godot so i'm going to press ctrl l here to get the path to the directory move to a new workspace and open goto you then want to click the import button paste the project path and import and edit it all right so let me run you through it very quickly custom signals is the scene with the light bar player and enemy i showed you we'll use it towards the end of this video toggle motion contains the rotating good of the head that we created in the previous tutorial where you created your first script we'll use it to connect our signals we're going to start with that so we're going to create a new scene by going to scene new scene and it's going to be a 2d scene i'll name it toggle motion and save it on my drive here as togglemotion.tscn in this scene we're going to create an instance of our good head turning good at head you can click and drag it anywhere onto the scene it gets that it's a bit far down so we're going to move it back to the center here a bit to the left because it's doing circles and from there we're going to select toggle motion and add a button node so i'll click the add button look for the button node and press enter it's a bit difficult to see a little thing gets added in the top left of the scene so with the button selected and the select tool on you can click and drag on the bottom right little icon here to expand it you can then click and drag on the button to move it away from the top left corner of the viewport in the inspector you can see it has a text property so we'll set it to toggle motion that's our scene setup to toggle the character's motion so currently when i click the button nothing happens the character keeps turning we want to tell the good head when we click the button to toggle its motion on and off to do so we use a signal and the button comes with plenty of them select the button node and on the right side next to the inspector there's a node tab which is split into two signals and groups so we're going to stay on signals to see the list of signals available to the node it's pretty long because every node as it inherits from parent classes object node canvas item etc also inherits all the properties and signals of those parent classes each of them is an event that will get emitted every time something specific happens for example if we look at the base button around the top you have the signals button down button up pressed toggle press gets emitted whenever you click the button or with the button highlighted in blue like that you press enter for example and so we can connect this signal to our good head to make it react to it so with the button selected you're going to either double click pressed or select the press signal and click the connect button then you get this menu that helps you by telling you which nodes have a script attached to them you'll see here you are unable to select nodes other than godot because we need a receiver method or function that's going to be called by the engine when the signal is emitted by default it will have this convention underscore on underscore name of the node the button underscore name of the signal this helps you know which node emitted the signal and which signal it was now you can click connect which opens the script editor with the new function and the scan button pressed note that you have a bit of code that comes from the previous lesson which is why while not re-explaining it now this function is empty it only has the pass keyword that just tells do nothing to the gdscript compiler we're going to replace that to toggle the character's motion because this is what we want to do upon pressing the button to do so our nodes have a function to turn the callback to process on and off it's called set underscore process so you can erase this line and call set underscore process it takes a value either true or false bull means boolean which is either true or false so if you say true it's going to turn on processing you say false it turns it off you can enter false and press f6 to play the current scene when you click the button the character stops when you click it again it doesn't start because we're always setting processing to false we can use an expression to toggle it we have another function which tells us whether or not the node is currently processing it's called is underscore processing so we're going to call it has parentheses because it's a function that's going to return true or false now we can negate that so if it is processing we turn it off if it is not we turn it on by using the not keyword with a space before the function call so we set process to the opposite of the current processing state and when you do that you can click the button to toggle the motion on and off i'll just mention that set process is safe to call it just turns a flag boolean value on and off on the node that good looks up to decide whether or not it's going to call this process function okay so that was connecting a signal using the editor now we're going to do that but using code and to do so we're going to add a new node as a child of our godot instance so let's create a new timer node this is a timer that has a waiting time and that cycles every one second in this case and if you look at its node tab it has a signal named timeout so it emits a signal whenever it times out by default every one second of course we could connect it via the editor but we're going to do it in the script so click the script icon next to the good bot and we're going to connect to the notes timeout signal will make the character blink okay so first we need a place to connect it and we're going to introduce a new function called ready so write the funky word underscore ready this one gets called by godot whenever you start running the scene i'm not going to give you all the details but it allows you to initialize the nodes to set them up like connecting a signal by a code okay then we have two steps that we need to follow first we need to get a reference to our timer in the code so we need to tell the script get access to the timer node okay so to do so i'm going to store the node timer in a variable so write var timer i'm going to set it equal to there's a function called get underscore node to get access to a node and it takes a path relative to the current node so dot means the good at node because the good at node has the script attached to it and timer stands for the timer node then we are going to call the connect method on the timer node this is a method you have on every object in godot that you can use to connect a signal we need to call it on the timer so we write timer dot to access the timers functions connect and you can press enter the autocompletion gives you a list of all the signals on the timer node we want to connect to the timeout signal then we have to give it a target to connect to so when we were using the interface we could double click the good at node now we have to use code to do that we're going to add a comma and we're going to write self self stands for this node the script is attached to in this case it's our good node then we add another comma and we need to give it a function to call again you remember on the interface we had the unbutton pressed method called for us here we're going to create a new one so add some quotes and we're going to name it following the same convention underscore on underscore timer underscore timeout which is the name of the signal and you can copy that name because we need to create the function now and at the bottom of the script add a new function with a fun keyword on timer timeout parentheses and the column at the end and here we're going to do something similar to what we did here we're going to toggle the character's visibility so we can say visible equals not visible this is how you toggle a boolean value if the character is visible it's going to become invisible and vice versa now we have one last step to do it's that our timer does not start by itself unless you tell it to so select the timer node and check the auto start checkbox so it starts and the time runs you can now press f6 to play the game and you will see the character blink it's a bit slow because the wait time is one second so we can lower it to 0.4 to see the thing happen a bit faster here is one last tip it's that when you call the get node function in the code we pass the name timer now i want to stress that this is the name of the node in the scene tree so you can double click any node to rename it for example if i call it visibility timer i have to update my call to get node to say visibility timer and you can see that the autocompletion changed when i was writing the name in case you forget to do that and you run the game you will get an error it's going to say something like uh attempt to call a function or to to get a property and base null instance on a null instance this typically means that you are trying to access something that is incorrect right when you call getnodetimer there's no node named exactly like that timer right now it's named visibilitytimer so you don't get anything and the value for nothing in code is that null n u double l and so if i set the name to visibility timer and i play the game again now it works now we can move on to the custom signal scene where we'll create a signal because you have plenty defined by default but the real power lies in the fact that you can create your own and trust me you will create plenty of them and you're good at games so now we want to open the custom signal scene i prepared for you this scene contains three scene instances the enemy that's a character that's uh moving like that and hitting the player the player takes some damage and we have a health bar to represent it but you can see that currently the health is not going down so we want to create a signal to do that because if you select the player you can see it already has a signal connected it's to take damage to play that little red animation but if you look at the list of signals there's nothing that says like took damage or the health went down and so we want to have a signal to say just that which the live bar can listen to to go down to do so we're going to click on the player script so you can see it has a bit of code already it has a health value that starts at 10. every time something touches it so on area entered means something touched this node which is an area2d which detects when something touches it we're going to call a function named take damage and inflict two damage the function is right above it so take damage takes an amount a number it's going to subtract that amount to the health variable then if the health goes lower than zero because numbers can go negative we ensure to set it at a minimum of zero and we get the animation player node in the player scene you can't see it here and play to take damage animation now we're going to add a signal that will emit whenever the character takes damage to do so around the top of the script you're going to write the signal keyword followed by the signal's name i'm going to call it health changed there we defined a new signal and we can emit values along with the signal something we haven't seen yet and here we want to emit either the amount of damage that the character took or we're going to write an argument in parentheses called new health we can then emit the signal by calling a function named emit signal so at the bottom of the take damage function we're going to call emit underscore signal the signal may not appear until you save the script so be sure to do so and then we're going to scroll to go find health changed okay so we're going to emit health change and then you can add a comma to emit the value the new health we are going to emit the health variables value because we subtract it then we can emit the new value once you did that you can select the player node and in the note tab you're going to see the health change signal appears under the player.gd category we can now double click it to connect it to our live bar we're going to click connect here which is going to create a new function on the light bar you can see that our new health argument gets passed to it this is just the name for the new health values it might be 10 8 6 etc and there in the life bar this bar which is something built in has a property named value which you can change to lower the bar so we can say value equals new health and when we play the game with f6 now the live bar should update so i'll go down every time the character takes health and that is it for this video now at this point you might be wondering what the signals are for because you have other ways to make node communicates in code the thing is this is a very basic scene in godot your game might have thousands of nodes and so signals will really help you keep your code separate to separate concerns to have the player do something specific the light bar do something specific receive some information from the signal but not directly access the player's code which helps your code stay bug free and make it scalable it's a really powerful feature you'll use it all the time and we'll see that in next week's video where we'll create a complete 2d game from scratch made by chris at kit scan code for the official good documentation if you like this video be sure to subscribe there's going to be more but also we made a complete free email series which you can get right now on our website the links in the description below it will teach you extra insights to get started with game development and godot again completely free you can unsubscribe anytime and with that we'll see one another in the next one bye you
Info
Channel: GDQuest
Views: 32,313
Rating: undefined out of 5
Keywords: godot signals, godot beginner tutorial, godot using signals, godot signal connection, godot engine, game development for beginners, godot tutorial, godot signals tutorial, godot signal tutorial
Id: NK_SYVO7lMA
Channel Id: undefined
Length: 17min 46sec (1066 seconds)
Published: Thu Apr 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.