Topic was automatically imported from the old Question2Answer platform.
Asked By
PixelRayn
I am currently writing a coding game in which the player needs to write and modify scripts. I would like to have these executed with python, since that reduces the load on me. For these to be executed I have two options:
OS.execute(), or to require it through Gadot native javascript. It does however have the significant downside of not allowing live output of data.
I have also read about GodoPy but as far as I understand i can not have the user call scripts with it. Is there any prebuild solution to execute python scripts in a confined environment from within godot with live console output?
OS.execute() is the least complicated of the two as it requires no setup and Just that you have a portable version of Python attached to your game (so as to not trouble your end users with “cant run missing python please download” messages).
func python_call():
var stdout = []
var exit = OS.execute("python3", ["script.py"], true, stdout)
if exit == OK:
do_stuff_with(stdout)
Note: Its advised to run this function in a separate thread as i doubt you can get an output without blocking which would hang the main thread
The problem with this answer is, that the thread is halted until the program is executed and only then the output can be processed instead of live processing:
If blocking is set to false we will receive no output at all. Another big downside of this is the fact that it would run the scripts on the players computer without restrictions thus allowing them to break out of the game. It would work though.
PixelRayn | 2021-02-08 07:35
True this option only simulates the act of “live processing” and that would become noticeable during long processing or scripts that they themselves call OS type functions.
thus allowing them to break out of the game
You don’t want your players to exit the game? Are you making a virtual Jumanji?
If your have the technical know how you can build python as a library for Godot using GDnative. I compiled it for Android despite their restrictions so it should be easily achievable.