You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
7.3 KiB
Plaintext
113 lines
7.3 KiB
Plaintext
<!--221006,220922,221103-->
|
|
<h1>coroutines in godot engine </h1>
|
|
august 11, 2022<br>
|
|
#gamedev #coroutines #godotengine #programming #demo<br>
|
|
<br>
|
|
<p>Demonstrating coroutines in Godot Engine with a simple application. <br></p>
|
|
<img src="/static/img/ent/stoplight_screenshot.png" alt="(screenshot: a simple tech demo with a stoplight and a walk button.)"><br>
|
|
<br>
|
|
<h2>defining coroutines </h2>
|
|
<p>Coroutines are functions that, instead of running to completion, yield until certain criteria are met. Godot Engine supports coroutines through <a href="https://docs.godotengine.org/en/stable/classes/class_@gdscript.html#class-gdscript-method-yield">yield()</a>, <a href="https://docs.godotengine.org/en/stable/classes/class_gdscriptfunctionstate.html#class-gdscriptfunctionstate-method-resume">resume()</a>, and the <a href="https://docs.godotengine.org/en/stable/classes/class_gdscriptfunctionstate.html">GDScriptFunctionState</a> object. <br></p>
|
|
<br>
|
|
<h2>why use a coroutine? </h2>
|
|
<p>Coroutines allow for scripted game scenarios that respond dynamically to the player and the changing game world. They let you bounce between functions, step-by-step, and respond to interruptions. This means functions can be automatically called at the completion of other functions, animations, player actions, in-game events, or timers. Add in interruptions and conditionals, and you have a tool for building a responsive game world. <br></p>
|
|
<br>
|
|
<h2>stoplight example </h2>
|
|
<p>As a simple demonstration, I made a stoplight. Follow along with my code on <a href="https://gitlab.com/chimchooree/stoplight">GitLab</a>. <br></p>
|
|
<br>
|
|
<p>The light changes every few seconds, going from green, yellow, then red. The light changes immediately if the walk button is pressed. This demonstrates that methods can wait for criteria (a timed duration in this case) to be met before resuming, and they can be influenced by player action. <br></p>
|
|
<br>
|
|
<a target="_blank" href="/static/img/ent/stoplight_demonstration.gif">
|
|
<img src="/static/img/ent/stoplight_demonstration.gif" alt="(gif: demonstration)" width="500" height="281.67">
|
|
</a><br>
|
|
<br>
|
|
<h2>how is it written? </h2>
|
|
<h3>node hierarchy </h3>
|
|
<img src="/static/img/ent/stoplight_nodehierarchy.png" alt="(screenshot: node hierarchy in the editor. Root is a node named Main. It's children are TextureRect BG, AnimatedSprite Stoplight, Sprite WalkButton, and a Label. Stoplight's child is a Sprite. WalkButton's child is a TextureButton.)"><br>
|
|
<p>I have a TextureRect background, an AnimatedSprite stoplight, a Sprite walk button with a TextureButton, and a label for displaying a timer. Most of the code is attached to the root. It's better to have code closer to where it's being used and to mind your separation of concerns in real projects, though. <br></p>
|
|
<br>
|
|
<h3>animation</h3>
|
|
<img src="/static/img/ent/stoplight_animationframes.png" alt="(image: the AnimatedSprite Stoplight has 4 animations - default (which is no light), green, red, and yellow.)"><br>
|
|
<p>The light is changed by setting its animation to one of these options. Each is one-frame - just the stoplight with the one or none of the lights colored in. <br></p>
|
|
<h3>the code </h3>
|
|
This project has two scripts: Main.gd, which is attached to the root node, and Label.gd, which is attached to the Label. <br>
|
|
<br>
|
|
<h4>Main.gd - available on <a href="https://gitlab.com/chimchooree/stoplight/-/blob/master/Main.gd">GitLab</a></h4>
|
|
<pre><code>extends Node
|
|
|
|
onready var stoplight = $Stoplight
|
|
|
|
func _ready():
|
|
stoplight.play()
|
|
|
|
var result = wait(5, 'green')
|
|
$WalkButton/TextureButton.connect('pressed', result, 'resume',
|
|
['interrupted on green'], CONNECT_ONESHOT)
|
|
yield(result, 'completed')
|
|
|
|
result = wait(5, 'yellow')
|
|
$WalkButton/TextureButton.connect('pressed', result, 'resume',
|
|
['interrupted on yellow'], CONNECT_ONESHOT)
|
|
yield(result, 'completed')
|
|
|
|
result = wait(5, 'red')
|
|
$WalkButton/TextureButton.connect('pressed', result, 'resume',
|
|
['interrupted on red'], CONNECT_ONESHOT)
|
|
yield(result, 'completed')
|
|
|
|
func wait(time, color):
|
|
print('waiting for: ' + color)
|
|
var result = yield(get_tree().create_timer(time), 'timeout')
|
|
if result:
|
|
print(result)
|
|
stoplight.animation = color
|
|
print('done: ' + color)
|
|
|
|
func _on_completed():
|
|
print('completed')
|
|
|
|
func _on_WalkButton_gui_input(event):
|
|
if event is InputEventMouseButton and event.pressed:
|
|
print ("Walk Button not functioning.")</code></pre><br>
|
|
<br>
|
|
<h4>Label.gd - available on <a href="https://gitlab.com/chimchooree/stoplight/-/blob/master/Label.gd">GitLab</a></h4>
|
|
<pre><code>extends Label
|
|
var time_start = 0
|
|
var time_now = 0
|
|
|
|
func _ready():
|
|
time_start = OS.get_unix_time()
|
|
set_process(true)
|
|
|
|
func _process(delta):
|
|
time_now = OS.get_unix_time()
|
|
var elapsed = time_now - time_start
|
|
var minutes = elapsed / 60
|
|
var seconds = elapsed % 60
|
|
var str_elapsed = "%02d" % [seconds]
|
|
text = str(str_elapsed)</code></pre><br>
|
|
<br>
|
|
<h2>how does it work? </h2>
|
|
<p>At <span class="code">_ready()</span>, <span class="code">wait()</span> is assigned to the GDScriptFunctionState <span class="code">result</span> and is called for the first color, green. <span class="code">_ready()</span> yields until <span class="code">wait()</span> is completed. <br></p>
|
|
<br>
|
|
<p>The wait method yields for the given amount of seconds then sets the stoplight to the given color. <br></p>
|
|
<br>
|
|
<p>At <span class="code">wait()</span>'s completion, <span class="code">_ready()</span> calls <span class="code">wait()</span> for yellow, then red. Each is called one at a time, waiting for the color to complete before moving on. <br></p>
|
|
<br>
|
|
<h3>interrupting the stoplight </h3>
|
|
<p>The Wait Button interrupts the wait times between colors. Before <span class="code">_ready()</span> yields, it connects the <span class="code">'pressed'</span> signal on the Wait Button. <br></p>
|
|
<br>
|
|
<p>If the Wait Button is clicked during <span class="code">wait()</span>'s yield, the GDScriptFunctionState <span class="code">result</span> resumes immediately, ignoring <span class="code">wait()</span>'s yield timer. This time, <span class="code">result</span> has a string arg <span class="code">"interrupted on green,"</span> so it will print the result, change the stoplight's color, then print <span class="code">"done: green."</span> The <span class="code">wait</span> method is complete, so <span class="code">_ready()</span> resumes and calls <span class="code">wait()</span> for the next color. <br></p>
|
|
<br>
|
|
<h2>play it yourself </h2>
|
|
<iframe frameborder="0" src="https://itch.io/embed/1643944?dark=true" width="552" height="167"><a href="https://chimchooree.itch.io/stoplight">stoplight by chimchooree</a></iframe><br>
|
|
<br>
|
|
<h2>applications </h2>
|
|
<p>The outcomes in this example can be swapped out with anything. I use coroutines in Blessfrey's skills to manage the flow of phases from activation, different phases of effects, cooldown, and interactions with any counters. I also use it in the basic weapon attack so the character continuously swings at the rate of his attack speed until he cancels, uses a skill, or moves. It could also be used for something like cars that stop and honk when the player walks in front of them then drive off once the path is clear. Anything influenced by other entities is a good coroutine candidate. <br></p>
|
|
<br>
|
|
<p>Coroutines enable practical ways to improve the flow and interactivity of games, so practice the concept a lot! <br></p>
|
|
<br>
|
|
<br>
|
|
Last updated July 31, 2022 <br>
|
|
<br>
|