[Godot] Four Ways to Use Signals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're just starting out with godot you're probably thinking the same thing i thought which was ugh signals it's just a godot thing i don't really want to just focus on just a godot thing but it makes your programming and go down a lot easier and i'm going to really just first of all just give you some examples where it made my life way easier from simple things like these buttons all these buttons just all done through signals and signals make it a lot easier to other things like in this 3d scene selecting and clicking on objects all done through signals signals make it very easy do something a little bit more complicated like this this hole being able to detect when objects enter it or exit it that's all through signals or my little delivery mechanism over here if you notice how it nicely doesn't crush things that are under each grate it's all through signals as well um so what i'm going to do first of all is give you a quick rundown on the concept really quick just the basic concept here signals are just going to help us at a specific event send some info okay so so um i know signals kind of sounds kind of broad but really when something happens send some information or when something happens call some functions and the functions might be somewhere else so as an example let's let's name this guy um let's try some cursive fred even though it should be a capital f here's fred and at some event let's say fred uh runs into a big hive of uh i don't know sharks hive shark a shark hive that makes sense when when that happens we might want to send information some signals to some subscribers and how do we get that there we connect them so we have if if he has a signal a pre-made one or a built-in one we can connect it to not this one other nodes that have scripts okay we can even send that information back to his own script or to other scripts meaning signal connections give us a opportunity to have one-to-one relationships or one too many relationships where you'd have one event sending info to mold to multiple things multiple other nodes with scripts fred might be the one we're concerned about but if these other little nodes had signals too of course this guy could send a signal to his own script and at the same time to this other guy's script but no one can help this guy no one can send him information without this one having a script not through signals anyway this this can get a little messy if you can imagine all these signals so uh ingoda is usually going to be all connected all the connections you're going to have are going to be maintained within a single hopefully smaller scene tree or a smaller scene so things don't get uh too spaghetti-like if you will first way i want to use signals is just to help you figure out what different nodes are for uh in the godot engine so if i just plop down a random node i'm going to find one that i've never used before uh hopefully something's scary how about proximity group i'm going to click on that i'm going to look i'm going to go from inspector to the node and i'm just going to look at the signals it has of course it has some basic ones that maybe every note has like if the script was changed then do something if if the note's ready do something blah blah blah blah if it's exited if it's entered the scene whatever over here at the top it has some node specific ones and this one only has one broadcast all right so this proximity group that makes sense it's called proximity group it broadcasts something to um to probably a group that makes sense let's plop down another one uh i'm gonna do what i've i use all the time a static body all right so let's look at see what the static body is even for i mean i know but you might not you know so here's some specific things the top oh this is useful input event oh so maybe if it's clicked or if the mouse goes over it something like that and look it gives me this information camera the the uh input mouse positions blah blah blah blah also mouse entered mouse exit okay that makes sense so i kind of know what this node's for let me try about how about just a button let's see what uh surprises you might have it's a button i'll look over here at the signals oh we have button down but not pressed so when these things happen i can connect this to something in a script so this not only does it tell you what a specific node might be for it gives you it also gives you hints on how to use it and trust me it's going to make your programming a lot uh easier the second thing look at our hair it's made out of flaps i didn't notice that before right the second way to use uh signals is through the they're built in ways uh and let's look at the just like we were just looking at this this girl she is a static body and naturally she has some built-in functionality and what i want to implement in this example is i want to be able to click on her and have her have her spin so um i've got her and she has a script i if i emphasize that before if we want to add some connect signals to herself then she needs a script um let's go into her scene though okay because she's a static body and she associates a collision mesh thing a nice capsule i put around her now let's try um let's see on input event we double click that and then what can we connect to uh oh we can't connect to anything because the scene does not contain any script so remember these things are going to be a little bit localized so if it was all in here on this scene sure let me see can i add a script sure this has a script i can add this uh script but connection and it will have a receiver method um which i can just let it use the default one or i usually like to cut out that the name or if you already have one in mind you want to connect to like maybe mine is just to uh toggle spin let's how about how this will toggle spin any time the mouse enters that shouldn't be too complicated mouse enters we will toggle spin now i can go back and see this little green thing here that's telling me there's been a connection hip hip hooray oh it got toggled whoopty do oh toggled again all right so that wasn't that complicated that was connected to the editor and of course the next thing we got to do is figure out how to connect these uh signals through code so we're going to disconnect this at this connecto and we can see then in our code that the little green thing is gone here in her script i've added an onput event and it's just a little script to see if uh if she has been left clicked and if she if she has we'll toggle this uh spinning not too complicated now through code the first way i think when will we connect this one option i guess would just be when uh when it's ready that's that's easy right so when it's ready we will connect and how do we connect stuff with signals the connect function of course what do we need first oh we need the name of that signal i already forgot it let's go look uh let's see input underscore event oh that's hard to remember but i can do it input underscore event what do we need next the target well that's who we're targeting who's getting the signal it'll just be most the time it's this can be yourself right because this is from her to her it could also be another uh another node any other node as long as you have a reference to it then we're going to say when that when that's fired what function do we want to call and of course yeah i want to call this one i've already prepared now are there any extra parameters i want to add nope are there any flags i need nope so most of the time this is going to do it for you let's give it a shot let's see if this works where's the mouse up click click what do you know was not too hard at all okay that wasn't true let's do this let's say um the user the player can only click on her and make her spin once and after that we'll sever the connection so first we can be nice and safe we can say we can say if if it's connected and then we still need to provide that information if this connection exists from the input event to myself that is connected to this function come here copy paste there you are then and look here we have a handy dandy disconnect function and oh that same bit of information so and if there's if that's not enough information for you can also remember control and then click you can read for example um maybe you can find out what those flags are for if you if you're running into trouble or what you can put into the array anything well let's give this a try all right here we go testing a click and then disconnect here we go boom sorry that was a little loud um click again oh it's been disconnected okay cool with the fourth way we can use signals we're going to get a little fancier we're going to make our own signals and what i want to implement is these lights turning on and off with her spinning being turned on and off so connect that basically we'll look at this uh well i've got some lights here right and i've added there are a little script give them a little class name and then uh function i've already thought about what i want to happen i want the i want them to know whether or not she's spinning if she is put that light energy up else turn it off basically make that light nothing so define for defining that if i can speak we'll first define it with the signal keyword and the name of the signal let's say is spinning and then we know that this will need one parameter and we'll call this spinning now when do we want to admit i think that's pretty clear uh when we toggle spin we'll we'll toggle this and tell everyone that's connected whether or not she's spinning so it is oh sorry uh emit signal is spinning then next i can give the variable i want to give and here it's just spinning and then i'll need to connect it i can do it through code oh let me get rid of this thing i want to be able to click it multiple times um we can do it through code and now this signal will be present in our inspector and we can see it up here and i can right click it connect to the omnilight do i want to just have a new function um no because i already prepared this function so i'm going to copy this function name get rid of this go back to the girl disconnect that try again disconnect to the light use my function name that i've already prepared and put it in there and then we'll see if this works click click oh interesting so the red light works but the green one is uh it doesn't let's check it out okay what was the problem there if we look at this script we see that a signal has been connected and if we look at the light uh the second light in its script we also see the signals being connected but actually we did not manually or programmatically connect them both we just connected the first one so let's get the name of the function i want to copy executed on the signal and then put that get that ready for when i make another connection connect to the second light give it the function i want to be called and oh and we can uh give it a try here we go look at that we are now signal masters um put some more emphasis on just figuring out the built-in signals uh you can get away with not making your own signals but it is good to know and fun so fun cool you're pretty much done i will show you however how to do that same thing uh what we just did we just connected uh her spinning to both of the lights let me show you a function how you could do that in code when we're ready uh i'm gonna i made a function called connect to the lights and how is it defined we just get the parent go through each of its children if it is my light how do we know what is that my light well i gave these lights a class named my light that's how it knows if it's a light uh if it is a my light then connect and what are we connecting this signal that we've already defined in this in this uh script the the target which you know is this this child in this uh as we go through all the children and then we're connecting it to its function spin toggled so when this happens this function will be called with that information what what information is it spinning or not boom done look how close i get my mouth right to the mic i have a few more seconds so i'm going to give you some other resources some references uh the first your best friend is probably the go.docs just read through it i mean it helps helps a lot um the gd quests they did a good job but you have to follow along in a project you have to download and i'm i'm not really about that even though they're really clear about everything and then these two other videos are nice especially if you kind of like have a more if you want a more professor kind of guy talking down to you i mean uh talking to you uh so those are good resources too but most of all just experiment around with it and you know you know the usual don't like don't subscribe dumb what a dumb thing to say
Info
Channel: Stactory
Views: 833
Rating: undefined out of 5
Keywords:
Id: qcXvV7YaAjE
Channel Id: undefined
Length: 14min 51sec (891 seconds)
Published: Tue Nov 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.