Skip to content

Commit 87ca5ea

Browse files
committedJul 3, 2018
修正客户端插件updateHZ调整后, 线程的更新频率没有匹配上
kbengine/kbengine#653
1 parent 9518e0d commit 87ca5ea

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed
 

‎KBEMain.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class KBEMain : MonoBehaviour
1919
public int port = 20013;
2020
public KBEngineApp.CLIENT_TYPE clientType = KBEngineApp.CLIENT_TYPE.CLIENT_TYPE_MINI;
2121
public string persistentDataPath = "Application.persistentDataPath";
22-
public bool syncPlayer = true;
22+
public int syncPlayerMS = 100;
2323
public int threadUpdateHZ = 10;
2424
public int serverHeartbeatTick = 15;
2525
public int SEND_BUFFER_MAX = (int)KBEngine.NetworkInterface.TCP_PACKET_MAX;
@@ -61,7 +61,7 @@ public virtual void initKBEngine()
6161
else
6262
args.persistentDataPath = persistentDataPath;
6363

64-
args.syncPlayer = syncPlayer;
64+
args.syncPlayerMS = syncPlayerMS;
6565
args.threadUpdateHZ = threadUpdateHZ;
6666
args.serverHeartbeatTick = serverHeartbeatTick;
6767
args.useAliasEntityID = useAliasEntityID;

‎KBEngine.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public struct ServerErr
133133
private System.DateTime _lastTickCBTime = System.DateTime.Now;
134134
private System.DateTime _lastUpdateToServerTime = System.DateTime.Now;
135135

136+
//上传玩家信息到服务器间隔,单位毫秒
137+
private float _updatePlayerToServerPeroid = 100.0f;
138+
private const int _1MS_TO_100NS = 10000;
139+
136140
// 玩家当前所在空间的id, 以及空间对应的资源
137141
public UInt32 spaceID = 0;
138142
public string spaceResPath = "";
@@ -157,7 +161,8 @@ public KBEngineApp(KBEngineArgs args)
157161
public virtual bool initialize(KBEngineArgs args)
158162
{
159163
_args = args;
160-
164+
_updatePlayerToServerPeroid = (float)_args.syncPlayerMS;
165+
161166
initNetwork();
162167

163168
// 注册事件
@@ -1914,26 +1919,26 @@ public void Client_onControlEntity(Int32 eid, sbyte isControlled)
19141919
}
19151920

19161921
/*
1917-
更新当前玩家的位置与朝向到服务端, 可以通过开关_syncPlayer关闭这个机制
1922+
更新当前玩家的位置与朝向到服务端, 可以通过开关_syncPlayerMS关闭这个机制
19181923
*/
19191924
public void updatePlayerToServer()
19201925
{
1921-
if(!_args.syncPlayer || spaceID == 0)
1926+
if(_updatePlayerToServerPeroid <= 0.01f || spaceID == 0)
19221927
{
19231928
return;
19241929
}
19251930

19261931
var now = DateTime.Now;
19271932
TimeSpan span = now - _lastUpdateToServerTime;
19281933

1929-
if (span.Ticks < 1000000)
1930-
return;
1934+
if (span.Ticks < _updatePlayerToServerPeroid * _1MS_TO_100NS)
1935+
return;
19311936

19321937
Entity playerEntity = player();
19331938
if (playerEntity == null || playerEntity.inWorld == false || playerEntity.isControlled)
19341939
return;
19351940

1936-
_lastUpdateToServerTime = now - (span - TimeSpan.FromTicks(1000000));
1941+
_lastUpdateToServerTime = now - (span - TimeSpan.FromTicks(Convert.ToInt64(_updatePlayerToServerPeroid * _1MS_TO_100NS)));
19371942

19381943
Vector3 position = playerEntity.position;
19391944
Vector3 direction = playerEntity.direction;

‎KBEngineArgs.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public class KBEngineArgs
2222
public string persistentDataPath = "";
2323

2424
// Allow synchronization role position information to the server
25-
// 是否开启自动同步玩家信息到服务端,信息包括位置与方向
25+
// 是否开启自动同步玩家信息到服务端,信息包括位置与方向,毫秒
2626
// 非高实时类游戏不需要开放这个选项
27-
public bool syncPlayer = true;
27+
public int syncPlayerMS = 100;
2828

2929
// 是否使用别名机制
3030
// 这个参数的选择必须与kbengine_defs.xml::cellapp/aliasEntityID的参数保持一致

0 commit comments

Comments
 (0)
Please sign in to comment.