[Godot] Four Ways to Share Data Between Nodes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you're learning godot one of the first things you got to figure out is how to get your nodes to be able to talk to each other share the information like health or points or whatever are their names like in this in this little example i've got i've got this little belt over here how is he going to communicate with his friends that's what we're going to look at i'm going to give you four different ways three different ways on how to go about solving this little problem the first way but probably not the most robust way is to make good use of the scene tree hierarchy right so if i have some uh what are we let's say they're students and i want to be able to have them communicate with a parent node so we just use the scene trade we use it carefully so we have our girls and we have them parented to the teacher now this teacher is just a a spatial note but i've given them scripts i've given this a teacher script for the teacher and i've given the girls student scripts and i've given them a class name student just to make it a little easier to test what it is having the two girls under the teacher is going to let me be able to let them talk through the get parent and get children functions like get parent or for the teacher get children now so let's look at what we've tried to do here we try to implement a role call function just to get all the names of the students that are here so we can get all the children with a get children function which will be girl and girl 2. what will we do with that oh we'll see oh we'll be safe we're going to be safe we're going to say hey do you have the method get name if you do well just give me that name from thatchild.getname function we can also change this to just ask if this is a is it a student if child is student and we can do that because we've given these girls a student class name so that's if if you want to be specific for students you can do that or maybe things in this in your scene besides students also have the get name function so you can just leave it the way it was so here for each for each one we'll get the name and we'll put it in an array and then at the end just print it out right and we'll do that on ready so it should activate as soon as we run this let's see what we get yep we found the students linda and sherry where did they come from well it had the get name function right so in here it had to get name and what did it do it returned its own name and this was this was saved in export variable so i can duplicate these and it's very easy for me to give them a custom name over here this one will be this one will be bob and we'll just move bob behind them so in that example we went from the top down from the teacher down to its children going the other way is also possible we can we can implement a find a friend function and in the ready here this this girl will try to find her friend sherry so let's look at that that function what we did here we got a name friend's name and how we could do this we could get tree get root find no teacher ugh yuck it works uh but we don't like that that's gross let's just keep it simple go up a level to the parent which we know the parent of all these girls will be the teacher so we'll know that that we know that'll be the teacher we'll declare our available friend and then we'll see if the parent has the method find student we'll go ahead and use that and if that friend was found let's say i got my friend i found my friend so that means the this function needs to be with the teacher right this is the parents function let's look so here it is with the parent the teacher find student we give it a name and just for each of the children if the child has get name if the student's name is that name then we'll return that child and this will be a reference to that note so let's hit f5 and run that and see what we get printed out and sure enough three students all looked for their friend sherry and all of them said i found my friend sherry even though yes one of them was sherry herself which one which one was it there it is that's sherry goodbye sherry you're done nah just kidding so that's the first way you pay attention to the scene uh century and have them interact through their uh position in that tree the second way we can have our nodes talk to each other is with a global script so we'll plot down a new script i always call it globals but you can call it not globals and we're going to make sure that everything in this script is accessible anywhere in any code and the way to do that is go to project project settings and in this auto load tab we can choose our global script and then add it in here now if you want to access any functions or variables in there for example in our teacher here we can just write globals which is the name of that script dot teacher and here what i've done for our globals is just say have the teacher be globally accessible anyone can find this teacher because it's global it's global now but i need to have it set when this teacher is ready so when this teacher is ready he will say hey globals i am the teacher me so then later we can use this and i'm going to put an input event here when i press a button i'll have our teacher if the teacher is ready to call roll call take roll and we'll see if that works and he took a roll oh but i forgot to clear the contents of that array so they're just adding those three students each time i take roll but that's okay it's a simple bug now if you like trickle down economics you're going to love the third way which i call trickle-down programming so i'm having input events like for this little machine i'll switch the gear and i'm going to trickle down the order switch gear or the function switch gear i'm going to start at the globals and then i'm going to trickle down to the floor which holds all the nodes and then to this specific component which has as you can might be able to tell there's more than one thing here that needs to know if the gear is getting switched but let's look at an example of that with our girls i want to be very organized and have my function trickle down from the top to the bottom to share the information the way i wanted to so i'm starting at globals let's look at globals and i'm going to give them a quiz that's what i'm going to try to do i'm going to have a quiz and we're going to start at the top cons having globals considered to be at the top and going down to the next level which will be the teacher so we'll make sure the teacher has that method and get the results from him giving the quiz and i'll just print out the quiz results so the next layer down as we trickle should be the teacher and he should have his own give quiz function and this we can define this function as we'd imagine a teacher giving a quiz counting how many students take it adding up their results and of course making sure they have the method calling it adding up the results and at last returning the average of their uh scores so at the very bottom should be the girls right let's see their give quiz function they have it too it's defined as i don't know a very stupid way to take a quiz just get a random score that will get returned up to the teacher which will be averaged out and returned up trickled back up to your globals uh so i'll hit f5 and then i'll hit the button a few times to take a quiz and we can see their results are as you expect all right you're done you did it the uh those those three ways should should be enough to help you figure out how to implement some information sharing in your own game uh but hey i will actually give you a fourth way and this is the dangerous and sexy way if you look here i have three computers and they are unlike the previous example where i could use a trickle down hierarchy to send information from the top to the bottom these these computers you know i might not know where they are in the scene tree but as you can tell they all are getting updated at the same time with with some information and we're doing this with a subscription plan just like subscribing to a magazine when a new magazine comes out they will all get updated or in this instance if the fps changes or if the number of objects in the game changes they'll get update and with that update of course they'll be able to change their display and this will be a one to many or a one publisher to many subscribers relationship which can be very helpful as well this is a little bit more complicated than the other methods so bear with me we here we have some global variables and this is going to be in our globals script globally accessible and we'll have our variables our global variables that you might need throughout your game for me i want to know fbs how many objects are in there maybe if the levels passed or maybe something from playing sounds so i'll have this in a dictionary with their with the name of the global variable and the value itself and then i'm also going to have a dictionary of subscribers and i'll store in here nodes or references to notes and what subscription they want to subscribe to so maybe i have the computer and it wants to subscribe to the object count or maybe i have i don't know a belt and it wants to subscribe to um i don't know some sounds or something so i have these three and i also have a service busy because uh changing arrays in multiple places or changing dictionaries in multiple places at the same time can cause crashes which are bad so we're going to be a little bit careful now let's see the functions for this bear with me here we go so of course you might just want to increment one of them but but since they're in a dictionary you'll need the name of it the name of that global variable what does updating it entail because it's going to be a little bit more complicated than just saving it with a subscription plan here in update of course we have the name and the value um we we first we can just set it we can say hey this this this global variable at that name now equals this this value and now we're going to go through our subscribers as long as our our phone line isn't busy and send it that update but of course for it to get an update from the subscription it would have had to subscribe in the first place so when we subscribe we say hey i'm i'm the computer i'm at this node and i want to subscribe to this string and we we add it to the list of nodes that want updates about that variable if we already have that variable in our dictionary then we just add another subscriber so it's an array of nodes that all want information about this subscription update which is why we're actually getting a raise here when we update it we get the array of people interested for each of them we send them that update with the variable name and value okay and lastly sometimes we might want to up unsubscribe a node might be deleted and we might need to tell the subscription service it's no longer interested so what we will do is we'll find that node and erase it now for the computer or the nodes that are subscribed to such a service let's look at one of those here's the here's that status bar thing and it will in its ready function subscribe it will say hey i want to subscribe hey globals which has that function i'm subscribing self and i'm subscribing to this variable so i want to get updates about this variable then we'll need a take update function which is what we are calling from globals here every time something has been updated we tell all the subscribers to take the update so they'll need to have this method we'll look at the name of the variable and it might be object count if and if it is object count well we know then we'll know what to do with the value in this instance we'll take the value make sure it's a string and put it in the text so that we can see it updated in the object value a little text node here and one more thing before we delete such a node in our game we might need to make sure it's okay to delete so if the service isn't busy if the line isn't busy we'll make sure to tell globals our subscription service to unsubscribe myself all right we're done good job you made it when uh when i was helping my friend learn a bit of godot uh his biggest one of his biggest problems was figuring out how to just get nodes to talk so hopefully hopefully some of these these methods help will help you a little bit um but i'm not a youtuber so don't like don't subscribe get out of here go make something awesome that's what you should do youtube
Info
Channel: Stactory
Views: 2,680
Rating: undefined out of 5
Keywords:
Id: BSxskl0XmdQ
Channel Id: undefined
Length: 14min 26sec (866 seconds)
Published: Fri Oct 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.