Error when starting a thread

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aaron
:warning: Old Version Published before Godot 3 was released.

I have a script like this:

var _thread

func start():
    _thread =
    _thread.start(self, "_thread_func")

func _thread_func():

Fairly basic stuff.

But when my game runs and I reach the point where this code is run I get a background error. There’s no stack trace for it, but it only appears when the line _thread.start(self, "_thread_func") is present. This is what I get when I hover over the error in the error box:

enter image description here

Time: <time>
C Error: Method/Function Failed.
C Source: core/bind/core_bind.cpp:1939
C Function: _start_func

Aren’t you missing () after func _thread_func? Also, can you try renaming _thread_func to something else.

Bojidar Marinov | 2016-04-16 12:32

The missing () was a typo. Just put those in.

Aaron | 2016-04-17 01:47

:bust_in_silhouette: Reply From: volzhs

You need parentheses for function ().
And one parameter is need for thread function.

var _thread

func start():
    _thread =
    _thread.start(self, "_thread_func")

func _thread_func(userdata):
    # return for ending thread
    return 0

I actually did have the parentheses for _thread_func, but was missing the userdata parameter. The thread started to work once I included the parameter.

Aaron | 2016-04-17 01:46

Thanks for the help. I think both the documentation and the error needs to be more explicit that even if no userdata is expected, the function used in the thread needs to have at least one argument.

MageJohn | 2019-08-30 16:46