Running callables in the same thread/sequence | Can I halt coroutines?

v4.5.1.stable.steam


Question

So I have this dictionary of commands,

var commands = {
	"wait" : func(x: String):
		var time = x.to_float()
		var start = tick()
		while tick() - start < time:
			if skipping:
				break
			await get_tree().process_frame,
	...
}

...

func tick():
	return Time.get_ticks_msec()

traditionally I would just use an Instanced Timer (await get_tree().create_timer().timeout). But I have a feature to Skip the dialogue’s typewriter effect so I opted to just ‘stall’ the Thread, however, using .call( … ) makes it run separately - I cannot directly access it and kill the coroutine; nor do I have any Idea of killing the coroutine/thread.

Furthermore, the reason I can’t simply call it, is because Godot’s Syntax refuses to do so,


So would I’ve been thinking, would It be possible to some how put these “threads” into an array and some how kill/halt them?

That’s why the chatbot who wrote the code put the skipping variable in there.

You can’t kill coroutines or threads “from the outside”. You can only message them to quit themselves.

1 Like

Chatbot? no, no, no, I wrote this myself..

Either way, it is asynchronous so it is a coroutine.

,and so no matter what, It will not yield, however needs an await?..

Let the coroutine periodically check some control flag and let it exit if that flag is not set.

var run := true

var f = func():
	while run:
		await get_tree().create_timer(.3).timeout
		print(randf())
	
func _ready():
	f.call()
	
func _process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		run = false
1 Like

That doesnt, really do much, and breaking it is much more clearer.

Though, I’m seeing another problem, Its returning zero and stopping..

## Output

3.0 # print(time)
true # (tick() - start < time)
done # print(done)

It does precisely what you’ve asked. The point is to have a messaging mechanism through which you can tell the coroutine to exit. How exactly will that exit be implemented inside coroutine code is up to you.

1 Like

Well, its not yielding nor stopping in the while loop anymore my friend.

Have you tried to run my example?

1 Like

Yes I did,

I found the problem anyway, nothing was wrong with my code, and it did run synchronously, I switched the tick function to this, and it worked, I think It failed to work with bigger numbers, but thanks for your help.

func tick():
	return Time.get_unix_time_from_system()

Well that would entirely depend on what you pass as an x argument, which is information you didn’t supply in your question. If you pass time in seconds there and compare to time in milliseconds, then of course the thing would exit almost immediately.

1 Like