godotengine / godotengine/godot-docs
An Example which Should be Added to "Coroutines with yield"
- Dominant language
- reStructuredText
- Stars
- 5.7k
- Forks
- 3.8k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 25
Description
**Godot version:** v3.4.3.stable.official [242c05d12]
**Issue description:**
A fundamental issue I ran into trying to learn GDScript was that there was seemingly no clean and simple way to pause execution of an entire script. Sure, yield() can pause a FUNCTION in a script, but not the script as a whole. One of the key things I wanted to be able to do with an RPG I'm working on is script out actions and events and dialogue using GDScript directly, but it didn't seem like there was a good way to do this.
However, I couldn't accept that, so I'd been tinkering around with how yield() works trying to come up with a simple method for being able to yield an entire script and while I didn't find a definitive solution, I found a VERY simple workaround which I feel should be explained in the documentation to some extent because of how fundamental this is towards creating RPGs with Godot:
```
var y
func YieldScript():
print("1st Yield Performed"); yield()
print("2nd Yield Performed"); yield()
print("3rd Yield Performed"); yield()
print("4th Yield Performed"); yield()
func _process(delta):
if Input.is_action_just_pressed("ui_accept"):
if (y != null):
y = y.resume()
func _ready():
y = YieldScript()
```
What this code effectively does is starts the script I want to be able to suspend execution from simply and quickly and stores the yielded function in y. Then, during normal processing (or this could also be attached to a signaled function) a check is made to make sure y is not null, and if it's not resumes y while storing the resumed result into y again!
This methodology of using yield() is not explained in the documentation and I REALLY feel like it should be, along with an example to demonstrate how using yield in this manner can allow for processing a script in stages.
**URL to the documentation page:**
https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html?highlight=yield#coroutines-with-yield
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.