# Unreal Engine Socket Communication

## 0. Environment

Windows 10

Unreal Engine 5.1.1

SteamVR 1.25.8

## 1. Create a new Unreal project

Select "Film / Video & Live Events" -> "Blank". Leave "Starter Content" and "Raytracing" unchecked.

Click "Create" button.

<figure><img src="/files/AGYu5PtxkhGxsNwIwi5z" alt=""><figcaption></figcaption></figure>

## 2. Add plugin

Click "Edit" -> "Plugins" to open the plugins window.

<figure><img src="/files/d8HdDniE4FZeAlmoYn5Z" alt=""><figcaption></figcaption></figure>

Search and add the following plugin.

<figure><img src="/files/xZHVtnguarkgrKVSVD0Y" alt=""><figcaption></figcaption></figure>

After adding the plugin, Unreal Engine needs to be restarted.

## 3. Set up a new GameModeBase class

Click the Blueprint button, select "New Empty Blueprint Class...".

<figure><img src="/files/EyM0PukpoORsCXvgYOmb" alt=""><figcaption></figcaption></figure>

Choose "GameModeBase" as the parent class.

<figure><img src="/files/ICWbzVTikl1TTP5x1oZ7" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Wvy9vaywm832Cr4dfhpK" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/fvRvLlmKstmw1KzLhJbI" alt=""><figcaption></figcaption></figure>

Finally, update the GameModeBase class

<figure><img src="/files/0etAsbBWiuY9kI0pJW4I" alt=""><figcaption></figcaption></figure>

## 4. External Python server

```python
import socket
import struct

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 8080))
s.listen(1)

conn, addr = s.accept()

print("connected by", addr)

while True:
    data = conn.recv(1024)

    print("recv:", data)

    content = "hello Unreal!"

    buffer = struct.pack(">BB", 1, len(content))
    buffer += content.encode()

    conn.sendall(buffer)
    print("sent:", buffer)

    break

s.close()

```

### A Note on FPS

Since we might want to use EventTick to transmit messages, it could be useful if we limit event tick rate on the gamemode base.

To set the rate, go to Blueprint setting of the GameModeBase, select "GameModeBase" on the Components view. Then, on the Details panel, change the "Tick Interval" value to desired value.

<figure><img src="/files/fRBPbDhzhhjBNYB0ef9M" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tk233.gitbook.io/notes/uw/unreal-engine-socket-communication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
