How to Use Coroutines: Godot GDscript tutorial
Vložit
- čas přidán 22. 08. 2024
- Support our Godot course on Kickstarter! bit.ly/godot-ki... 🌟
Kickstarter source projects: github.com/GDQ... 🤖
Discord ► / discord
Twitter ► / nathangdquest
This video is licensed under the CC-By 4.0 license: creativecommon...
You can attribute it to "GDQuest and contributors - www.gdquest.com/"
Your tutorials have been tremendously helpful to me - thank you so much for the content.
One thing to note to any viewers - you have to be very careful using yield. If you free the object running the yield (an enemy that dies, a scene that reloads, the child of a scene that reloads) it will crash your project in ship. The Yield continues even if the initiating code is removed, and upon completion, it will look for the return point and find none: so crash.
I found this out after using co-routines liberally for a tower defense game (spawn timers, round timers, damage ticks etc). I have to go back and rewrite them all.
TLDR: Don't use yields if there's a chance that your object can be deleted. If you must, there's a fair bit of work to do to make sure it doesn't throw up a crash in ship.
Great intro. The "completed" signal is what got me tricked in the beginning when I needed to call a method that returns a value, but has a yield statement inside -- took me a while to realize that I need to call that method as a coroutine itself.
Oh my God, You have just enlightened me, thank You.. It was so cryptic !
This combination of Tween with Courotines is fantastic !
Thank you so much, this is extremely helpful!
Thanks man, now I could finally use 'yield' to do something I wanted but just couldn't :D
For the example at around 4 minutes in: You don't have to check `active` in the middle to fix the delayed stop. Instead, I'd just modify my potions first and then start the timer. This way it will always stop immediately and you've got no extra branch. Depends on the actual use case of course.
sure, would work. But there are also reasons not to do it like that: you could exploit that. If you spam click the active toggle in your case, you could get a lot more potions per second than just one
Thank you guys. One thing to note or clarify. If the function I'm calling in yield doesn't have a yield call itself then an error occurs: "First argument of yield() not of type object."
So for example:
func im_a_fuction():
print("I'm a function")
func _ready():
yield(im_a_function(), "completed")
What am I doing something wrong?
Your im_a_function() needs a yield.
What's the difference between
yield(play_intro(), "completed")
and just calling play_intro() ?
thanks
TLDR:you only have to use --yield(play_intro( ), "completed")-- when you have a yield inside play_intro( )
as far as godot is concerned, yield(get_tree.create_timer(0.5),"time_out") is the end of play_intro( ), and thus continues to the next line of the function that called play_intro( )by using yield(play_intro(), "completed") you force godot to wait for the full function to finish
Awesome thanks
It's amazing xD thanks! little Godot 7w7 better that Blender. Regards from Argentina
Mind. Blown
4:49 I've been going through the whole playlist here and is the first thing I've not understood.
So you have a bool variable with a setget. Then you have the set method with a while loop on that bool variable. Cool, now you're controlling the loop remotely. Lovely jubbly.
Why did you add a second evaluation "if not" with the loop? Was it just to stop that final iteration before "while" evaluates false? Your while loop is about to evaluate false in a split second, I don't see what the point of the "if not" is.
I'm obviously missing something...
The "if not" is because "active" could go from True to False during that 1-second timer delay. It's kind of a silly example case because I don't really see why anyone would start it off with a timer delay. If anything I'd probably put that yield at the end of the function, not at the start of it.
Edit: I thought about it a bit more and actually it does make sense. Putting it at the start makes the potion increment after at least 1 second. Putting it at the end would give the first potion immediately after hitting Start.
can you make a floating platforms tutorial for a 2d platform game?
What about cancelling the current target for a new one? Any way to do that with coroutines?
Sometimes when I have If statement and yield inside if and if i increment somethin by +1 it increments by + 60
Is there anything wrong with having a lot of coroutines like this and relying on tree timers? I hear different things on forums... I guess its programmer elitism or something.
11:57 what if that function returns an object? Will it wait for function to complete or wait for completed signal from returned object?
Completed is a special case so it should wait for the function to return or to end in an invalid state.
@@Gdquest Thanks.
Is this openRPG is a "RPGmaker killer"?
Well, godot can do way more than RPGs, I really hate rpg maker because it's actually limited to these crappy JRPGs
@@Derheight with jrpg I mean crap like some of these overrated nintendo based games, games like monster hunter still look cool and yeah rpg maker sucks too
@@Derheight walking simulators pretending to be role playing games
@@Zen-rw2fz you only accept the world that's in your mind lol. I actually enjoyed RPGMaker a lot when I was young, making zelda-like games I remember (you could make a custom "action rpg" system). It was my first step to game development and I'm sure many developers identify with this.
@@samqwerty RPG maker sucks
Please share the link to the doc page at 1:29.
www.wikiwand.com/en/Coroutine
5:05 so we can look at *Vuex* and implement some external mutator, which is remember the state ;)
So basically async/await equivalent in js.
Not exactly, but close
It's the equivalent of function* and yield in JS. I think async and await in both Py and JS do more than that.
@@Gdquest ahh generator functions