[0.8.9][NK] NetStream

johnycilohoklajohnycilohokla REGISTERED Posts: 283 Seed
edited December 2015 in Modding Tutorials

NetStream - Data serialization

Data serialization should only be used for values that change over time and don't need to be ordered, serialization guarantees that the last packet will be received, but if packets get lost or arrive out of order only the most recent ones will be deserialized.

Examples: health, energy, score, durability, amount of stored liquid/energy, etc...

NetStream is order sensitive, the write order has to be the read order!


List all of the functions that can be used on the stream to read/write data, those can be used with the first argument (NetSteam) of serialize (write) and deserialize (read) functions:
		Serialize			Deserialize			Type					Example
NKWriteBool NKReadBool type: Boolean true/false
NKWriteDouble NKReadDouble type: Double 1234.12345
NKWriteInt NKReadInt type: Integer 1234
NKWriteString NKReadString type: String "abc"
NKWriteBuffer NKReadBuffer type: String(size=256) "abdef..."
NKWriteVec3 NKReadVec3 type: Vec3 vec3
NKWriteQuat NKReadQuat type: Quat quat
NKWriteGameObject NKReadGameObject type: GameObject GameObject
NKWriteObjectId NKReadObjectId type: ObjectID Network id
Usage: stream:NKWrite*(value) value = stream:NKRead*()


NetSerializeConstruction is called only on the server side, it will be called only once per new client when the client connects, it is used to send the required data for construction (to NetDeserializeConstruction).
function Example:NetSerializeConstruction( stream, connection )
-- Example.__super.NetSerializeConstruction(self, stream, connection)
-- Example: stream:NKWriteString(self.variable)
NetDeserializeConstruction is called only on the client side, it will be called when the client receives the serialized construction data from the server, this happens only once.
function Example:NetDeserializeConstruction( stream )
-- Example.__super.NetDeserializeConstruction(self, stream)
-- Example: self.variable = stream:NKReadString()
NetSerialize is called only on the server side, it will be called every * seconds, if the data has changed it will send the serialized data to client, all of the client that can see the object will receive the data and pass it into NetDeserialize function.
function Example:NetSerialize( stream )
-- Example.__super.NetSerialize(self, stream)
-- Example: stream:NKWriteString(self.variable)
NetDeserialize is called only on the client side, it will be called when the client receives the serialized data from the server.
function Example:NetDeserialize( stream )
-- Example.__super.NetDeserialize(self, stream)
-- Example: self.variable = stream:NKReadString()


NetSerialize example (Class PlaceableObject)
function PlaceableObject:NetSerialize(netWriter)
-- PlaceableObject.__super.NetSerialize(self, netWriter)

netWriter:NKWriteInt(self:GetMaxStackSize()) -- write integer max stack size
netWriter:NKWriteInt(self:GetStackSize()) -- write integer stack size
NetDeserialize example (Class PlaceableObject)
function PlaceableObject:NetDeserialize(netReader)
-- PlaceableObject.__super.NetDeserialize(self, netReader)

self:SetMaxStackSize(netReader:NKReadInt()) -- read integer max stack size
self:SetStackSize(netReader:NKReadInt()) -- read integer stack size
NetSerialize example (Mixin ConsumesFuel)
function ConsumesFuel:NetSerialize(netWriter)
netWriter:NKWriteDouble(self.m_fuelTimer) -- write double m_fuelTimer
NetDeserialize example (Mixin ConsumesFuel)
function ConsumesFuel:NetDeserialize(netReader)
self.m_fuelTimer = netReader:NKReadDouble() -- read double m_fuelTimer

if self.m_fuelTimer <= 0.0 then -- check the time if it is below or equal to 0
self.m_fuelTimer = 0.0 -- set the time to 0
self:OnDeactivate() -- deactivate the CraftingStation (Fire Pit/Stone Furnace)
My Blog
  yt.png  t.png  
Sign In or Register to comment.