+2 votes

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!

in Engine by (67 points)

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).

@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.

1 Answer

0 votes
Best answer

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.

https://realpython.com/python-sockets/
https://www.gamefromscratch.com/page/Godot-3-Tutorial-Networking.aspx
https://stackoverflow.com/questions/27241804/sending-a-file-over-tcp-sockets-in-python

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.

by (699 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.