@lagless/create 0.0.57 → 0.0.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -199,6 +199,51 @@ for (const rpc of rpcs) {
|
|
|
199
199
|
|
|
200
200
|
The key difference: server events have `rpc.meta.playerSlot === SERVER_SLOT` (255).
|
|
201
201
|
|
|
202
|
+
## String Data in RPCs
|
|
203
|
+
|
|
204
|
+
RPCs use numeric fields (`uint8`, `float32`, etc.), but sometimes you need to send string data (e.g., username in `PlayerJoined`). Use `encodeStringToUint8` / `decodeStringFromUint8` from `@lagless/binary` with `uint8[N]` array fields.
|
|
205
|
+
|
|
206
|
+
**Encoding:** BMP-only UTF-16, fixed 2 bytes per character. Supports Latin, Cyrillic, CJK, Arabic, Greek. Emoji (non-BMP) are replaced with `?`.
|
|
207
|
+
|
|
208
|
+
### Schema
|
|
209
|
+
|
|
210
|
+
```yaml
|
|
211
|
+
inputs:
|
|
212
|
+
PlayerJoined:
|
|
213
|
+
slot: uint8
|
|
214
|
+
username: uint8[64] # 64 bytes = 32 characters max
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Sending (server hook)
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { encodeStringToUint8 } from '@lagless/binary';
|
|
221
|
+
|
|
222
|
+
onPlayerJoin(ctx, player) {
|
|
223
|
+
const { buffer } = encodeStringToUint8(player.username, 64);
|
|
224
|
+
ctx.emitServerEvent(PlayerJoined, { slot: player.slot, username: buffer });
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Reading (system)
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
import { decodeStringFromUint8 } from '@lagless/binary';
|
|
232
|
+
|
|
233
|
+
const rpcs = this._input.collectTickRPCs(tick, PlayerJoined);
|
|
234
|
+
for (const rpc of rpcs) {
|
|
235
|
+
const username = decodeStringFromUint8(rpc.data.username as Uint8Array);
|
|
236
|
+
// username is a JS string, e.g. "Игрок_123"
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Key Points
|
|
241
|
+
|
|
242
|
+
- `maxBytes` must be even (2 bytes per character). `maxBytes / 2` = max characters.
|
|
243
|
+
- `encodeStringToUint8` returns `{ buffer: Uint8Array, truncated: boolean }`.
|
|
244
|
+
- Truncation never splits a character — always clean cut.
|
|
245
|
+
- Buffer is zero-padded; `decodeStringFromUint8` stops at first null (`0x0000`) or end of buffer.
|
|
246
|
+
|
|
202
247
|
## Adding a New Input Type
|
|
203
248
|
|
|
204
249
|
1. **Schema** — Add to `ecs.yaml`:
|
|
@@ -85,6 +85,7 @@ const server = new RelayGameServer({
|
|
|
85
85
|
config: {
|
|
86
86
|
maxPlayers: 4,
|
|
87
87
|
reconnectTimeoutMs: 15_000,
|
|
88
|
+
inputRecordingEnabled: true, // enable replay export
|
|
88
89
|
},
|
|
89
90
|
hooks,
|
|
90
91
|
inputRegistry: MyInputRegistry,
|
|
@@ -200,6 +201,23 @@ ctx.emitServerEvent(InputClass, data); // Send server-originated RPC
|
|
|
200
201
|
ctx.getPlayers(); // Get all player info
|
|
201
202
|
ctx.endMatch(results); // End the match
|
|
202
203
|
ctx.roomId; // Room identifier
|
|
204
|
+
ctx.exportRecordedInputs(); // RPCHistory binary (requires inputRecordingEnabled)
|
|
205
|
+
ctx.exportReplay(); // Full replay binary (seed + maxPlayers + fps + RPCHistory)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Replay Export
|
|
209
|
+
|
|
210
|
+
Enable `inputRecordingEnabled: true` in server config. All broadcast inputs (client + server events) are stored. Export in `onMatchEnd`:
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
onMatchEnd: async (ctx, results) => {
|
|
214
|
+
const replay = ctx.exportReplay();
|
|
215
|
+
if (replay) {
|
|
216
|
+
// replay is ArrayBuffer — save to DB, file, or cloud storage
|
|
217
|
+
// Load later via ReplayInputProvider.createFromReplay(replay, inputRegistry)
|
|
218
|
+
await saveReplay(ctx.matchId, replay);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
203
221
|
```
|
|
204
222
|
|
|
205
223
|
## Server Events via emitServerEvent
|
|
@@ -100,6 +100,39 @@ Tags have no fields — they only occupy a bitmask bit (zero memory per entity).
|
|
|
100
100
|
}
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
+
## Send String Data in RPCs (e.g. Username)
|
|
104
|
+
|
|
105
|
+
RPCs only support numeric fields. To send strings, use `uint8[N]` array fields with `encodeStringToUint8` / `decodeStringFromUint8`.
|
|
106
|
+
|
|
107
|
+
1. Edit `ecs.yaml`:
|
|
108
|
+
```yaml
|
|
109
|
+
inputs:
|
|
110
|
+
PlayerJoined:
|
|
111
|
+
slot: uint8
|
|
112
|
+
username: uint8[64] # 64 bytes = 32 characters max
|
|
113
|
+
```
|
|
114
|
+
2. Run `pnpm codegen`
|
|
115
|
+
3. Encode on send (server hook):
|
|
116
|
+
```typescript
|
|
117
|
+
import { encodeStringToUint8 } from '@lagless/binary';
|
|
118
|
+
|
|
119
|
+
onPlayerJoin(ctx, player) {
|
|
120
|
+
const { buffer } = encodeStringToUint8(player.username, 64);
|
|
121
|
+
ctx.emitServerEvent(PlayerJoined, { slot: player.slot, username: buffer });
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
4. Decode in system:
|
|
125
|
+
```typescript
|
|
126
|
+
import { decodeStringFromUint8 } from '@lagless/binary';
|
|
127
|
+
|
|
128
|
+
const rpcs = this._input.collectTickRPCs(tick, PlayerJoined);
|
|
129
|
+
for (const rpc of rpcs) {
|
|
130
|
+
const username = decodeStringFromUint8(rpc.data.username as Uint8Array);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Supports:** Latin, Cyrillic, CJK, Arabic, Greek. **No emoji** — replaced with `?`.
|
|
135
|
+
|
|
103
136
|
## Add a New Entity Type
|
|
104
137
|
|
|
105
138
|
1. Define components and filter in `ecs.yaml`:
|
|
@@ -82,6 +82,22 @@ for (const rpc of rpcs) {
|
|
|
82
82
|
const buffer = inputProvider.getFrameRPCBuffer(tick);
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
## String Encoding (for RPCs)
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { encodeStringToUint8, decodeStringFromUint8 } from '@lagless/binary';
|
|
89
|
+
|
|
90
|
+
// Encode string into fixed-size buffer (BMP UTF-16, 2 bytes/char)
|
|
91
|
+
const { buffer, truncated } = encodeStringToUint8('Игрок', 64); // 32 chars max
|
|
92
|
+
|
|
93
|
+
// Decode buffer back to string
|
|
94
|
+
const str = decodeStringFromUint8(buffer); // 'Игрок'
|
|
95
|
+
|
|
96
|
+
// Use with uint8[N] RPC fields:
|
|
97
|
+
addRPC(PlayerJoined, { slot: 0, username: buffer });
|
|
98
|
+
const name = decodeStringFromUint8(rpc.data.username as Uint8Array);
|
|
99
|
+
```
|
|
100
|
+
|
|
85
101
|
## Signals
|
|
86
102
|
|
|
87
103
|
```typescript
|