Threads | Godot GDScript Tutorial | Ep 27

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another episode in the GT script fundamental tutorial series in this episode we will be taking a look at threads so what exactly is a thread well the word thread is short for thread of execution basically it is a way for a program to divide itself into two or more simultaneously running tasks now threads is a form of concurrent processing often referred to as multitasking concurrency is when the execution of multiple tasks is interleaved basically instead of each task being executed sequentially it is instead run concurrently now one thing to keep in mind is that threads are used to balance processing power across CPUs and cores basically when you run a thread you're running it inside the CPU and how many tasks can run concurrently is based on the amount of course your CPU has now a unit of execution is called a process you can have multiple processes running simultaneously when using threads threads have something called a priority basically if your computer is lacking resources to run all your processes the program or rather the operating system will run the most important thread and when resources free up it will go ahead and run your processes that have a lower priority assigned to them now this is an example of what a sequential process would look like in a thread most of the time when programming in Godot you will be using only a single thread and that is your main thread now as you can see here your main thread has priority 1 basically high importance and these are your tasks basically you can think of these as your function calls inside of your classes now this is what a sequential process looks like in a single thread basically you run one task at a time now a single thread is called a processing unit so in this case you are using one processing unit however threads run side by side almost concurrently as you can see you can have multiple threads in this case we have three and each thread has a different priority now each thread is one processing unit you can call multiple threads with similar priorities in this case we have three threads all assigned a high priority in this case priority one however let's move on with the example of three threads with three different priorities one being the highest and three being the lowest now let's say your CPU only has the resources to one one priority from your application well the CPU will go ahead and run the thread with the highest priority they won't even start the other two until resources are freed up now let's say your CPU can only handle running two processes then it will go ahead and find your two threads with the highest priorities in this case one and two are higher than three but since you can only run two we will go ahead and run your threads with priority one and priority two and priority three will just have to wait on two resources for you in your game your priorities may look like this obviously your priority one would be your main game loop you want to run this at all costs this is the most important threat now priority two could be something like math intensive calculations for example you may need to calculate something in terms of distance or in terms of generating objects 100 meters away from your main character even though this is important it's not a priority and lastly your third priority could be something like non important Network calls this isn't multiplayer but rather let's say you're playing a game and every 10 seconds for some reason you want to make a network call to a server to grab your high scores maybe it's a popular game and when your character or rather when the player's character dies you want to show them the highest worldwide scores well you don't want to block your main game loop just calling Network calls to grab something as simple as a high score so you would assign that a lower priority it's important to your game or at least to you it's important to the way your game is handling user interface such as displaying a score when the player dies however if you have to wait an extra 5 seconds after the player dies it's no big deal now there are advantages to threats obviously the first one is it provides concurrency you can run multiple tests simultaneously now because you can run multiple tests simultaneously this by itself improves your game performance through a throughput computational speed and responsiveness and lastly it does reduce complexity of your applications now thread declarations in different languages are different but they follow similar processes the first is to declare a thread then you need to start your thread and then you need to exit the thread how exactly do you go about using threads in GT script well first you need to declare a thread in this case we use the new method on the global class thread and assign that to a variable then we can go ahead and start the thread when you use the start method from your thread object instantiation it takes in four parameters the first is the object instance most of the time this will be self because you want to use it in the class you plan on doing your thread computations so again in this case you will use the self keyword followed by the second parameter takes in a string method it's the method name in string format of the object instance the third is a value variant is just a way to save value now you must provide a value last parameter is a priority basically your thread priority now the new method is self-explanatory all it does is it assigns a new instantiation of the thread class to a variable the variable holds the thread object ID the heart of a thread is the start method it takes in again four parameters four arguments now one into the first two parameters the first two arguments are self explanatory object instance most likely the self keyword in many cases and the string method or a method in string format that's inside your current class however I want to look into the variant this is important the third argument is important because you need to put in a value so first of all user data passes the value to the argument of your string method your string method or rather the method in your class that you in the second argument of your start method must accept a parameter and the third argument in your start method must pass a value and it must be known at minimum or it will pass the no value at minimum now lastly is the priority by default we will be passing the normal priority however you can pass in zero one or two to let the system know that your thread is either low normal or high lastly you do have access to a weight to finish basically this lux your main thread while you wait for another thread to finish basically you join the thread and wait for it to finish you return whatever the method called return that you declared in your start method basically this is a weight and luck functionality that you can use in your current thread and wait for a value from another thread before moving on with your current thread we're gonna go ahead and take a look at that through code one thing to keep in mind is that the thread class object inherits from the reference class so basically your thread object disposes itself from memory once the reference count is zero so that's good news when using threads and GT script you do not have to worry about managing your thread in memory that is done for you now let's go ahead and take a look at some code so as you can see here we are inside the timer script now we are extending from the timer class and even though this isn't important except for when using a timer the timer class script is attached to a timer node which is in our scene tree out of scope for this video but something I wanted to let you know the most important thing is that we're using threads so of course I went ahead and declared a global variable thread now all of this is done on the ready function so we go ahead and we create a new thread using the new method from the global class thread so you can see here we're printing to the screen basically the ID of the thread and we're also given this method called is active basically this passes back or rather it returns a boolean true or false in this case our thread is not active so we are going to return false so keep that in mind just because you created a new thread does not activate the thread the thread is inactive to activate a thread you need to use the start method in this case it takes four arguments the first argument is the class object we use the self keyword we call a method in string format basically our current class has a function called start timer now our third argument needs to pass any value in this case we're just passing no we're not going to do anything with it and lastly we set a priority in this case we set it to zero now we're printing to screen start the thread and we're checking to see if our thread is active by calling is active method we will get back the value true basically because we use the start method our thread is now active now over here we're going to get a value back from our function in this case we're gonna get a value back from our start timer function however we don't want to execute the rest of the code because we're running two different threads remember we're calling a thread for the start timer function however our timer script because we're running in the ready function is running on another threat so technically we have two threads running at the same time one thread runs our ready function another thread is running the start timer function so basically if we want to wait for our second thread to finish we have to use the wait to finish method what this means is that as our function start timer runs in our second thread our ready function which is our first thread we want our first thread to wait now you have to be careful about this because you are locking your current thread in this example our function timer is very simple however the more complex your thread is the more time you have to wait therefore the more time your current thread has to wait before it can continue running its own code so what this is called is a lock we're looking on line 16 we are waiting for thread two to finish and pass us back a value before thread one can continue running the code so while we wait to finish what we're doing and our start timer function and take a look at this or start timer function must accept a parameter because we pass in a value the no value we're basically printing that we're inside timer these three lines of code were just basically setting up a timer timer is out of scope for this project but the most important thing is when we're done we're returning a value 100 so when our thread finishes we're gonna go ahead and return 100 and we're gonna pass that to our variable wait for thread once we've received that value we're gonna go ahead and we're gonna continue running our code in this case we print to the screen our value that's been returned in this case 100 we're printing this is my mistake but we're printing again the value 100 so let me go ahead and delete that and lastly this is the one I want to point out is the thread active and because we finish our function our thread is no longer needed so our thread closes so thread is active is false that's all I have for you in this episode I'm gonna go ahead and upload this file to get hub feel free to download it thank you for subscribing and thank you for clicking the like button I look forward to seeing you in the next episode have an amazing day
Info
Channel: Godot Tutorials
Views: 6,293
Rating: undefined out of 5
Keywords: Thread, Godot, GDScript, Concurrent, Processing, Tutorial, Series, Learn, Programming, Beginner, Fundamental, Code, Example
Id: WC54UlD8Q1s
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.