Photon Documentation Repository

Photon.SNet

Regular Simplified Networking. Easily optimise networking for setting local values on entities.

Registering a variable with Photon.SNet:Map generates a matching setter and getter on both this table and the Entity metatable, and networks every change to it automatically. The generated entity methods are listed on the Entity page.

Functions

SharedPhoton.SNet:Map(name, netType, extra)Source ☍

Register a variable to be networked, under a name and a wire type.

Generates four accessors for it: Photon.SNet:Set<name>(ent, value) and ent:SetPhotonNet_<name>(value) on the server, and Photon.SNet:Get<name> and ent:GetPhotonNet_<name> on both realms. Re-registering an existing name updates its type in place and keeps its index, so the wire format does not shift under a client that is already connected.

Warning

Both realms have to register the same names in the same order: indices are what go over the wire, not names.

Parameters

Example

Photon.SNet:Map("SirenOn", Photon.SNet.BOOL)
Photon.SNet:Map("SirenSet", Photon.SNet.UINT, 10)
SharedPhoton.SNet.Normalise(name) → stringSource ☍

The key a registered variable is cached under on an entity.

Parameters

  • namestringRegistered variable name.

Returns

  1. stringThe prefixed field name, e.g. PhotonNet_SirenOn.

Reading

SharedPhoton.SNet:Get(ent, name, default) → anySource ☍

Get the latest cached value of a networked variable on an entity. Reads the client's local copy; it never asks the server for a fresh one.

Parameters

  • entEntityThe entity to read the value from.
  • namestringThe registered name to read.
  • defaultanyValue to return when nothing has been networked yet.

Returns

  1. anyThe networked value, or default.

Sending

ServerPhoton.SNet:CollectFields(ent) → table?Source ☍

Collect every currently-set networked variable on an entity.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Parameters

  • entEntityEntity to read the values from.

Returns

  1. table?Array of {idx, netType, extra, val}, or nil if nothing is set.
ServerPhoton.SNet:QueueResync(ply, targets)Source ☍

Queue vehicles to have their full networked state sent to a player. Values are only broadcast when they change, so a client that has just become aware of an entity has to pull the current state explicitly.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Parameters

  • plyPlayerPlayer to send the current state to.
  • targetsEntity[]Array of entities to queue.
ServerPhoton.SNet:SendChange(ent, name, val, to)Source ☍

Send the current value of a networked variable to a recipient.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Parameters

  • entEntityThe entity the value belongs to.
  • namestringThe registered name to send.
  • valanyValue to send.
  • totable|Player|nilPlayer (or players) to send to; broadcasts to everyone if omitted.
ServerPhoton.SNet:SendQueuedResync(ply, count)Source ☍

Drain up to count vehicles from a player's queue into one net message. Draining over several frames keeps any single message well under the 64KiB net limit, which a whole-map resync could otherwise exceed.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Parameters

  • plyPlayerPlayer to send to.
  • countintegerMaximum number of vehicles to process this frame.
ServerPhoton.SNet:Set(ent, name, val)Source ☍

Change a networked entity variable, broadcasting it if it actually moved.

Runs the Photon.SimpleNet.ValueChanged hook with (name, old, new, ent) once the change has been sent, mirroring what the client does on receipt. A value that has not moved broadcasts nothing and runs nothing, so a listener sees a given change once per realm rather than once per write.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Warning

name must be pre-registered with Photon.SNet:Map.

Parameters

  • entEntityThe entity to change the value on.
  • namestringThe registered name to change.
  • valanyValue to set.

Tables

SharedPhoton.SNet.ReadFunctionsSource ☍

The net reader used for each PhotonNetType.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

SharedPhoton.SNet.WriteFunctionsSource ☍

The net writer used for each PhotonNetType.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Classes

SharedPhoton.SNetSource ☍

Fields

SharedPhoton.SNetSource ☍
SharedPhoton.SNet.BitsSource ☍

Bit width currently needed to send a variable index, kept in step with Photon.SNet.FMap by Photon.SNet:Map.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

SharedPhoton.SNet.FLOATSource ☍
SharedPhoton.SNet.FMapSource ☍

Registered variables by index, as {name, netType, extra}. This is the order indices are sent in, so an entry's position is part of the wire format for as long as both realms are running the same build.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

SharedPhoton.SNet.RMapSource ☍

Registered variables by name, as {index, netType, extra} — the reverse of Photon.SNet.FMap.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Resync Queue

ServerPhoton.SNet.ResyncQueueSource ☍

Pending full-state sends, keyed by player, as {list = {ent, ...}, set = {[ent] = true}, pos = <next index to send>}. Drained a few entities per frame by the Think hook below.

Internal

This function is internal. Whilst you can call it, you probably shouldn't.

Network Types

Photon.SNet.* · 4 values Source ☍

The wire types a variable can be registered as, passed to Photon.SNet:Map. Each one picks the net library reader/writer pair the value is sent with.

SharedPhoton.SNet.BOOLSource ☍

A boolean, sent as one bit. Takes no extra data.

SharedPhoton.SNet.INTSource ☍

A signed integer. The extra data is its bit width.

SharedPhoton.SNet.UINTSource ☍

An unsigned integer. The extra data is its bit width.

SharedPhoton.SNet.STRSource ☍

A string, sent null-terminated. Takes no extra data.