Skip to main content

TSMPEncoder

TSMPEncoder builds a TSMP frame from synchronized behaviours, queued RPC calls, and the selected codec. It writes the result to one output RenderTexture.

Use this page when you need to drive encoding from code, inspect frame counters, or send a TSMP RPC.

Component fields

FieldPurpose
outputRender texture that receives the encoded frame. The texture size defines the final frame size.
frameRateMaximum automatic encode rate when autoEncode is enabled.
autoEncodeEncodes on the component update loop. Disable it when another script calls EncodeNow().
clearAfterEncodeClears unused frame pixels after writing the current frame. Enable this when changing codecs or payload size frequently.
selectedCodecCodec component used to convert payload bytes into pixels. Luma4 is the default codec.
useBlockSymbolTextureUses the codec block texture path when the selected codec supports it.
networkBehavioursBound behaviours that can contribute [TransSync] data and TSMP RPC messages.
transRpcRepeatFramesNumber of additional frames that keep a queued RPC visible. Increase this for lossy capture paths.
streamIdLogical stream identifier written into the frame header.
layoutIdLogical layout identifier written into the frame header.

TSMPSetup normally fills binding and codec fields. Manual editing is useful only for custom tools or test scenes.

Diagnostics

MemberMeaning
EncodedObjectCount / encodedObjectCountNumber of variable source behaviours encoded in the last frame.
QueuedRpcCount / queuedRpcCountNumber of RPC messages waiting to be encoded.
PayloadBytes / payloadBytesBytes written into the last payload.
usablePayloadBytesPayload capacity after the frame header and codec layout are considered.
messageCountVariable messages plus RPC messages in the last network frame.
variableMessageCountNumber of variable state messages.
rpcMessageCountNumber of RPC messages.
FrameIndex / frameIndexMonotonic frame counter written to the header.
lastEncodeStageShort text identifier for the last encode stage.
LastError / lastErrorLast encoder error. Runtime builds also log important failures.

EncodeNow()

public void EncodeNow()

Encodes one frame immediately. It is safe to call with autoEncode disabled.

Typical uses:

  • A debug button that forces a frame.
  • A capture pipeline that has its own timing.
  • An editor-time preview tool.

EncodeNow() returns without writing a frame when there is no variable data and no queued RPC data.

ResetFrameIndex()

public void ResetFrameIndex()

Resets the frame counter used in the header and diagnostics. This is useful for a clean capture or a reproducible test.

QueueRpc() and QueueRpcHash()

public void QueueRpc(ushort networkId, string rpcName, params object[] arguments)
public void QueueRpcHash(ushort networkId, uint rpcHash, params object[] arguments)

Queues a lower-level RPC message. Most user components should prefer TSMPNetworkBehaviour.SendTransRPC() because it already knows the behaviour network ID.

Arguments should use primitive TSMP value types. For high-frequency or complex data, pack the data into a [TransSync] byte[] field instead of sending many RPC arguments.

QueueTransRpc()

public void QueueTransRpc(int networkId, uint rpcHash, string methodName)

Queues a TSMP RPC by network ID and method hash. This is the encoder-side entry point used by TSMPNetworkBehaviour.SendTransRPC().

The queue is written into subsequent frames according to transRpcRepeatFrames, so a single event can survive short frame drops in the texture path.

Raw variable writer API

These members are intended for advanced tools and generated code:

public bool BeginFrame()
public void ClearFrame()
public bool BeginVariableState(int networkId)
public bool WriteRawBytesVariable(uint variableHash, byte[] value)
public bool EndVariableState()
public bool BeginRpcCall(int networkId, uint rpcHash)
public bool WriteStringRpcArgument(string value)
public bool WriteInt32RpcArgument(int value)
public bool EndRpcCall()
public void CancelCurrentMessage()

Use this flow for a custom variable message:

if (encoder.BeginFrame() &&
encoder.BeginVariableState(networkId) &&
encoder.WriteRawBytesVariable(variableHash, payload))
{
encoder.EndVariableState();
}
else
{
encoder.CancelCurrentMessage();
}

For normal components, [TransSync] is simpler and less error-prone.

Runtime rules

  • The encoder should not know optional codec packages by type. It talks to the selected TSMPCodec.
  • The output texture should be large enough for the selected codec and payload size.
  • A payload overflow logs a warning and drops the over-budget message.
  • RPC-only frames are valid. A frame can contain no variable messages and still carry queued RPC messages.