How do you call a python script ingame and receive the console output live?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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?

:bust_in_silhouette: Reply From: Jamam150

As far as I know, you can’t modify or call scripts in-game from your player. Sorry!

:bust_in_silhouette: Reply From: Wakatta

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:

OS — Godot Engine (stable) documentation in English

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.

Wakatta | 2021-03-07 04:01