OS Methods questions

Godot Version

4.2

Question

I’m working on a chess game that can communicate with a process that acts as a server and a chess engine. Being relatively new to Godot, I needed confirmation on two things:

  1. Does OS.CreateProcess() require the executable file to exist outside the exported game? Or, can I use the “res//…” path somehow?
  2. Assuming I use OS.CreateProcess() with an apk made by Godot, can I pass arguments and use OS.GetCmdlineArgs() in the apk to get them?

When you say apk, are you on Android?

Yes. I tagged the question on Android…

1 Like

I found this run command in res:// folder on android · Issue #16379 · godotengine/godot · GitHub

1 Like

I see. So I must include any binary executable file in the export filters when exporting to Android. I will test to see If I can create a process using ProjectSettings.globalize_path(“res://”) path.
And do you know anything about my second question?

I would hope so, yes. I would do it that way.

It didn’t work. Both when I used
gamePath = OS.GetExecutablePath().GetBaseDir();
executable = gamePath.PathJoin(“Bin/UdpServer/ServerTest.exe”);
OS.CreateProcess(executable , System.Array.Empty(), true);
Or
gamePath = ProjectSettings.GlobalizePath(“res://”);
executable = gamePath + “Bin/UdpServer/ServerTest.exe”;
OS.CreateProcess(executable , System.Array.Empty(), true);

I’m not sure .exe can run on Android? Is that binary compiled in the architecture of the target os?

Also the difference between create process and execute. One will create a new process the other a child thread. IDK if Android app permissions have specific applications here?

Do you have any errors?

I tested on Windows with .exe file using
gamePath = ProjectSettings.GlobalizePath(“res://”);
executable = gamePath + “Bin/UdpServer/ServerTest.exe”;

and then with
gamePath = OS.GetExecutablePath().GetBaseDir();
executable = gamePath.PathJoin(“Bin/UdpServer/ServerTest.exe”);


and I did set the export filters to include .exe file

That line of code listed in the error on os_windows_cpp:851 is making a windows function call CreateProcessW. So it seems like windows didn’t like the parameters you provided, or there was another issue.

If you execute the parameters on the command line yourself, what happens?

Try using the second example:

“C:\Users\<?>\Desktop\UdpServer\ServerTest.exe”

Your username seems like it isn’t utf compatible with Godot?

You can’t use a res:// path to launch a binary with OS.create_process(). In most platforms the path is read-only when exported or it’s a pck file. You’ll need to extract the executable to a path in the system.

For example:

extends Node


func _ready() -> void:
	copy_file('res://test_json.json', 'user://test_json.json')


func copy_file(from:String, to:String) -> void:
	var in_file = FileAccess.open(from, FileAccess.READ)
	var out_file = FileAccess.open(to, FileAccess.WRITE)
	out_file.store_buffer(in_file.get_buffer(in_file.get_length()))
	in_file.close()
	out_file.close()

Note: DirAccess.copy() or DirAccess.copy_absolute() don’t work because the file is inside a pck file.

But, anyway, Godot already has a way to create an UDP server with UDPServer

Godot Engine provides the OS class for various platform-specific functionalities, including process creation. However, Godot’s OS class does not directly expose methods for redirecting a process’s standard input, output, or error streams.

If you need to interact with the standard input of a process in Godot, you may need to consider alternative approaches, such as using custom inter-process communication (IPC) mechanisms or modifying the source code of the external process if you have control over it.

That’s why I need to create a process that acts as a server. This is the method I saw on this webpage on Godot chess:
https://gdscript.com/projects/chess-in-godot/

Also, your answer is similar to previous question in this topic:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.