Godot: Custom Main loop in GDScript and CSharp (no engine changes)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what up nerds in this video I want to cover how we can actually write our own custom main Loop and override it inside of gdau this can be useful if you want to add some custom functionality to say the scene tree itself now if this video actually helps you and you want to support the channel you can uh check out my steam developer page that I will link in the description below as well as like And subscribe so to kind of get things started I'm going to do this a little bit differently than what I normally do I'm actually going to do everything over the course of the video as some people have asked me to actually slowly type some of this stuff out and yeah we can try this formula out so let's get started so I'm going to show this in both GD script and C so it doesn't matter which language that you actually end up using the first thing that we want to do is go down here and create a new script and we can call it custom Loop for uh GD to say that it is the GD script version and we're going to inherit the scene tree so we're going to go ahead and create that and then if we go into the documentation and take a look here we actually want to look at the main Loop class so when we go in here we can see an example of what we can actually do the few methods that we actually need to override are pretty straightforward initialize you know it gets called when uh everything is initializing finalized is pretty much the end of everything physics process and process now for this demonstration we're not going to be messing with the physics process or process we're really just going to be tacking on extra properties and signals and things like that all right so when we go into the scen Tre oh the script itself let's just go ahead and create uh VAR thing equals 3 all right that's that's all we want to do so then we go and into the scene tree create a basic node add a script to it and all of this is fine so in here we want to do G tree. print oh no it was thing and then we want to wrap this inside of a print statement all right so we'll go ahead and save the scene and next we have to actually tell gdau that we want to use this script as the new main Loop so inside of the Run setting and make sure you have advanced settings turned on because if you don't all we get is the main we actually want to put this as our name custom Loop GD now if we run this it will not work and I'm going to show you it doesn't exist so what we have to do and this is very important is make it a class name and this is actually the name that you will be putting into that uh dialogue box so if I change this to custom Loop and run it still doesn't work so if I change this to custom Loop and then run it it works as expected so let's go ahead and change it back to GD for the differentiator and now we can do whatever we want we can go ahead and add a signal say thing happened and then from here if we just do G tree and try and look for it it won't work because G tree is going to return the type of scene tree so what we can do is cast it inside of here as Uh custom Loop GD and then close the parenthesis so this is a cast it's going to execute first and now we have access to the signals properties methods uh anything that we have in that class okay and then you can just do connect just like any other thing right all right so that's how we can kind of do the stuff uh on the GD script side and admittingly it can be a little bit wonky so to like always have to cast so something else that we can do we can create a static function get main pass in a node and that can return the custom Loop GD and we can just do this as Uh custom Loop GD and it does the casting for us really now because this is a static function we all we're going to have to do is call the class name dogit Main and the reason that we're passing in the node is so we can actually get the scene tree itself because this is a static function all right so what that means is it doesn't actually live on any instance so what we can do here is custom loop. GD git main self and it's just like as if we casted it ourselves all right so that's something that you can do to kind of uh make it easier to retrieve and now let's look at how we can do the same thing but on the CP side all right so on the C side we're just going to create a new script we want this to be C it'll inherit from scam Tre and we're going to call this custom Loop CS all right and then let's go ahead and create that bring it up and from here we can do the same thing public int uh we can just call it say C VAR make it our nice getter and Setter and then set it equal to 1,000 just so we can see that it is actually working so we're going to go back here go into the project settings change this to custom Loop change it to Cs and then go into here this won't work because thing doesn't exist anymore so we can do csharp VAR and it's going to fail because we need that Global class so we just add that annotation and run it and it works so the global class annotation is something that's fairly recent so you used to not be able to do this inside of C so you can do the same types of things inside of the C sharp version that you can with the gript version you know setting custom signals setting uh additional properties setting up your own initialization your own tear down and things like that um but we can also do something in C to make it a bit easier to actually get the specific uh scene tree that we want so let's take a look at what we can do uh if you're familiar with C uh then you'll be familiar with extension methods so let's go ahead and name this uh just seam tree ex extensions and then we want this to be a static class all right and then we'll go ahead and write our extension method uh then we can do let say get tree and we want it to be on the Node class where T is going to be a scene tree and all we're going to do is return node do uh G tree as T all right so then let's go ahead and go in here and create a new script we want it to be on C CP inheriting from node is fine we can just name this example all this is fine so far then we'll go in here make sure we're using the correct name space custom main Loop because if you see it automatically added this name space for me so then we can just do this dog tree and then it'll be custom Loop Cs and from there we can get the C variable and do we can do something like that right all right so then I'll go ahead and save that then can go ahead and do this close that out make sure that we're yep using the Cs one and then run R run we get the same result so that's how you can go about actually replacing the main Loop adding some custom functionality if you want to and and both GD script and c and just a another little tidbit potentially quick way to help ease actually retrieving the main Loop that you want now some things that you might want to do uh by doing this is maybe you have a very simple say object you you don't want to instantiate a whole node into the scene tree uh but you do want something that lives throughout the entire lifespan of the application so what you can do is create it inside of your own custom scene tree like your uh main Loop and everything right so this really kind of opens the door to architecting things in a bit different of a way now obviously if you do need scen tree functionality for your say Singleton uh I would still encourage you to use autoload uh as well as if you just want something simple and it works just just go with autol loads for Singleton as well but really this is just an alternative approach if you wanted to take it as well as you know tack on some extra things that you wish the scene tree did maybe some true Global uh signal or maybe you have something you want to make sure it gets initialized before the rest of your game uh and you know really the sky the limit here and gdau really makes it very easy to actually override this Behavior if you have any questions or comments uh please let me know in the comments below uh and that about wraps everything up later nerds
Info
Channel: Duroxxigar
Views: 209
Rating: undefined out of 5
Keywords: Godot, GDScript, MainLoop, SceneTree, C#, CSharp, GodotSharp, GameDev, Tutorial
Id: fCk7mtf0XX4
Channel Id: undefined
Length: 13min 44sec (824 seconds)
Published: Mon Apr 29 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.