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.
- 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)andent:SetPhotonNet_<name>(value)on the server, andPhoton.SNet:Get<name>andent: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
- namestringVariable name to use.
- netType`Photon.SNet.BOOL`|`Photon.SNet.INT`|`Photon.SNet.STR`|`Photon.SNet.UINT`The wire type to send the value as.
- extrainteger?Bit width, for
Photon.SNet.INTandPhoton.SNet.UINT; unused by the others.
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
- stringThe prefixed field name, e.g.
PhotonNet_SirenOn.
- 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
- anyThe networked value, or
default.
- 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
- 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
- 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
- ServerPhoton.SNet:SendQueuedResync(ply, count)Source ☍
-
Drain up to
countvehicles 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.ValueChangedhook 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
namemust be pre-registered withPhoton.SNet:Map.Parameters
- entEntityThe entity to change the value on.
- namestringThe registered name to change.
- valanyValue to set.
Reading
Sending
- SharedPhoton.SNet.ReadFunctionsSource ☍
-
The
netreader used for eachPhotonNetType.Internal
This function is internal. Whilst you can call it, you probably shouldn't.
- SharedPhoton.SNet.WriteFunctionsSource ☍
-
The
netwriter used for eachPhotonNetType.Internal
This function is internal. Whilst you can call it, you probably shouldn't.
- SharedPhoton.SNetSource ☍
- SharedPhoton.SNetSource ☍
- SharedPhoton.SNet.BitsSource ☍
-
Bit width currently needed to send a variable index, kept in step with
Photon.SNet.FMapbyPhoton.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 ofPhoton.SNet.FMap.Internal
This function is internal. Whilst you can call it, you probably shouldn't.
- 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 theThinkhook below.Internal
This function is internal. Whilst you can call it, you probably shouldn't.
Resync Queue
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.