How to Use Coroutines: Godot GDscript tutorial

Sdílet
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/"

Komentáře • 38

  • @adamgascoine
    @adamgascoine Před 4 lety +17

    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.

  • @gadzhikharkharov140
    @gadzhikharkharov140 Před 5 lety +7

    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.

    • @krzysztofswierzy4226
      @krzysztofswierzy4226 Před 3 lety +2

      Oh my God, You have just enlightened me, thank You.. It was so cryptic !

  • @leandrokl2827
    @leandrokl2827 Před 4 lety +1

    This combination of Tween with Courotines is fantastic !

  • @boerbol9422
    @boerbol9422 Před 2 lety

    Thank you so much, this is extremely helpful!

  • @alexanderglassgames4170

    Thanks man, now I could finally use 'yield' to do something I wanted but just couldn't :D

  • @Smaxx
    @Smaxx Před 5 lety

    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.

    • @kunai9809
      @kunai9809 Před 2 lety

      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

  • @hidemat5141
    @hidemat5141 Před 4 lety +2

    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?

  • @thomasbjarnelof2143
    @thomasbjarnelof2143 Před 5 lety +2

    What's the difference between
    yield(play_intro(), "completed")
    and just calling play_intro() ?

    • @arthur5552
      @arthur5552 Před 5 lety +4

      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

  • @decrodedart2688
    @decrodedart2688 Před 5 lety

    Awesome thanks

  • @diegohiroshiysusvideos2909

    It's amazing xD thanks! little Godot 7w7 better that Blender. Regards from Argentina

  • @johnnygossdev
    @johnnygossdev Před 5 lety

    Mind. Blown

  • @fecu2394
    @fecu2394 Před 4 lety

    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...

    • @MoogieSRO
      @MoogieSRO Před 4 lety

      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.

  • @CentralNaythf
    @CentralNaythf Před 5 lety +3

    can you make a floating platforms tutorial for a 2d platform game?

  • @ZoidbergForPresident
    @ZoidbergForPresident Před 5 lety

    What about cancelling the current target for a new one? Any way to do that with coroutines?

  • @Destroyer19941995
    @Destroyer19941995 Před 3 lety

    Sometimes when I have If statement and yield inside if and if i increment somethin by +1 it increments by + 60

  • @timothyeggers7825
    @timothyeggers7825 Před 2 lety

    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.

  • @___frkn___
    @___frkn___ Před 5 lety

    11:57 what if that function returns an object? Will it wait for function to complete or wait for completed signal from returned object?

    • @Gdquest
      @Gdquest  Před 5 lety

      Completed is a special case so it should wait for the function to return or to end in an invalid state.

    • @___frkn___
      @___frkn___ Před 5 lety

      @@Gdquest Thanks.

  • @damascake
    @damascake Před 5 lety +3

    Is this openRPG is a "RPGmaker killer"?

    • @Zen-rw2fz
      @Zen-rw2fz Před 5 lety +5

      Well, godot can do way more than RPGs, I really hate rpg maker because it's actually limited to these crappy JRPGs

    • @Zen-rw2fz
      @Zen-rw2fz Před 5 lety +1

      @@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

    • @Zen-rw2fz
      @Zen-rw2fz Před 5 lety +2

      @@Derheight walking simulators pretending to be role playing games

    • @samqwerty
      @samqwerty Před 5 lety +4

      @@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.

    • @Zen-rw2fz
      @Zen-rw2fz Před 5 lety

      @@samqwerty RPG maker sucks

  • @geogreenmanu
    @geogreenmanu Před 5 lety

    Please share the link to the doc page at 1:29.

    • @DairanPL
      @DairanPL Před 5 lety

      www.wikiwand.com/en/Coroutine

  • @chiboreache
    @chiboreache Před 5 lety

    5:05 so we can look at *Vuex* and implement some external mutator, which is remember the state ;)

  • @cyb
    @cyb Před 5 lety +1

    So basically async/await equivalent in js.

    • @RandPixel
      @RandPixel Před 5 lety

      Not exactly, but close

    • @Gdquest
      @Gdquest  Před 5 lety +1

      It's the equivalent of function* and yield in JS. I think async and await in both Py and JS do more than that.

    • @cyb
      @cyb Před 5 lety

      @@Gdquest ahh generator functions