This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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 =
        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", ""], true)
        if result == 0:
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:

by (22,704 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 (658 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.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", ""], true)
        if result == 0:
        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 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 Frequently asked questions and 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 [email protected] with your username.