0 votes

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")
        add_child(check_timer)




    func stop_check():
        if not check_timer.is_stopped():
            check_timer.stop()
    func start_check():
        if check_timer.is_stopped():
            check_timer.start()
    func _check_connection():
        result = OS.execute("ping", ["-w", "3", "google.com.tr"], true)
        if result == 0:
            emit_signal("connection_success")
        else:
            emit_signal("error_connection_failed")
in Engine by (43 points)

2 Answers

+1 vote

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:

https://docs.godotengine.org/en/stable/classes/class_os.html#class-os-method-execute

by (10,894 points)

Thank you for the information.

+1 vote

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

by (490 points)

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")

thank you for the information and code.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.