Master Signals in the Godot Engine Fast! | Walkthrough | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right signals they're actually known in the computer science world as the observer design pattern now signals built into godot is very specific but it's still a riff off of the observer design pattern which is the idea that you have a subject which is the thing you care about and you have observers that are watching the thing that you care about and then when the subject changes something uh whether it's in the game design world picking up keys or doing events that you want them to watch they're listening to be aware of those changes uh let's take a more detailed example say you have in-game a radio and it's tuned to channel 21. listening on a transmitter that is also broadcasting on on signal 21. in this case you're trying to have potentially players or group listening for something whether it's the enemy or their allies or waiting for some kind of information to come through or you're trying to create clues in the game and you're changing channel and you're trying to pick up that that clue in this case in this case the subject is the transmitter and the radios are the observer now when it's transmitting those that are listening on that proper channel they're connected to that allows them to actually receive those events going through and then whether it's information clues or ways in which you want to progress your game you can have them listen but this way they're not tied together and directly tied to each other in a way that's creates a lot of dependencies and issues this way it's a very simple way of understanding signals and godot does this in a safe manner it's just a little bit more user-friendly whereas you normally have to code all this yourself now that we've gone through the theoretical let's actually apply some of this i have set up a scene with a bunch of stuff already added to it what we want to accomplish is we want to make this character run over and spring the trap uh then we want to go over pick up the key and have it unlock the door then we're gonna come over here and have the player attack and kill all these slimes and finally have that the slimes being done trigger this door to unlock and finally going over the game over to start off we'll run this guy and you'll notice that he could you can already move with wasd and mouse button to attack but here nothing's happening and we can't get past the door we want to look at this spike trap and we don't see this being available so we actually need to open the spike trap scene and i already have this area2d setup and so we want to do is say on area2d enter on body inert because we want it to register the player which is here says when a physics body 2d enters this area in this case our player being a kinematic 2d object is what we want so we will double click on this guy and say hey spike trap let's connect this is how we do the signal uh connection it's very simple you can double click do it from the ui just say on area 2d body entered connect make sure and then it looks for a script so you need to have a script already set up which i set it up on the main uh top level object click connect and now it's connected in here get rid of this and we can say print uh triggered the trap save that and now let's go ahead and run this come on over here notice down here that we have triggered the trap now that's all nice and good but i already have an animation set up here so all we have to do is replace this statement with animation which is already connected up here dot play the default and we'll run it again come on over here and bam look at that a lovely signal created congratulations you've just now mastered the basics of signals ah but let's move on to something a little more tricky let's try to pick up this key and have it open up the store well to do that let's jump over to the key dot tscn file open up the script and see that it's empty not only can you create signals from the ui here but if we want to do something custom that's not listed here you could do it in code very simply so this case like we were talking about earlier there's a broadcaster and a receiver the subject and observer so in this case the subject is the key and the thing we want to broadcast that whole line is is the signal of the event so it's very simply just type in signal on key pickup and if you save it you'll then notice if you go over and click on key now and look at node notice that hey key has on key pickup and so from this point you could double click and set it up you don't want to do that here because there's no references to player or the world so really we just want it just to shout out and say hey i've been i've been picked up and to do that we go right back to basics like we did with the with a trap we go back to this area 2d we say hey if if any person anybody comes by and picks up this key on body entered then i want to let everybody know so when anybody clicks this we'll do this just like we did with the trap and now we have on body entered when when he hits the key all we want to say now is transmit the statement that hey the key has been picked up and so we'll go and say unit signal on key pickup now we also want to get rid of the key because once you picked it up we don't want it to be there anymore and that's really easy easy we just go self dot q3 and that's it let's go run it all right now we run over here we'll trigger the trap for fun click on the key hey it's gone but nothing happened well that's because we've only completed one half of this we have the broadcast saying hey key's been picked up but nothing has been up set up to listen and say hey yes i know the key has been picked up i'm going to open the door let's go do that right now all we have to do is pop on over to our level so our level here and i have in this top level root a script uh most games have managers that kind of looking at the state of the game and figuring out hey did something happen you know did you kill the thing do i need to progress to the next level do i need to load something just basically watching what's happening in this case i want the level to know that the key was picked up to open the door now you can add additional things like wait for the player to be next the door but in this case we're just going to have it open immediately we're going to take this ready method and we're going to oh also quick note i'm actually linking to that non-existent key anymore uh i want it should know it exists and we should connect to listen to it before it goes away because once you do the self.q it's gone and you can't connect to it after the fact but we can still listen to the event so on ready we're gonna say hey that key that's if i spell it correctly that key that's over there let's connect to it and we know it is on key pickup uh i want the level to be the one to receive the message so self and then we're gonna make the method arm key picked up now it's red because this method doesn't yet exist so we're going to come right down here and say function on key clicked up and now this one should become happy and it did but this one's like hey now what well once it's picked up we want to unlock the door we come over here we have our reference to our door a door unlocked door oh yes uh remember to wrap the method in a string because it can be anything you enter it's trying to figure it out but it happens at runtime and cannot be compiled so if you uh add something like this it's not gonna tell you it's wrong because it doesn't know during until run time so always good to make sure that's like that and let's run it so we've got our character we're gonna trigger that trap we're gonna pick up the key and look at that the door is now unlocked now we're gonna run up here and now we've got some slimes but nothing's happening well how do we get past it well let's make it where we kill each of these slimes and then it triggers makes it it's a trigger that says oh while the slimes are dead let's open the screen this door as you can see i've already started a lot of this work for you i'm getting to see if those three three slimes exist very simply saying if the slimes are gone unlock the door so in this case the level really doesn't have to do anything other than it's continually checking in this process if they're a lot if that nodes are still there if they all three null hey that means you killed them all and that's the condition to get to the end but it doesn't need to know everything other than the state because in this case it's the player versus the slime so let's open up the player.tcn file and yes there's a lot of code in here but it's it's relatively simple um it's really just animation setup for attacking uh running around which i've covered in previous videos and that's about it there's nothing beyond that other than checking to see if there's an enemy to kill it which right now it's no nothing's happening what we want to do here is say hey i've got this attack zone that means anything that's in this field while i am attacking will die now this isn't perfect um you'd want it to be smart enough to know that if this enemy is behind him and he's not flipped that way that this wouldn't work you may even want to put the attack zone there's a child reanimator sprite but this is just simple stuff here we want to take the attack zone and like we did with the trap we just want to say okay i want to know when a slime body has entered and i'm going to connect it to my player script and then it's there and then again we want to know if it's a slime and not say like an ally like maybe of someone you're escorting or a good character or vendor or shopkeeper so instead we just say if the slime is in the body dot name then our enemy equals body now you could do this more complicated you can set it up to say maybe you have a type system in category and say hey if this body is of this type then it's an enemy and make sure that then says that that that's the one we have targeted because that that's all that variable is doing as being our target after that we just basically attacked and it will kill it right here well the safe thing to do is to also take into account when they are no longer in your uh zone so you can go back to this attack zone and when the body is exited reconnect as well and say hey they're no longer in my target zone i can't bring them in run away and do an attack and still kill them let me show you what this does if we don't add this all right so now i come over here right come over to this dude and i'm like oh cool and then i'm gonna run away and we're gonna attack no he died that makes no sense in a game so we got to make sure to account for when we leave the area we just come into here and say when we're no longer in the attack range the enemy is low so now they have to be within the range for it to work go ahead and try that again okay let's kill all these slimes yeah and of course you can make this much easier and have the collisions be more accurate and now notice because that level was taking into account to check when all three slimes were there or not it opened the door and now there's nothing here well how do we end the game just like we started it come on over here go to our level scroll down to game over look for game over body entered click on the level we come on over here to if layer in body dot name get free change scene to game over okay let's test this again and here we go congratulations i hope after this you now have a great understanding of signals and can integrate them into your game with better knowledge and specific goals in mind again if you could like and subscribe to appease the algorithm and so you can be aware of new content that would be great alright well thank you so much stay safe stay awesome you
Info
Channel: Hex Blit University
Views: 942
Rating: undefined out of 5
Keywords: observer, computer science, design patterns, game development, godot, godotengine, demo, tutorial, pixelart, godot engine, gamedev, game engine, how to create a game, godot tutorial, game development course, godot signals, godot tutorial gdscript, game development for beginners, game development process, godot signals tutorial, godot signals between scenes, godot tutorial 2021, godot engine tutorial, godot custom signals, game engines explained
Id: ZsJ-IRdC2eA
Channel Id: undefined
Length: 16min 27sec (987 seconds)
Published: Sun Jun 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.