1810 [
RplProp(onRplName:
"OnIValueChanged")]
1816 [
RplProp(customConditionName:
"MyCondition")]
1817 float fCustomCondition;
1819 void OnIValueChanged()
1834class EntityWithRplPropClass: GenericEntityClass {}
1835EntityWithRplPropClass g_EntityWithRplPropClassInst;
1842 void OwnerRpc(
int a)
1870class EntityWithRplRpcClass: GenericEntityClass {}
1871EntityWithRplRpcClass g_EntityWithRplRpcClassInst;
1880 WorldTimestamp m_Timestamp;
1902 static bool Extract(ComplexType instance, ScriptCtx ctx, SSnapSerializerBase snapshot)
1905 snapshot.SerializeBool(instance.m_Bool);
1906 snapshot.SerializeInt(instance.m_Int);
1907 snapshot.SerializeString(instance.m_String);
1908 snapshot.SerializeFloat(instance.m_Float);
1909 snapshot.SerializeBytes(instance.m_Timestamp, 8);
1910 snapshot.SerializeVector(instance.m_Vector);
1914 static bool Inject(SSnapSerializerBase snapshot, ScriptCtx ctx, ComplexType instance)
1917 snapshot.SerializeBool(instance.m_Bool);
1918 snapshot.SerializeInt(instance.m_Int);
1919 snapshot.SerializeString(instance.m_String);
1920 snapshot.SerializeFloat(instance.m_Float);
1921 snapshot.SerializeBytes(instance.m_Timestamp, 8);
1922 snapshot.SerializeVector(instance.m_Vector);
1926 static void Encode(SSnapSerializerBase snapshot, ScriptCtx ctx, ScriptBitSerializer packet)
1930 snapshot.EncodeBool(packet);
1931 snapshot.EncodeInt(packet);
1932 snapshot.EncodeString(packet);
1933 snapshot.EncodeFloat(packet);
1934 snapshot.Serialize(packet, 8);
1935 snapshot.EncodeVector(packet);
1938 static bool Decode(ScriptBitSerializer packet, ScriptCtx ctx, SSnapSerializerBase snapshot)
1942 snapshot.DecodeBool(packet);
1943 snapshot.DecodeInt(packet);
1944 snapshot.DecodeString(packet);
1945 snapshot.DecodeFloat(packet);
1946 snapshot.Serialize(packet, 8);
1947 snapshot.DecodeVector(packet);
1951 static bool SnapCompare(SSnapSerializerBase lhs, SSnapSerializerBase rhs , ScriptCtx ctx)
1959 return lhs.CompareSnapshots(rhs, 4+4)
1960 && lhs.CompareStringSnapshots(rhs)
1961 && lhs.CompareSnapshots(rhs, 4+8+12);
1964 static bool PropCompare(ComplexType instance, SSnapSerializerBase snapshot, ScriptCtx ctx)
1970 return snapshot.CompareBool(instance.m_Bool)
1971 && snapshot.CompareInt(instance.m_Int)
1972 && snapshot.CompareString(instance.m_String)
1973 && snapshot.CompareFloat(instance.m_Float)
1974 && snapshot.Compare(instance.m_Timestamp, 8)
1975 && snapshot.CompareVector(instance.m_Vector);
1978 static void EncodeDelta(SSnapSerializerBase oldSnapshot, SSnapSerializerBase newSnapshot, ScriptCtx ctx, ScriptBitSerializer packet)
1987 oldSnapshot.SerializeBool(oldBool);
1989 newSnapshot.SerializeBool(newBool);
1990 packet.Serialize(newBool, 1);
1995 oldSnapshot.SerializeInt(oldInt);
1997 newSnapshot.SerializeInt(newInt);
1998 int deltaInt = newInt - oldInt;
1999 packet.SerializeInt(deltaInt);
2005 oldSnapshot.SerializeString(oldString);
2007 newSnapshot.SerializeString(newString);
2008 bool stringChanged = newString != oldString;
2009 packet.Serialize(stringChanged, 1);
2011 packet.SerializeString(newString);
2014 oldSnapshot.SerializeFloat(oldFloat);
2016 newSnapshot.SerializeFloat(newFloat);
2017 bool floatChanged = newFloat != oldFloat;
2018 packet.Serialize(floatChanged, 1);
2020 packet.Serialize(newFloat, 32);
2028 WorldTimestamp oldTimestamp;
2029 oldSnapshot.SerializeBytes(oldTimestamp, 8);
2030 WorldTimestamp newTimestamp;
2031 newSnapshot.SerializeBytes(newTimestamp, 8);
2032 float deltaMs = newTimestamp.DiffMilliseconds(oldTimestamp);
2033 bool isUsingDelta = -16777216.0 <= deltaMs && deltaMs <= 16777216.0;
2034 packet.Serialize(isUsingDelta, 1);
2037 int deltaMsInt = deltaMs;
2038 packet.SerializeInt(deltaMsInt);
2042 packet.Serialize(newTimestamp, 64);
2046 oldSnapshot.SerializeVector(oldVector);
2048 newSnapshot.SerializeVector(newVector);
2049 bool vectorChanged = newVector != oldVector;
2050 packet.Serialize(vectorChanged, 1);
2052 packet.Serialize(newVector, 96);
2060 static void DecodeDelta(ScriptBitSerializer packet, ScriptCtx ctx, SSnapSerializerBase oldSnapshot, SSnapSerializerBase newSnapshot)
2069 oldSnapshot.SerializeBool(oldBool);
2071 packet.Serialize(newBool, 1);
2072 newSnapshot.SerializeBool(newBool);
2076 oldSnapshot.SerializeInt(oldInt);
2078 packet.SerializeInt(deltaInt);
2079 int newInt = oldInt + deltaInt;
2080 newSnapshot.SerializeInt(newInt);
2085 oldSnapshot.SerializeString(oldString);
2087 packet.Serialize(stringChanged, 1);
2090 packet.SerializeString(newString);
2092 newString = oldString;
2093 newSnapshot.SerializeString(newString);
2096 oldSnapshot.SerializeFloat(oldFloat);
2098 packet.Serialize(floatChanged, 1);
2101 packet.Serialize(newFloat, 32);
2103 newFloat = oldFloat;
2104 newSnapshot.SerializeFloat(newFloat);
2106 WorldTimestamp oldTimestamp;
2107 oldSnapshot.SerializeBytes(oldTimestamp, 8);
2109 packet.Serialize(isUsingDelta, 1);
2110 WorldTimestamp newTimestamp;
2114 packet.SerializeInt(deltaMsInt);
2115 float deltaMs = deltaMsInt;
2116 newTimestamp = oldTimestamp.PlusMilliseconds(deltaMs);
2120 packet.Serialize(newTimestamp, 64);
2122 newSnapshot.SerializeBytes(newTimestamp, 8);
2125 oldSnapshot.SerializeVector(oldVector);
2127 packet.Serialize(vectorChanged, 1);
2130 packet.Serialize(newVector, 96);
2132 newVector = oldVector;
2133 newSnapshot.SerializeVector(newVector);
2139class RplExampleDebugShapeClass: GenericEntityClass {}
2140RplExampleDebugShapeClass g_RplExampleDebugShapeClassInst;
2144 static const int COLOR_COUNT = 4;
2145 static const int COLORS[] = {
2154 void RplExampleDebugShape(IEntitySource src,
IEntity parent)
2161 vector worldTransform[4];
2166 bool SetColorByIdx(
int colorIdx)
2168 if (colorIdx < 0 || colorIdx >= COLOR_COUNT)
2179RplExample1ComponentColorAnimClass g_RplExample1ComponentColorAnimClass;
2185 private static const float COLOR_CHANGE_PERIOD_S = 5.0;
2189 private float m_TimeAccumulator_s;
2192 private int m_ColorIdx;
2199 auto shapeEnt = RplExampleDebugShape.Cast(owner);
2202 Print(
"This example requires that the entity is of type `RplExampleDebugShape`.",
LogLevel.WARNING);
2207 shapeEnt.SetColorByIdx(m_ColorIdx);
2218 int colorIdxDelta = CalculateColorIdxDelta(timeSlice);
2219 ApplyColorIdxDelta(owner, colorIdxDelta);
2222 private int CalculateColorIdxDelta(
float timeSlice)
2226 m_TimeAccumulator_s += timeSlice;
2227 int colorIdxDelta = m_TimeAccumulator_s / COLOR_CHANGE_PERIOD_S;
2231 m_TimeAccumulator_s -= colorIdxDelta * COLOR_CHANGE_PERIOD_S;
2233 return colorIdxDelta;
2236 private void ApplyColorIdxDelta(
IEntity owner,
int colorIdxDelta)
2239 if (colorIdxDelta == 0)
2243 int newColorIdx = (m_ColorIdx + colorIdxDelta) % RplExampleDebugShape.COLOR_COUNT;
2247 if (newColorIdx == m_ColorIdx)
2251 m_ColorIdx = newColorIdx;
2254 RplExampleDebugShape.Cast(owner).SetColorByIdx(m_ColorIdx);
2261RplExample2ComponentColorAnimClass g_RplExample2ComponentColorAnimClass;
2265 private static const float COLOR_CHANGE_PERIOD_S = 5.0;
2267 private float m_TimeAccumulator_s;
2273 [
RplProp(onRplName:
"OnColorIdxChanged")]
2274 private int m_ColorIdx;
2280 auto shapeEnt = RplExampleDebugShape.Cast(owner);
2283 Print(
"This example requires that the entity is of type `RplExampleDebugShape`.",
LogLevel.WARNING);
2287 shapeEnt.SetColorByIdx(m_ColorIdx);
2291 auto rplComponent = BaseRplComponent.Cast(shapeEnt.FindComponent(BaseRplComponent));
2294 Print(
"This example requires that the entity has an RplComponent.",
LogLevel.WARNING);
2302 if (rplComponent.Role() ==
RplRole.Authority)
2311 int colorIdxDelta = CalculateColorIdxDelta(timeSlice);
2312 ApplyColorIdxDelta(owner, colorIdxDelta);
2315 private int CalculateColorIdxDelta(
float timeSlice)
2317 m_TimeAccumulator_s += timeSlice;
2318 int colorIdxDelta = m_TimeAccumulator_s / COLOR_CHANGE_PERIOD_S;
2319 m_TimeAccumulator_s -= colorIdxDelta * COLOR_CHANGE_PERIOD_S;
2320 return colorIdxDelta;
2324 private void ApplyColorIdxDelta(
IEntity owner,
int colorIdxDelta)
2326 if (colorIdxDelta == 0)
2329 int newColorIdx = (m_ColorIdx + colorIdxDelta) % RplExampleDebugShape.COLOR_COUNT;
2330 if (newColorIdx == m_ColorIdx)
2334 m_ColorIdx = newColorIdx;
2340 Replication.BumpMe();
2343 RplExampleDebugShape.Cast(owner).SetColorByIdx(m_ColorIdx);
2347 private void OnColorIdxChanged()
2349 RplExampleDebugShape.Cast(
GetOwner()).SetColorByIdx(m_ColorIdx);
2357RplExample3ComponentColorAnimClass g_RplExample3ComponentColorAnimClass;
2361 [
RplProp(onRplName:
"OnColorIdxChanged")]
2362 private int m_ColorIdx;
2366 auto shapeEnt = RplExampleDebugShape.Cast(owner);
2369 Print(
"This example requires that the entity is of type `RplExampleDebugShape`.",
LogLevel.WARNING);
2373 shapeEnt.SetColorByIdx(m_ColorIdx);
2375 auto rplComponent = BaseRplComponent.Cast(shapeEnt.FindComponent(BaseRplComponent));
2378 Print(
"This example requires that the entity has an RplComponent.",
LogLevel.WARNING);
2385 m_ColorIdx = (m_ColorIdx + 1) % RplExampleDebugShape.COLOR_COUNT;
2386 Replication.BumpMe();
2387 RplExampleDebugShape.Cast(
GetOwner()).SetColorByIdx(m_ColorIdx);
2390 private void OnColorIdxChanged()
2392 RplExampleDebugShape.Cast(
GetOwner()).SetColorByIdx(m_ColorIdx);
2397RplExample3SystemClass g_RplExample3SystemClassInst;
2401 static const ResourceName s_ControllerPrefab =
"{65B426E2CD4049C3}kroslakmar/RplExampleController.et";
2402 static const ResourceName s_SpherePrefab =
"{1AD0012447ACCE3F}kroslakmar/RplExampleShape.et";
2404 ref RplExample3SessionListener m_SessionListener =
new RplExample3SessionListener(
this);
2405 ref map<RplIdentity, RplExample3Controller> m_Controllers =
new map<RplIdentity, RplExample3Controller>();
2407 ref array<RplExample3ComponentColorAnim> m_Spheres =
new array<RplExample3ComponentColorAnim>();
2417 RplMode mode = RplSession.Mode();
2420 RplSession.RegisterCallbacks(m_SessionListener);
2425 RplExample3Controller controller = NewController(
RplIdentity.Local());
2426 controller.RplGiven(null);
2429 Resource prefab = Resource.Load(s_SpherePrefab);
2431 spawnParams.TransformMode = ETransformMode.WORLD;
2433 float xBase = spawnParams.Transform[3][0];
2434 float yBase = spawnParams.Transform[3][1] + 2.0;
2435 for (
int y = -1; y <= 1; y++)
2436 for (
int x = -1; x <= 1; x++)
2438 spawnParams.Transform[3][0] = xBase + x;
2439 spawnParams.Transform[3][1] = yBase + y;
2441 m_Spheres.Insert(RplExample3ComponentColorAnim.Cast(
2447 RplExample3Controller NewController(
RplIdentity identity)
2449 ref Resource controllerPrefab = Resource.Load(s_ControllerPrefab);
2450 auto controller = RplExample3Controller.Cast(
2453 controller.m_System =
this;
2454 m_Controllers.Set(identity, controller);
2461 auto controller = m_Controllers.Get(identity);
2463 m_Controllers.Remove(identity);
2466 void ChangeColor(
int idx)
2468 m_Spheres[
idx].NextColor();
2474 RplExample3System m_System;
2476 void RplExample3SessionListener(RplExample3System system)
2483 RplExample3Controller controller = m_System.NewController(identity);
2484 auto rplComponent = BaseRplComponent.Cast(controller.FindComponent(BaseRplComponent));
2485 rplComponent.Give(identity);
2488 override void EOnDisconnected(
RplIdentity identity)
2490 m_System.DeleteController(identity);
2494class RplExample3ControllerClass : GenericEntityClass {}
2495RplExample3ControllerClass g_RplExample3ControllerClassInst;
2499 static const KeyCode s_KeyMap[] = {
2511 RplExample3System m_System;
2512 int m_IsDownMask = 0;
2514 bool RplGiven(ScriptBitReader reader)
2531 int keyBit = 1 <<
idx;
2532 bool isDown =
Debug.KeyState(kc);
2533 bool wasDown = (m_IsDownMask & keyBit);
2534 if (isDown && !wasDown)
2535 Rpc(Rpc_ChangeColor_S,
idx);
2538 m_IsDownMask |= keyBit;
2540 m_IsDownMask &= ~keyBit;
2545 void Rpc_ChangeColor_S(
int idx)
2550 m_System.ChangeColor(
idx);
2557 foreach (
KeyCode kc : s_KeyMap)
2559 if (
Debug.KeyState(kc))
2560 isDownMask |= keyBit;
2564 Rpc(Rpc_OwnerInputs_S, isDownMask);
2568 void Rpc_OwnerInputs_S(
int isDownMask)
2570 int inputsChanged = m_IsDownMask ^ isDownMask;
2576 int keyBit = 1 <<
idx;
2577 bool isDown = isDownMask & keyBit;
2578 bool wasDown = m_IsDownMask & keyBit;
2579 if (isDown && !wasDown)
2580 m_System.ChangeColor(
idx);
2583 m_IsDownMask = isDownMask;
RplMode
Mode of replication.
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
enum EVehicleType IEntity
proto external IEntity SpawnEntityPrefab(notnull Resource templateResource, BaseWorld world=null, EntitySpawnParams params=null)
proto external bool InPlayMode()
proto external int SetEventMask(notnull IEntity owner, int mask)
void Rpc(func method, void p0=NULL, void p1=NULL, void p2=NULL, void p3=NULL, void p4=NULL, void p5=NULL, void p6=NULL, void p7=NULL)
proto external EntityEvent SetEventMask(EntityEvent e)
proto external Managed FindComponent(typename typeName)
void EOnFrame(IEntity owner, float timeSlice)
proto external void GetWorldTransform(out vector mat[])
See IEntity::GetTransform.
proto external BaseWorld GetWorld()
void EOnFixedFrame(IEntity owner, float timeSlice)
void EOnFrame(IEntity owner, float timeSlice)
proto external GenericEntity GetOwner()
Get owner entity.
void EOnInit(IEntity owner)
Game g_Game
Game singleton instance.
IEntity GetOwner()
Owner entity of the fuel tank.
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
EntityEvent
Various entity events.
RplRole
Role of replicated node (and all items in it) within the replication system.
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
RplCondition
Conditional replication rule. Fine grained selection of receivers.
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.