Communication between Python and Godot using Networking

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

Hi guys,

I wonder how I can communicate Godot and Python. I am confused because I want to take some data from Godot and give them to Python so that I can use the data to process for machine learning. But I think I am only able to send/receive strings not screenshot or some values like distances, angles (in an array).

Is there a way to send/receive arrays of rgb values of game screen and sensor values and then use as is?

I need to record thousands of gameplays and this must be as fast as possible =)

… one more thing: I don’t know if that’s a good idea using Godot as environment for the purpose of machine learning. Is it?

Thanks in advance!

I do not know but I would like to find out as well. I’ve heard you do that using a network socket (like a TCP connecction).

johnygames | 2019-09-05 00:06

@Parjina I’m curious about your solution. I myself build TCP server in Godot and communicate in JSON in python. Then in python wrote a OpenAI Gym implementation to connect to Godot.

I managed to set physics to 240 fps which processed the communication but am still puzzled about performance.

clemens.tolboom | 2021-01-01 13:08

:bust_in_silhouette: Reply From: gmaps

In networking communication over sockets is quite common if you want performance, alternatives would be using higher level API’s, which I don’t recommend for your use case. So basically you would have to setup a TCP connection between godot and your python app. Create a python tcp server, and connect to it from godot, and then send wanted data. You’ll have to serialize the data (arrays and images) in godot and deserialize it on the python side, so don’t expect python to know what the data is out of the box. You could create a higher level API with mimetypes, and than it would understand what the contents are, you could send pictures and arrays in json, but it’s slower and you would have to have a rest framework (e.g. django rest framework). I suggest going with TCP connections.

Here are the tutorials for python tcp server and godot tcp client, they are very simple and easy to understand.

EDIT Use python for machine learning, godot is not meant for that. Python has big collection of machine learning libraries, godot is a game engine and thus, has none. There is some limited support for python, but I would avoid it for now and build a seperate system for ML.