Nodes, Scenes and Trees -- Godot 3 Tutorial Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody it's Mike here came from scratch welcome back to our ongoing to do 3 tutorial series today we're gonna be talking about something this should be a fairly brief subject some kind of dull sounding if I'm honest but in the end it's very very important to understanding that you do a three and that is nodes but we're also gonna be talking about scenes and trees but notes is the biggie and if you come from another game engine if you come from a component based engine such as cry engine or unity or even the sort of Unreal Engine this is the analog to your component system but more important than that as an ode is sort of the universal building block of Godot in its a unity for example you have your game object which is composed of various different components in unreal for example you may have a actor that has been composed of various different components in good--do you have nodes and you have hierarchies of nodes and that hierarchy of nodes can be saved as a scene and at the root of the scene controlling which scene is opened is the tree so don't worry we will jump in and demonstrate all of that it's actually a very very straightforward process so here is an example from the ongoing book that I'm developing which by the way time for a plug I'm also doing a book that I'm doing as I'm doing this series I'm making preview chapters available for patron backers so if you want to learn more head on over to patreon there are five or six chapters so far hopefully we will keep coming keep them coming at a brisk a rate than they have been but as you can see here this is pretty much what the default scene looks like in the world of Godot at the root you have your scene tree the scene tree in turn has a scene the scene itself is composed of a hierarchy of nodes at the root of every scene there is a single node this is always true we'll get will show this in practice in a second but here's like an exact example of using real world link so the root you may have a scene tree and then your your level or scene may be called level one and then in that you may have the player powerup enemies etc at the next level down in the hierarchy and then below that you have say your player instead of having components it just has other nodes those can do the same role as components so it could have a sprite or an animation etc now one important thing to understand is by default these guys all in here at the positioning information of their parents so if you've rotate or translate the player the player sprite attached to it or the animation attached to it will automatically transform as well so that is the basic higher key notice actually take a look at it in practice in Godot now you have to warn an upfront there is a bit of a chicken and egg problem I have doing this tutorial series in order to really get Godot you need to understand nodes but in order to demonstrate nodes you kind of have to understand programming but in order to be able to program you need to have the ability to create a node to attach it so there's a bit of a chicken and egg snare over here so what I'm going to be doing here it's going with notes first and the next chapter or hopefully will be on programming so then you gotta be able to come back to this chapter and make a bit more sense right so we're gonna get into a little bit of coding that may not immediately make sense to you but one of them had to come first so I chose nodes okay so if you do run into a bit of a gotcha on the coding site don't worry I'm going to explain that in a lot more detail very very soon so this is your default level opened up I'm in the 2d editor as you can see right here I already walked through this in the getting started tutorial so if you need some familiarity of how to navigate around the the game engine please do refer back to that but we were specifically interested in right now it's creating a node just come up here over to the scenes category and click plus now this is your ultimate node list this is all the built-in stuff in Godot and the cool thing is there is a lot of it we're gonna be covering these in depth as we get to them and the relevancy but you'll notice at the base there is no this is the base class everything below is inherited from the class of bus so for example spatial is inherited from node canvas item is inherited from node node 2d is inherited from canvas item etc so node is the root level of all of the nodes so all of the functionality in node is available to everything below it this is your typical object-oriented heritage style approach the things the two that you're gonna probably be most relevant to is the hierarchy of nodes under spatial spatial couldn't probably be more clearly called 3d this is where all your 3d scene stuff is and what we're dealing with most of the time in the short term is no 2d and this is where all your predictably 2d stuff is so you see you've got a lot of things down here you've got lights you got sprites you got two deepali gods rake I think tile maps etc those in turn again are all inherited from the no debuffs so animated sprite is a node 2d back buffer coffee is a no 2d which in turn is a campus item which in turn is a node so all of the functionality of the parent classes is available from all the drive nodes sorry I might switch the words class and note together my apologies so be aware of those hierarchies but what I want to start with is the route class of our scene I generally make it a note now as I was mentioning earlier there can only be one route so if I create another class right now like this you'll see it will automatically be parented and I have no ability to change that parenting so there is always a root level note by the way to get rid of a node simply highlight it and hit delete but that is how you can go ahead and create notes now the cool thing is from that point on once that route is established anything below it can easily be reparent it by drag and drop right-click repairing etc but you always have one node that is the very very root level aspect of your entire hierarchy so there we go there is a single node here now I'm going to show you kind of how it looks behind the scenes so as I mentioned the scene at the root of the scene is the tree the tree holds this parent node here the tree is not something you'll deal with two too much he's mostly used for switching between scenes which I will show very very briefly in this example but the scene itself is very very simple so we've got our node defined here and now we're gonna go ahead and save our scene so you have to have at least one node defined before you can save a scene or you're going to need an error message here I would call this my scene like so and you'll notice here you've got two options TSC N and SC n once you've got three and res you're going to choose between T Sen it's Sdn but you're always gonna choose t SC n for the most part no t SC n is basically just a text version of the scene format it's human readable etc T's of one of the SCN is a binary format of it it's more efficient but here's the thing when you publish your game your TS en file is automatic you converted to an SDN file so you're pretty much always best served saving as a tea SCN and then there is our scene it is now saved you see down here in our file system it showed up now as a scene file and we're going to go ahead and show it in our file explorer so you see here right there tspn file very small in size I'm just gonna open that up so you can see just how straightforward and simple this is open that guy up with more apps Visual Studio code where are you there you go we use Visual Studio code so here is the TSE n format very very simple straightforward it it's almost like somewhere between XML and oh it's the other one yeah Mille or even Jason to a degree it's very very simple basically the scene is defined and then inside of it you can see that node we created its name is here this type is here and it's index or you know where it is in the tree of nodes is stored there and that's it that is how straightforward a scene is so a scene really is just a collection of notes so if I go ahead and say well add a child to it so here we go I'll rename this guy call it root and then out on top of root so we're through it selected I am now creating another node and I will call this grand parent and this will make sense in time so root now has a child called grandparent now I'm gonna with grandparent selected I then go ahead and create one more node called parent oh sorry I'm I'm naming at the wrong spot so pick node type right here we'll call this parent and you'll see parent is a child of grandparent so if I move grandparent now nodes don't have any positional information but if it did if it was a node to D or a spatial or inherited nodes from that if I moved around grandparent parent would automatically follow it now if I want to re parent nodes you can actually either right-click and reparent or move up move down or we can literally take it here and now I am parented to the root and then Here I am back parent the grandparents so repairing note it's very very simple but it does matter what order they are actually in so you know the the inheritance you want parent to be a child of grandparent in this particular case and not a child of root and I will show you why when we get into coding in a second and then I'm going to create two more we will call two more nodes that's the one is a child one and then with parents selected again go ahead and create one more node and we will call the oops child two now another thing that's important is their order so we get into coding later on this guy will be at the zero earth position this guy will be at the first position if you want it to be in a different order order here very much matters now you can do some of this but up by drag-and-drop which is a little finicky but if I get right below that guy hey hey hey come on you can do it yeah I can reorder it that way or as you saw we can right click and move them up or down and again that is important for determining things from code if you do things positionally this will be relevant so now let's get on a little bit - oh here actually before we go so far I will save this scene like so so there our scene is saved we head on back over to our Explorer and let's open that guy up again and now you'll see the hierarchy is again very very very straightforward but you could see it's just a list of notes again but now they have parents to find so this dot I will explain in a second but you can see here this guy's parent is this guy and then it's using link style path to describe the hierarchy of nodes and again I will show you exactly how this works but you see the scene format is very simple very straightforward alright so that's it for scenes we're not going to go back to that anymore other than maybe to show you how to switch scenes now one important thing with seams though is you need to have at least one defined in order to run your game and I'll go ahead and run it the first time and you'll get an error basically saying you haven't set a scene up to run when you your game and this you just go ahead and say select and then choose your scene it's gonna run is it's not actually going to do anything because well frankly our game doesn't do anything but that is a required process and if you have multiple scenes in your game and you want to choose which one is run we can do that in the project settings go to project settings right here go to run and then it says main scene and you can choose it this way so you go file and then we can just select the scenes if we had multiple scenes we could switch between them that way so that is basically creating your first scene in Godot how hierarchies work now let's get down a little bit to coding I'm going to start off with attaching a script to child 1 this will enable us to show basically the hierarchy how nodes interact with the tree and other nodes their parents grandparents siblings etc so we're gonna go ahead and attach the script to this guy now again this is me kind of jumping forward in time a little bit I will show you more about scripting later on so I'm going to kind of skip a little bit through that process but basically we're gonna head attached a script I'm fine with the default name so of type Gd script here we go so this is a straightforward script again I'm going to get into the process of using the editor scripting etc later on so I'm glossing over that a little bit but I want to show you how you can handle various different relationships so first off I'm gonna show you entirely with prints so this is it so what you're supposed to start anyways this command is very simple basically just prints out whatever name or spring value you pass into it so in this case I'm passing it the string hello world so we run our code you're gonna see down here in the output hello world okay so we're gonna make heavy use of print in this particular example so first off I'm going to show you how you can get your parent so get parent is a function built-in to the class node if you ever want to know the functionality in node you can grab them for you here bring up the node so let's type node like so and here so you can see there are a no of different functions here for doing different things so you see get child etc get groups note that's what we're gonna be dealing with today now let me head on back over to where I want so this will get our parent and every node has a property called name and we're just gonna print that out so you can see who we're dealing with here so basically here we're saying get our parent and print its name so what that should basically do is say what's the note that owns me and what does it call it and it will print out the word parent run that and you will see parent right that's exactly what we wanted to see so that is how a node could get the the node above it and you can nest this so basically I can do get parents again on I can't type apparently I need to get parent again and this will now give me the grandparent and root and you can do this add basically until you run other nodes that have parents and what you do it will return a null node if it doesn't exist so that is definitely one way of getting your parent now another way is if you're used to URL paths or Linux command line Mac command line in to a certain degree das command line you'll understand this as well so we're gonna do is say get no don't get node is a function that is used a lot we're gonna come back to there's a shortcut for dealing with get node but getting node is your number way of getting notes so you can pass in another number of different options here you can pass in the name of the node since we're a root level no those sort of the farthest out leaf you can be we have no children so there's nothing we can pass now we've got the option of dot and if you've ever used a command line terminal you know dot refers to the current location exactly the same thing in and the Godot roll so this would basically print out child one nothing special going on basically that's the way of saying get me and there are some reasons to do this but you're not gonna do it a whole lot but what you couldn't do is especially if you don't know the name of your parent you can get your parent using get parent or you can use dot dot so what this will do is basically say give me the node above me so if I print that you'll see all of a sudden it will print out an error because I didn't close my code all right let's do that one more time it will print out another error because I didn't put the property I'll by the way if you print out a name you will see it just basically prints out the what's type is and it's ID unique ID so what we want to do here is dot name like so and finally we go ahead and print that and we will see parent okay so you get how that's working basically we're saying one directorate so one dot is me dot dot is above me and you can keep doing this till the cows come home so if i want just like pathing in linux i can do up one more directory and you will see this will return grandparent and you can again keep doing this as much as you want and then you can also go back down so if i had other children nodes so if grandparent had other children we can now put other node name there and then start going back down the tree so it's basically a way of setting a full path going on here now another one that you're often you'll want to do is go from the top so your parent level node remember i said there's always at least one note at the very top of the entire hierarchy and the one thing that owns it is the tree and the tree is used now we're not going to use tree a whole lot but you're basically the most common thing you're going to use for get tree is to get the parent node so there's a function in get tree called get root like so so this basically is giving us this guy here now it's coincidental it's called root that's a little bit confusing so if i call this guy my room it's so it's got nothing to do root just happens to be the top-level node in the entire hierarchy it's like so and I can go ahead print out that guy's name so put that guy and it should print out root why'd you print out a root oh yes this is actually my mistake so actually the root node is always called root so this is actually a child of root so we still have to do a get note and we include the root two snowed in there it's like so I close that off now we print that get node in base viewport good tree get root hid node sigh hi I'm an idiot today alright again Gd script is case-sensitive and it helps to use the right function name alright so let's go ahead and print that guy out and there is haha I'm an idiot today I forgot to put the dot name there again but you see when I run this guy finally stop being an idiot it will now print out my roof and now we can do the exact same thing from the top so we can start navigating it down we can go all the way down through the hierarchy so for example I could say my route I can also keep just doing it now so I can chain get nose to the cows come home so I can go that route yeah stop being an idiot let's try this again and their boo I got an error my roof why do you not like my route oh jeez I'm stupid today alright get No there we go so there you see we're now at the grandparent we can also fully address it out in this hierarchy so I could come back to here and this time I will not forget the closing brace I can also do it this way so you can just keep adding these until the cows come home get parent Parent Child one as an example now these note names are all case sensitive so do be aware of that but this will now return in this art here will will your child too haven't actually used it yet and then we'll spit up that name and there you see child two is the note that's returned so that is how the various different addressing goes now let's look at a node in the middle so instead we're going to ask we're gonna apply a node to grandparents bed or a script to grandparent let's go here attach script it's exact same process it's just we're a little bit different spot in the tree so I want to show you that instead so here we are in the ready function and don't worry I will explain exactly what ready is later on but now I want to show you how you obsessed child notes we're dealing here with the oven again a root level leaf of like so the farlis down in the tree it had no children but as you see grant parent has a child and that child has two children so let's look at how you can then access them exact same process what you have to do so for example I want to get that parent I can do get no parent can see here I get nice code completion of the options available to me so it automatically figures out who I have as children note so we do Co completion get it out quickly so there you go that is one way of getting your parent now you're gonna do this again get note is so very very commonly used that there is actually a shorthand version of this so I can go here and instead go parent and this gives me the the child note with that name so I can do literally this line of code print dollar sign parent name like so so it's a lot like oh it's that famous JavaScript library that started this that's not coming to me but it's a very common practice it's just a quick masking it's this is a functional equivalent of get node quote our parentheses open quote and then the string close quote this is just shorthand form of it very common way it can cut down on some code it's this is literally the exact same line of code as this those are identical so just know you could do one or you can do the other as you wish now another thing you can do is let's get our parent here so I'll use the shorthand version so now that we got our parent you notice our parent has two children another way we can access child notes is using get duped not named so our node has a method called get child and this takes an index so this is what I said indexing is very important so we have 0 and 1 so I'll grab one let's go ahead and actually put the results out so print so you can get children nodes the index of the order they appear in so you'll see here it's printing out child 2 as the result let's spin that down and another option you've got is to actually search for note so at any point in this tree including all the way up to the very top of the root there is an option called find no so if you need to search for a note you can go find node and find node takes a couple things first one is a mask of the name so part of them so if I was looking for a parent I could type parent net etc and this case I'm gonna give you the full name though so child to that's the note I'm searching for do we recurse through the free yes we do do we own it no don't go ahead and search that so let's do a print on the name of the result found dead oh there you see child 2 is immediately resulted I don't know why we're getting doubles on that but did we get that again oh I must have a print still in here yeah that's why oops let's turn that guy back into empty and if we run it again you will see now we are only going to get child 2 written once so back to our code so that is how you can actually search for a node and finally the last way you're going to do things is by iterating through the node so in this case again let's go find the parent so again we're in grandparent right now and parent is the only one with multiple children so we will go parent and now what we have the option of doing is getting all of our children as an array of nodes and that is done using the get so it's gonna get child get children like so and then this is array so I'm not going to get into the syntax of iterating through arrays but you could actually just now go through all of them that's in the coding category I'll do that very shortly but you can also grab it like that so that's getting the first that would print out child one that would print out child two and that is essentially how you can create a hierarchy of nodes how nodes interrelate how you program and communicate between nodes etc so you see it's a very straightforward hierarchy now one thing I didn't really touch on is you have selective nodes so for example and this is we're getting ahead of ourselves we're gonna come back and cover this later on but for example a physics note so let me go physics so let's say a rigidbody so I'm gonna create a rigidbody node it has now inherited to child but you're noticing immediately here there's this error message or warning message and it says has no children shapes but this is basically saying that this node requires other children note specific notes it requires a child a collision shape or collision polygon as a child node and that will only go away once we create that so it's easy enough we can just add a collision shape to it and it is immediately happy but it now has requirements and it has to have a shape defined to it and again I will get into how you go about doing this later on but I did think it was important to show you this that specific notes have requirements for children nodes or for parent notes etc and those will be explained to you by the editor when they exist so this is sort of like when certain components in other gaming just depend on the existence of another component this is how that relationship is expressed within the goat good dope game engine but we will cover this in great depth once we get on to the the process of actually dealing with collision objects etc so what when we hit those relationship details I will cover them but I wanted you to know that certain node relationships are enforced and required by Godot but it makes it very clear to you when a certain parent requires a certain type of child etc now the final thing I want to show you and I'm gonna show you a lot of detail but because I only have one scene but this is where the tree comes in now the tree is used for switching between scenes and let's say we wanted to switch from this scene to another scene I only have one so I'm gonna be cheating a little bit well let's go back to the script for a grandparent and we get rid of everything now if you want to switch between scenes this is where the tree comes in so the code to do so is get tree and then you just simply call change scene or change scene two and change scene allows us to basically pass in the string name of the past so in the kiss case we only really have one scene it's my might seem TSN slit so if I had a to that and as soon as this code runs that other scene will load so if we had a second teen in here this would fire it off so that's where the tree comes in the tree is responsible for switching between scenes and a few other things but this is probably the most ready function other than forgetting that root node so do be aware of that the other thing I'll get back to that we'll cover 2d so that's really all you need to know I won't look at switching scenes later on in a more detailed example but a bigger it fit here so that is another rule of what the tree is so in a quick summary before I finish a tree is holds the scene the scene itself contains one or more nodes nodes in turn are a hierarchy of the stuff that goes together to make up the visible items in your game and then all kind of basically inherit from each other as they go down the hierarchy so they get all the functionality of the node type that the derive from and the type that it's derived from and the type of its drive from etc and they actually expose it all to so if I take something very high down this less so say a static body so I got a static body defined here you'll see here in the inspector we got the static body and it's derived from the physics about it so we can change its property its defined from collision objects we can change its properties its from no 2d so we can select its projects and no TD intern is a canvas item which in turn is a node standard object or any programming stuff going on there but just do be aware that relationship exists and again we will cover that in more detail later now as I said a pretty straightforward but I'm kind of dull topic but it's one of those things that you really need to get to understand Godot going forward it's very once you've got it you get it because it's very it's a very clean way of doing things it's a lot lot simpler in many ways that you know you're just dealing with nodes and collections of nodes and hierarchies of nodes and that's it so you don't really need to wrack your brain about the difference between them but we did show a couple of other things here now again if I lost you a little bit on the coding don't worry I will have a GD script tutorial up very soon that will cover some of the things like lost over like such as what the heck that ready function is and somewhat repeatedly how to attach a script how that stuff works so we will get into that very very shortly but I hope what we did today does make sense because this is the stuff we're going to build on for the rest of the entire series and don't worry the next tutorial is going to be more about well other than GD script 1 is going to be about 2d graphic so we're going to subscribe work drawing things on screen and really making things a bit more exciting so do stay tuned for that but I hope you learned something here because this is important stuff to know again may not be the most exciting things in the world but it is probably one of the most important concepts to understand when it comes to the Godot game engine and once again there is the chapter for this available to patreon backers it's based on the same code so everything we just did here will be available there I also make the as the samples here work yeah I'll make the sample code available on the game from scratch entry for nodes as well which I will link down below once that's published so if you want any of the source code for what we just covered in this I will have those examples there as well so I hope you enjoyed that if you did of course please do click that like button and one other thing that kind of came to my notice recently apparently this is stupid but YouTube only notifies you 100% if you click that Bell icon otherwise you get a 10% chance of 20% chance or something of getting notification when a new video is published on a channel you fall out which is exactly the opposite of how I figured it worked but apparently if you want to get notified 100% when a new video is done you do need to click that little bell icon so if you're not already subscribed and you want to you know stay up to date please do hit that subscribe button but more importantly I guess hit that notify button cuz that's the only thing that guarantees you will actually get notification that is if on YouTube isn't currently bugged out so anyways hope you enjoyed that and again if you're interested in the book please do check out the patron that would be greatly appreciate and also help support the channel in general and I will see you all very soon oh and also of course if you have any questions comments confusion anything at all like that please do let me know in the comments down below I try to be as thorough as possible in the timeline possible but this can be a bit confusing so I understand if you have any questions or comments do shoot them down below and I will do my best to answer them alright I will see you all later good bye
Info
Channel: Gamefromscratch
Views: 72,682
Rating: undefined out of 5
Keywords: Godot, Tutorial, Node, Nodes, Scene, Scene Graph, GameDev, Game Development, Level, Game Programming, GDScript
Id: WUARiOGSGKY
Channel Id: undefined
Length: 31min 31sec (1891 seconds)
Published: Wed Mar 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.