I think this refactoring of your code works
Note - I could not figure out how to get a timer to work inside the thread (I believe adding a new timer to the tree is not thread safe) so this just runs checkconnection in a new thread until you tell it to stop by setting check_network = false. In this example I do that after 10 seconds. Of course, you should remove all the print statements :-)
extends Node
signal connection_success
signal error_connection_failed
var thread
var count
var check_network = true
func _ready():
thread = Thread.new()
thread.start(self, "_check_connection", null)
yield(get_tree().create_timer(10), "timeout")
check_network = false
print("waiting for thread to finish")
func _exit_tree():
thread.wait_to_finish()
print("Thread cleaned up")
func _check_connection(data):
var count = 0
print("starting thread")
while check_network:
print("%s) Pinging" % count)
var result = OS.execute("ping", ["-w", "3", "google.com.tr"], true)
if result == 0:
emit_signal("connection_success")
else:
emit_signal("error_connection_failed")
print("%s) Ping finished" % count)
count += 1
print("Thread stopping")