js function synchronizer

For AI and campaign script related discussions and questions
Post Reply
wanjia1
Rookie
Rookie
Posts: 27
Joined: 01 Nov 2023, 06:05

js function synchronizer

Post by wanjia1 »

some js function,like addDroid,is not synchronized. if it not called at all clients at the same tick,it will cause desync.
although the function syncRequest(req_id, x, y, object, object2) products a synchronized eventSyncRequest(from,req_id, x, y, obj_id, obj_id2)
it can only carry arguments of fixed length and type,

so i make this library, serialize the arguments of non-synchronized function(which must have fixed name and json serializable arguments) and wrapped in syncRequest send it to all clients, make it synchronized.
for example, make addDroid a synchronized function:

Code: Select all

include("multiplay/script/mods/sync.js");
var syncAddDroid=synchronize(addDroid)//must be executed in all clients
below is a demo mod that use this library.
when a player says "/tank", a "Gauss Cannon Hydra Dragon Hover" will be spawned at its startPositions. and it works in multiplay.
Attachments
synced.zip
license:CC0
(2.59 KiB) Downloaded 37 times
Last edited by wanjia1 on 06 Jan 2024, 07:42, edited 2 times in total.
wanjia1
Rookie
Rookie
Posts: 27
Joined: 01 Nov 2023, 06:05

Re: js function synchronizer

Post by wanjia1 »

screenshot:
Attachments
syncAddDroid.jpg
wanjia1
Rookie
Rookie
Posts: 27
Joined: 01 Nov 2023, 06:05

Re: js function synchronizer

Post by wanjia1 »

its implement is inefficient. theoretically, a syncRequest can carry 24 bytes of data (req_id, x, y of type number in float64. object and object2 have constraints and unable to carry data),
but the implement only use the req_id to carry data, the charcode of every character of the json serialized argument, which take 1 byte at most cases.
the x is also used. it carry the position of charcode. because it's not sure if the eventSyncRequest called in order.
it also have no validation. it assumes there is no packet loss or errors, cheating. validation should be implemented in the function that to be synchronized.
Last edited by wanjia1 on 06 Jan 2024, 21:16, edited 1 time in total.
wanjia1
Rookie
Rookie
Posts: 27
Joined: 01 Nov 2023, 06:05

Re: js function synchronizer

Post by wanjia1 »

when nested synchronized function is called, only the outer-most function call will product synchronized events, and will no return value.
the inner function call is short-circuited and will execute at local client, returns as normal.
this avoided duplicate event.
Post Reply