How to use OS.execute()?

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

There aren’t any comprehensive tutorials on this, rather only a few forum posts with bits and pieces of partial answers. I have been using the little information online to barely use this function but I’m at a point where I can’t go on without knowing more.

I’m currently building a bot. I made a python script to do all the typing and I made a Godot UI to control the script. I kept making changes to the script, exporting it to an exe, and then running it using OS.execute() in Godot and it always worked. But recently, I made some changes to it and now the script exe can be opened everywhere except by Godot.

So, could I be using OS.execute() wrong? The function uses three main parameters.

The first is the target application’s path which makes sense.

But the second is the applications arguments. What even are these? Godot doesn’t explain in the documentation, it just gives an example- ["-w", "3", "godotengine.org"]. I have no clue what those mean and what they are supposed to do to the application. I imagine that my application isn’t running right from Godot because of these arguments.

The last one is blocking which doesn’t really make sense. “If blocking is true, the Godot thread will pause its execution while waiting for the process to terminate. The shell output of the process will be written to the output array as a single string. When the process terminates, the Godot thread will resume execution.” How is Godot supposed to use the shell output if the Godot thread is paused? Godot has a OS.kill() method that states that it uses the PID obtained from OS.execute() to kill the running program. But if the program is running, the godot thread is paused and can’t do anything. This doesn’t really make sense so I just set blocking to false and used other ways to get the script to stop.

Maybe I’m understanding all of this wrong. I don’t know how Windows shell stuff works so if I need to understand that first then it would be appreciated if somebody could give me a link to a tutorial for it or explain how it works. I also would really appreciate some help on how OS.execute works. Thank you!

Update: jgodfrey has provide a super helpful explanation of how OS.execute works. For me, the problem of not running the application correctly was fixed by exporting the godot project and running the exported instance. weird.

:bust_in_silhouette: Reply From: jgodfrey

While I haven’t used it, looking at the docs for Godot’s OS.execute - it looks like a pretty standard implementation of such a feature.

Let me try to answer some of your questions.

First, the application arguments.

The docs don’t explain much about these because they can’t. Those arguments have nothing to do with Godot and have everything to do with the application you’re calling via OS.execute(). Many applications support command line arguments, and this argument in the OS.execute() call allows you to define them for your called application. In the document example, a call is being made to the ping command and a few arguments are being passed into ping.

It may be easiest to understand by thinking about how you would successfully start your application from the command line. In the example, that would look like this:

ping -w 3 godotengine.org

So, ping is the command being called and the -w 3 godotengine.org are the arguments being passed to the ping command. Again, those have nothing to do with Godot, or the OS.execute() command. Rather, they’re arguments being passed into (and processed by) the ping command itself.

The OS.execute() command lets you define any such arguments using the arguments array. So, in the example, the above ping command is setup as follows in the OS.execute() call:

OS.execute("ping", ["-w", "3", "godotengine.org"], false)

The first arg is the command being called (ping). The second arg are any command line args that need to be passed to the command (including none). Finally, the 3rd arg defines blocking. In this case, blocking is false, so Godot will just execute the command and then continue running the code immediately following the OS.execute() line in your code.

Regarding the blocking setting and getting output from the executed command back in Godot…

If you set blocking arg to true, Godot will stop execution of your code while the executing program is running. Once that program exits, the exit code of the called program is returned as the result of the OS.execute() call. And, if you’ve provided the output arg (an Array) in the OS.execute() call, any output from the called program will be stored there once the program exits and your gdscript code starts executing again. To use the output that’s stored there, just access the array from your code.

If blocking is false in the OS.execute() call, it’s not possible to get the output of the called application (as your code isn’t waiting for the application to exit). Also, it’s not possible to get the exit code from the called application, again, because you’ve elected to NOT wait on the application to exit. Instead (as documented), the return value from the OS.execute() call will be the PID of the external process you started. With that value, you could potentially monitor the process status or use it to kill the process at a later time.

Hopefully that helps.

1 Like