-
Notifications
You must be signed in to change notification settings - Fork 0
Replicated Objects
These are the special objects that are available on the server and the client. They share data and provide a remote procedure call (RPC) interface.
As explained in the setup tutorial these must be declared in a special directory called net_objects
. You may only declare one object per file, the object is created for you and you just have to describe it. The name is the same in every file and will always be OBJECT
.
For example the following code is a valid replicated object declaration.
OBJECT.Properties = {
NotReplicatedProperty = "Hi I am not replicated !",
ReplicatedStringWithCallback = { Replicates = true, Default = "toto",
OnRep = function (self)
print("ReplicatedStringWithCallback was modified !")
end
},
ReplicatedBool = { Replicates = true, Default = true },
ReplicatedInt = { Replicates = true, Default = 45 },
ReplicatedFloat = { Replicates = true, Default = 12.345 }
}
OBJECT.NetRPCs = {
TestRpc = {
OnMaster = function(self, a, b, c)
print("master received TestRpc(" .. a .. ", " .. b .. ", " .. c .. ")")
return true
end,
OnProxy = function(self, a, b, c)
print("proxy received TestRpc(" .. a .. ", " .. b .. ", " .. c .. ")")
end
}
}
function OBJECT:OnActivate()
self.NetRPCs.TestRpc(12, 24, "test")
end
This is the only place where you should declare variables, everything outside of Properties
will be removed from the object. Properties can be replicated or not, as you can see in the sample above a property can be declared directly with a value which is equivalent to { Default = value }
, the following fields are allowed :
- Default: Sets the default value when an object is created, the allowed types are number, string and boolean, anything else will not work for now.
- Replicates: Set to true if you want the property to replicate on clients, otherwise every instance controls the value.
-
OnRep: Can be bound to a
function(self)
and will be called automatically when the property is updated.
Note: A property has a static type, meaning if you set a number as a default, you can only set a number to the value.
You access a property exactly the same way you declared it, for example if the declared type is
OBJECT.Properties = {
Test = { Default = 42, Replicates = true }
}
It can be accessed like so
myInstance.Properties.Test = 54
print(myInstance.Properties.Test) -- prints 54
Remote Procedure Calls act as any function you would call in Lua except they are executed on the server first and then can be replicated to clients. When declaring a RPC you actually declare two functions OnMaster
(required) and OnProxy
(optional). OnMaster
will execute on the server only and returns a boolean, if it returns true the OnProxy
function will be called on all connected clients with the same parameters, if it returns false nothing more happens.
Note: The parameter types are limited to numbers, strings and booleans for now.
You call a RPC exactly the same way you declared it, for example if the declared type is
OBJECT.NetRPCs= {
DisplaySomeValues = {
OnMaster = function(self, a, b, c)
print("master received TestRpc(" .. a .. ", " .. b .. ", " .. c .. ")")
return true
end,
OnProxy = function(self, a, b, c)
print("proxy received TestRpc(" .. a .. ", " .. b .. ", " .. c .. ")")
end
}
}
It can be accessed like so
myInstance.NetRPCs.DisplaySomeValues(123, "hello", true)
Some member functions are special and will be called automatically :
function OBJECT:OnActive()
-- Called when an instance comes alive, server only
end
function OBJECT:OnDeactive()
-- Called when an instance is destroyed, server only
end
function OBJECT:Tick(delta)
-- Called every frame with the delta time in seconds, server & client
end