godot: Thread does not start with a method with no arguments

ElementaryOS 0.4.1 (Ubuntu 17) - Godot version: 2.1.3

Thread does not start with a method with no arguments This node works:

extends Node
var thread = null

func _ready():
	thread = Thread.new()
	var err = thread.start(self, "work")

func work(userdata):
    print("oi")
    while(true):
        print("oi")

But this does not:

extends Node

var thread = null



func _ready():
	thread = Thread.new()
	var err = thread.start(self, "work")

func work():
    print("oi")
    while(true):
        print("oi")

I found this error during my question (https://godotengine.org/qa/16730/thread-not-running), but only by adding the argument did the method start to run.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 36 (35 by maintainers)

Most upvoted comments

@schweigert it’s better if you open a new issue with your suggestion.

@schweigert I think, if we introduce a new keyword for threads, it should be located at the function invocation rather than at the function itself.

func _ready():
    var thread = async my_method(1, 2, 3)
    var no_thread = my_method(1, 2, 3)

func my_method(a, b, c):
    # do work
    pass

This would still allow us to call the method synchronously. I don’t know if it’s worth a new syntax sugar keyword though.

An option would be to make Thread.start a vararg method.

Ok, then that’s a bug IMO. It should be:

func _ready():
	var thread = Thread.new()
	thread.start(self, "work", [11, 22, 33])
	thread.wait_to_finish()

func work(arg1, arg2, arg3):
	print(arg1, arg2, arg3)

prints:

11 22 33

i.e. just like connect() or call_deferred() work.

I can confirm that it’s fixed in 3.4 beta 2 (so it was fixed by #38078).

Did someone fix this bug? It seems the workaround in the Voxel demo isn’t necessary in 3.4. https://github.com/godotengine/godot-demo-projects/pull/639

Was making a thread in my demo project today and i made a thread with no argument and ofc it did not work, and then after awhile i remembered this error 😃

Its extremely confusing to have the Thread.start() fail if the method called in the thread has no arguments. If I don’t pass any userdata to the Thread.start() method then it should not try passing an empty array to the method I’m calling in the thread.

If we keep it like that we should consider allowing 0 parameters for convenience reasons and to prevent confusion.

No, if you pass no arguments the parameter in the start method defaults to an empty array.

The OP’s problem is, that the thread doesn’t start even if there are no arguments given. This is intended.

Now we can discuss if the thread should start if the thread method has no parameters defined (func work():) and no arguments are given (thread.start(self, "work")) for convenience reasons and to prevent confusion for beginners.

I think Threads should still start even if there are no parameters defined in the Thread method signature.

  1. Not starting the thread at all leads to confusion if you don’t see/understand the error message
  2. Often you just don’t need an argument passed to your Thread

My proposal:

  • If no arguments are given and the signature doesn’t have any parameters defined → No error and start the thread
  • If arguments are given in the .start method but no parameters are defined → Show an error, or even better crash the game with an error
  • If no arguments are given but a parameter is defined → Run the thread with the parameter being null or []