Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
RplTestComponentWrong.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Test", description: "Test component showcasing doing the replication from script the WRONG way")]
2 class SCR_RplTestComponentWrongClass : ScriptComponentClass
3 {
4 }
5 
7 {
8  [RplProp(condition: RplCondition.NoOwner)]
9  private int m_iTest = 0;
10  private int m_iTestLast = 0;
11 
12  float m_fdelay = 0;
13 
14  //------------------------------------------------------------------------------------------------
15  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
16  void NetTestRpc(int testNum)
17  {
18  Print("RPC RECEIVE: " + testNum, LogLevel.NORMAL);
19  }
20 
21  //------------------------------------------------------------------------------------------------
22  override void EOnFrame(IEntity owner, float timeSlice)
23  {
24  if (Debug.KeyState(KeyCode.KC_P))
25  {
26  Debug.ClearKey(KeyCode.KC_P);
27  int rndRPCNum = Math.RandomIntInclusive(0, 100);
28  // WRONG:
29  // When performed on server, this is broadcast to everyone.
30  // However when executed on client, this is only performed on the client.
31  Rpc(NetTestRpc, rndRPCNum);
32  Print("RPC SEND: " + rndRPCNum, LogLevel.NORMAL);
33  }
34 
35  /*
36  WRONG:
37  Never never never (unless you really have to).
38  If possible, never decide which RPC to call be checking for wheter you're
39  a client or not.
40  In general, if you need to write this condition it means your thought process is
41  not the best one and you should think things through again.
42  Instead, use proper attributes for the RPC. It takes a little bit of getting
43  used to, however, you'll end up with code which is cleaner and easier to understand.
44  This design actually enforces the right way of thinking on you.
45  */
46  if (RplSession.Mode() == RplMode.Client) // Client
47  {
48  /*
49  WRONG:
50  Never never never.
51  Always check for property changes on client using a callback.
52  You won't need to remeber the previous value + you'll avoid
53  cases where OnFrame isn't called for whatever reason (sometimes,
54  simulation of certain entites might turn off EOnFrame for proxies).
55 
56  In this case, every time m_iTest changes on sever, OnTestChanged
57  is called on client. The moment it's called, you know the value has
58  already been updated to a new one. If you want to keep the old one (
59  which you usually don't) you need to store it e.g. m_iTestLast or
60  in an array depending on your needs.
61  */
62  if (m_iTestLast != m_iTest)
63  Print("RPLPROP CHANGED: " + m_iTest, LogLevel.NORMAL);
64 
66  }
67  else
68  {
69  m_fdelay -= timeSlice;
70  if (m_fdelay > 0)
71  return;
72 
73  m_fdelay += 2;
74 
75  // Change the value server side
76  if (RplSession.Mode() != RplMode.Client)
77  {
78  m_iTest = Math.RandomIntInclusive(0, 100);
79  Replication.BumpMe();
80  }
81 
82  Print("RPLPROP CHANGE: " + m_iTest);
83  }
84  }
85 
86  //------------------------------------------------------------------------------------------------
87  void SCR_RplTestComponentWrong(IEntityComponentSource src, IEntity ent, IEntity parent)
88  {
89  SetEventMask(ent, EntityEvent.FRAME);
90  ent.SetFlags(EntityFlags.ACTIVE, true);
91  }
92 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
SCR_RplTestComponentWrong
void SCR_RplTestComponentWrong(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: RplTestComponentWrong.c:87
m_iTestLast
private int m_iTestLast
Definition: RplTestComponentWrong.c:10
m_fdelay
float m_fdelay
Definition: RplTestComponentWrong.c:12
EOnFrame
override void EOnFrame(IEntity owner, float timeSlice)
Definition: RplTestComponentWrong.c:22
NetTestRpc
void NetTestRpc(int testNum)
Definition: RplTestComponentWrong.c:16
SCR_RplTestComponentWrongClass
Definition: RplTestComponentWrong.c:2
m_iTest
private int m_iTest
Definition: RplTestComponent.c:16
RplProp
SCR_RplTestComponentWrongClass ScriptComponentClass RplProp(condition:RplCondition.NoOwner)] private int m_iTest=0
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180