-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
38 lines (31 loc) · 1.15 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import socket
import json
class UnrealEngine:
def __init__(self, host="127.0.0.1", port=12345):
self.host = host
self.port = port
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start_client(self):
try:
self.client_socket.connect((self.host, self.port))
self.client_socket.settimeout(3.0)
except ConnectionRefusedError:
print("Make sure the TCPServer Actor is loaded in the Unreal Engine Editor")
print("And game is running!!!")
def send_data(self, data: dict):
json_data = json.dumps(data)
self.client_socket.sendall(json_data.encode('utf-8'))
response = self.receive_response()
return response
def receive_response(self):
buffer_size = 4096
response_data = b""
while True:
part = self.client_socket.recv(buffer_size)
response_data += part
if len(part) < buffer_size:
break
return response_data.decode("utf-8")
def add_object(self, data: dict):
object_id = self.send_data(data)
return object_id