Introduction to Signals in Godot (2019)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's clear me and today we're gonna talk about signals signals is good those implementation of the observable pattern and some of the use cases that you might see signals being used our UI achievements collision detections and other situations where you might want to notify someone that something has happened the demo that we are using here is made in Godot version 3.1 so if you want to play with and run with it you're gonna have to use this version of the engine let's begin by having a look at our demo we have uploaded we can control using our keyboard an HP bar and an enemy as soon as we get close to that enemy he's going to start followers and as soon as he gets in range he's going to attack us then when we get damaged our HP bar is gonna get updated and all of this is being done using signals to achieve this behavior we are using two signals one is emitted by our player to notify our UI element that our HP has been changed and the other signal that we have is when our player enters an area around our enemy that signals to him that he has encountered a player and it's now time for him to start following the player and try to attack it you better understand what signals are you can think of them as messages that are sent by an emitter and are received by an observer the observer may do whatever he wants with that information this means that different observers may do different things when a signal is emitted and the emitter the one that is sending the messages was not care who or what is listening to these signals and if there are listeners in the first place he's just sending these messages and whoever wants to react to them does it so in the case of our player even if we didn't have our UI element in the button the signal would be emitted nonetheless and this is really important to keep in mind because this leaves no coupling between the emitter and the observer to see what signals are available to you you can select any node in our scene in this case I'm going to select the player and to see the signals you're gonna go to the tab right next to the inspector here you're gonna find a list with all the signals that are going to be emitted by this node in the case of our player we also have a custom signal called health change it we are going to connect to our life bar to update it now let's open the enemies scene and here you notice that we have our heat box which is a collision shape but we also have as a child of our enemy an area 2d and this Rho D has a collision shape which is circular and we are using it to detect our player if we play our game now you'll notice that the behavior of our enemy is not working correctly so let's fix that by connecting the correct signals to it what we want to do here is connect our body enter signal from our air HDD to our enemy for it to react when this happens so as we saw before we can go to the signals tab and here we're gonna look for the body entered and we're gonna click on connect this video is going to open it and here we can select which node we want to connect this signal to rename the method if you want to we can call this whatever we want if we already have this function created inside of our script the dough is not going to create a new one it's going to identify that we already have this function and connect to it if not he's going to make this function and if we'd like to we can also add extra arguments to our function call with the root node of the scene selected in this case the enemy itself I'm gonna click on connect and the script editor is going to be open to us and here we can see the newly created function that Godot create to us when we connected this signal by connecting signals this way we get an icon that tells us that this node has a signal connected to something and on the node tab we can also see which function this signal is connected to if you like to you can also disconnect this signal we don't want that so let's reconnect it and as I said before this function has not been reiterated because we already had it inside of our script here we are going to check if our body is not a player and if that's the case we're going to return from this function but if it is a player but we're going to do is set our player to be equal to the body that we just found set our physics process to true now we can already play the game now when I get closer to the enemy you can see that he is starting to follow me and is going to attack me when he gets in range though our HP bar is not working anymore so let's fix that before doing so we're gonna take a look on another method of connecting signals which is by code we're gonna go to our actor detector and on our node tab we're going to disconnect our body enter signal from our function and now we're going to open the enemy script if you take a look on our first line of the script we have a reference to our RHD and with this we can go to our ready function and access our area and here we're going to call the function connect this is a function that you have to use to connect signals you can already see that Godot is giving to us options of signals that we can connect to in this case we are interested in the body entered the second argument is the object that we want to connect the signal to in this case we want to connect it to ourselves so we're going to pass off to it the third argument is the name of the function that we want to connect the signal to in this case it's going to be on actor detector body entered and if we'd like to we can also pass an array with extra arguments as we saw before on our editor that we were going to receive as arguments in our function and we could use them inside of it in this case we don't want to do it so I'm just going to remove this array and close the function now by playing our game you notice that the behavior of our enemy is to working as expected and lastly there's one more thing that we can do in the script to improve it as of now if I put a breakpoint in our function call and play our game you notice that this function keeps getting called even after we have detected the player if I go inside we reach that breakpoint because we found the player I'm gonna press f7 to keep playing now if I go far away from the enemy and enter it again this function is being called again but we have already found the player so there's no need to keep this C know connected to ourselves we're just spending resources that could be saved so let's stop the execution of our game and after setting the player variable and our physics process to true we're going to reach once again to our area and cow disconnect here we once again have to pass the name of the signal who we want to disconnect the signal from the name of the method to make our lives easier I'm just going to copy and paste the signature that we had on our connect function now if I press f5 you see that when we get close to the enemy for the first time we are going to reach this function but if I keep playing the game remove away from the enemy and back once again close to it this function is not going to be called again now let's fix the problem with our health bar by going to our game scene and here what we want to do is connect our custom signal which is health change it that we created inside of our players so you can see that by opening its script and on the top we can see that we have a signal called health change it that passes along with it a parameter which we are calling new value if we look at our exported health variable we have a setter method called set health and if we scroll down to it you can see that whenever we reach this function we're emitting the signal health changing with the current health this means that whatever connects to this signal is going to get the new value of our health when the signal gets emitted if we take a look on our life bar scene you notice that this thing is just at extra progress with an HP label attached to it if you open its script you'll notice that it's currently empty so let's connect the signal from our player by going back to our game scene relaxing our player on the note tab we're going to double click health change it here we don't have to change anything just like our life bar and click on connect as we're using type of GT script I'm going to set the return type of our function in this case is void and the type of the new value which in this case is int and here whenever this function gets called we are going to reach out to our value and set it to be equal to our new value now we can save our game and play it and whenever we get hit our life bar is going to get updated here we saw some use cases where you can use not only signals provided to you by Godot but also custom signals and how to connect them using both the actor and code you also learn how to disconnect signals although this is not really common but it can happen as you just show to you and hopefully you could see how powerful these two is and how you can apply it to your own games as always the project is available on github and you can find a link to it in the description of the video thank you so much for watching if you have any questions leave them in the comment section and I'll see you in the next one you
Info
Channel: GDQuest
Views: 35,978
Rating: undefined out of 5
Keywords: godot signals, godot signal connect, godot signal tutorial, godot observer, observer programming pattern, godot delegates, godot 3 tutorial, game development, godot engine, godot tutorial, open source, game creation tutorial, free software
Id: O5abE3aODxg
Channel Id: undefined
Length: 8min 7sec (487 seconds)
Published: Sat Jan 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.