+1 vote

I would like to know how to send a screenshot of the game to a python program through a socket connection.

I already know how to get the image:

var img = getviewport().gettexture().getdata()
yield(get
tree(),"idle_frame")

The UDP interconnection is ok, but the tcp for to sent the data is not connecting. My first trial was to sent a string, where my python server is:

import socket

HOST = '127.0.0.1'
PORT = 4343

sock = socket.socket(socket.AFINET, socket.SOCKSTREAM)
orig = (HOST, PORT)
sock.bind((HOST,PORT))
sock.listen()
print("Listening")
conn, addr = sock.accept()
print ('Connected to: ', addr)

while conn:
data = conn.recv(5)
if len(data) == 0:
break
print("Received: ", data.decode('utf-8'))
sock.close()

My godot client is:

var socketTCP

var thread
var msg = ">"
var ip
var port

func _ready():
thread = Thread.new()
socketTCP = StreamPeerTCP.new()

icon.position.x = get_viewport().size.x/2
icon.position.y = get_viewport().size.y/2
randomize()
vel_x = rand_range(5,20)
vel_y = rand_range(5,20)

print("v_x: " + str(vel_x))
print("v_y: " + str(vel_y))
move_obj.wait_time = interval
move_obj.start()

if thread.is_active():
    print("thread already active")
else:
    print("starting thread ...")

thread.start(self,"connect_server","values")

print("Thread ID: " + str(thread.get_id()))

if thread.is_active():
    print("thread is working!!!")
else:
    print("thread not working!!!")

pass

func connect_server(values):
var done = false

ip = "127.0.0.1"
port = 4343

socketTCP.set_no_delay(true)

var connection
while !done:
    print("connecting ...")
    connection = socketTCP.connect_to_host("127.0.0.1",4343)
    done = socketTCP.is_connected_to_host()
    print(done)

socketTCP.put_utf8_string("Test of connection")

socketTCP.disconnect_from_host()
print("Connection finnished!!!")

Any idea of how to sent the img through this connection?

in Engine by (13 points)

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.