When I ping the internet connection, the game freezes.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By maxiproduksiyon

I pinged with os to check that the internet is working. ping works fine. But the game freezes every time a ping is thrown. Problem with OS.execute or ping. what should I do? My aim is just to check if there is an internet connection.

also Unicode error: invalid skip error occurs.

 signal connection_success
    signal error_connection_failed
    var result =null
    var check_timer = null
    func _ready():
    	check_timer = Timer.new()
    	check_timer.autostart = true
    	check_timer.one_shot = false
    	check_timer.wait_time = 2
    	check_timer.connect("timeout", self, "_check_connection")
    func stop_check():
    	if not check_timer.is_stopped():
    func start_check():
    	if check_timer.is_stopped():
    func _check_connection():
    	result = OS.execute("ping", ["-w", "3", "google.com.tr"], true)
    	if result == 0:
:bust_in_silhouette: Reply From: jgodfrey

It looks like you’re explicitly calling OS.execute() with blocking enabled (that 3rd argument). Setting that to false will allow the Godot thread to continue running while the command is executed.

Note, there are some differences in the return value handling based on the blocking flag. See details here:

Thank you for the information.

maxiproduksiyon | 2020-11-23 20:59

:bust_in_silhouette: Reply From: AndyCampbell

I believe that when you call OS.execute, the true parameter makes that a blocking call, so the game will stop to wait for the command to finish. In your example every time you run ping for 3 seconds, you get a 3 second pause.

However, if you change that to false, which is non-blocking, it will not return the output.

You could try using a separate thread to launch the ping command. Docs are here

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 _check_connection 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 :slight_smile:

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():
	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:
		print("%s) Ping finished" % count)
		count += 1
	print("Thread stopping")

AndyCampbell | 2020-11-17 17:47

thank you for the information and code.

maxiproduksiyon | 2020-11-23 21:00

ok I understand. there are problems with the pandemic here.my office is closed.
i will try when i go to the my office. Thank you.

maxiproduksiyon | 2020-11-23 21:09