Syncing issues + MouseScroll

saw792

Is known to say things. That is all.
Reaction score
280
I'm working on a system to detect mouse scrolls using changes in the camera of each player. Naturally this is local data so it must be synced. The Sync library below is based on TSync by Toadcop.

Sync Library:
JASS:
library Sync initializer Init

  globals
    private integer DATA = 0
    private player P = null
    private trigger T = CreateTrigger()
    private unit array U
    private string ZERO = "Z"
    private integer ZEROABIL = 'A000'
    private string ONE = "X"
    private integer ONEABIL = 'A001'
    private integer TOSYNC = 0
    private integer DUMMYID = 'h00E'
    private group SELECTED = CreateGroup()
    private boolexpr FILTER
  endglobals
  
  private function Filt takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) != DUMMYID
  endfunction
  
  function SyncInt takes integer i, player p returns nothing
    call GroupEnumUnitsSelected(SELECTED, GetLocalPlayer(), FILTER)
    if GetLocalPlayer() ==  p then
      set TOSYNC = i
      call ClearSelection()
      call SelectUnit(U[GetPlayerId(p)], true)
    endif
  endfunction
  
  function GetSyncedInt takes nothing returns integer
    return DATA
  endfunction
  
  function GetSyncedPlayer takes nothing returns player
    return P
  endfunction
  
  function SyncAddCallback takes boolexpr callback returns nothing
    call TriggerAddCondition(T, callback)
  endfunction
  
  private function SyncFinish takes nothing returns boolean
    if GetSpellAbilityId() == ZEROABIL then
      set DATA = 0
    elseif GetSpellAbilityId() == ONEABIL then
      set DATA = 1
    endif
    set P = GetOwningPlayer(GetTriggerUnit())
    return true
  endfunction
  
  private function SyncHandler takes nothing returns boolean
    if TOSYNC == 1 then
      call ForceUIKey(ONE)
    else
      call ForceUIKey(ZERO)
    endif
    call ClearSelection()
    call ForGroup(SELECTED, function SelectGroupBJEnum)
    call GroupClear(SELECTED)
    return false
  endfunction
  
  private function Init takes nothing returns nothing
    local integer i = 11
    
    set FILTER = Condition(function Filt)
    loop
      set U<i> = CreateUnit(Player(i), DUMMYID, 0, 0, 0)
      if GetLocalPlayer() == Player(i) then
        call TriggerRegisterUnitEvent(T, U<i>, EVENT_UNIT_SELECTED)
      endif
      exitwhen i == 0
      set i = i - 1
    endloop
    call TriggerAddCondition(T, Condition(function SyncHandler))
    
    set T = CreateTrigger()
    
    loop
      exitwhen i == 12
      call TriggerRegisterUnitEvent(T, U<i>, EVENT_UNIT_SPELL_CAST)
      set i = i + 1
    endloop
    
    call TriggerAddCondition(T, Condition(function SyncFinish))
    
  endfunction
  
endlibrary</i></i></i>
Scroll Library:
JASS:
library Scroll initializer Init requires Sync, Event
  
  globals
    private real PERIOD = 0.02
  endglobals
  
  globals
    private real DEFAULT_DISTANCE = 0
    private camerasetup CAMSETUP = CreateCameraSetup()
    private integer array PLAYERFIRE
    private Event E
    private boolean array B
  endglobals
  
  function TriggerRegisterPlayerScrollEvent takes trigger t, player p returns nothing
    set E.register(t).data = GetPlayerId(p)
  endfunction
  
  function GetScrollingPlayer takes nothing returns player
    return Player(E.getTriggeringEventReg().data)
  endfunction
  
  private function Fire takes nothing returns boolean
    local integer i = GetPlayerId(GetSyncedPlayer())
    
    set PLAYERFIRE<i> = GetSyncedInt()
    call BJDebugMsg(I2S(PLAYERFIRE<i>))
    if PLAYERFIRE<i> != 0 then
      set PLAYERFIRE<i> = 0
      set B<i> = true
      call E.fire()
    endif
    return true
  endfunction
  
  private function Detect takes nothing returns nothing
    local integer i = 11
      
    loop
      if GetLocalPlayer() == Player(i) then
        if GetCameraField(CAMERA_FIELD_TARGET_DISTANCE) != DEFAULT_DISTANCE then
          call CameraSetupApply(CAMSETUP, false, false)
          set PLAYERFIRE<i> = PLAYERFIRE<i> + 1
          if B<i> then
            call SyncInt(PLAYERFIRE<i>, Player(i))
            set B<i> = false
          endif
        endif
      endif
      exitwhen i == 0
      set i = i - 1
    endloop
  endfunction
  
  private function Init takes nothing returns nothing
    local integer i = 11
    
    set E = Event.create()
    call SyncAddCallback(Condition(function Fire))
    
    loop
      set PLAYERFIRE<i> = 0
      set B<i> = true
      exitwhen i == 0
      set i = i - 1
    endloop
    
    set DEFAULT_DISTANCE = GetCameraField(CAMERA_FIELD_TARGET_DISTANCE)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_TARGET_DISTANCE, DEFAULT_DISTANCE, 0)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_FARZ, GetCameraField(CAMERA_FIELD_FARZ), 0)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_ZOFFSET, GetCameraField(CAMERA_FIELD_ZOFFSET), 0)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_ANGLE_OF_ATTACK, bj_RADTODEG * GetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK), 0)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_FIELD_OF_VIEW, bj_RADTODEG * GetCameraField(CAMERA_FIELD_FIELD_OF_VIEW), 0)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_ROLL, bj_RADTODEG * GetCameraField(CAMERA_FIELD_ROLL), 0)
    call CameraSetupSetField(CAMSETUP, CAMERA_FIELD_ROTATION, bj_RADTODEG * GetCameraField(CAMERA_FIELD_ROTATION), 0)
     
    call TimerStart(CreateTimer(), PERIOD, true, function Detect)
  endfunction
  
endlibrary</i></i></i></i></i></i></i></i></i></i></i></i>


Despite my syncing efforts it's still desyncing, although not on the first event fire (usually the second or third). I'm having trouble figuring out what could be causing it as the local variables are all 'globalised' correctly. It's possible that the SetUnitOwner() call being run locally from the SyncInt() function is causing issues but other than a dodgy timer there seems to be no way to stop calling it locally from the Scroll library (as I only want it to sync when the event fires for a player).

Any tips/things I'm forgetting to sync that you can see would be helpful.

Please don't post things like 'Check GetLocalPlayer() blocks' lest you face my wrath :p

Thanks
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
JASS:
private real PERIOD = 0.02


You can't sync local data so fastly, use it each 1 second or so.
So in fact it can't be used like that.
 

saw792

Is known to say things. That is all.
Reaction score
280
Please read the code first. It doesn't sync every 0.02 seconds. It syncs only when the event actually occurs, and only when the previous occurence of the event has been synced already. I already stated that the actual data gets passed over correctly.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
When i worked on IsWar3Minimized i got a desync when i used GroupEnumUnitsSelected locally, it's the case here with SyncInt.
(Needs to be confirmed though, i've fixed the desync quickly)
 

saw792

Is known to say things. That is all.
Reaction score
280
Righto. How exactly did you fix the desync (or did you just remove the GroupEnum call)?

Hmm... could it have something to do with the filter function and boolexpr tables desyncronising? Not sure if that's possible.
 

saw792

Is known to say things. That is all.
Reaction score
280
Neither do I. I tested it with that line commented out, and no desync. I'm thinking the Sync library will need a SyncReady type function that does that.
 

saw792

Is known to say things. That is all.
Reaction score
280
Updated code, the SelectUnit was definitely causing a desync. It still desyncs however, just slightly later (only when the event is fired fairly quickly). Right now I'm still thinking it has something to do with the group (which I now realise will actually cause conflicts for multiple players as it isn't instant... but I'll fix that part later). Unfortunately I can only test it occasionally, so I've updated the code in case somebody else has an idea.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
ForceUI is lame, because it has delay, you can't call it many times in a short interval, it's not silent the player will notice it, plus it could be fucked up with a customkey.txt.
The only good thing seems to select dummies units, it's still not instant but the player can't notice it, but theoretically a player unit selection could be partially fucked up.

Currently i also need to sync local reals, i'm trying to use the gamecache which will be totally silent, but maybe slower.
If i win vs gamecache i will tell you, else i would submit a snippet with dummies units selection stuff anyway.
 

saw792

Is known to say things. That is all.
Reaction score
280
My syncing code is extremely quick, actually. About the same speed as an arroy key event, and much faster than gamecache syncing is.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
My syncing code is extremely quick, actually. About the same speed as an arroy key event, and much faster than gamecache syncing is.
But it doesn't work :p

Also i hate the ForceUI key thing since it's not silent, the user will notice it.
Even if the gamecache is slower i still prefer something totally silent.
But if we can't synch 12 local datas in less than 1 second (one for each player) with gamecache, then i will go with dummies units selection stuff.
 

saw792

Is known to say things. That is all.
Reaction score
280
It totally works... sortof not really...

Well the group isn't the problem. I'm totally depressed now :p
Still desyncs when group code is commented out.
 

saw792

Is known to say things. That is all.
Reaction score
280
I'm well aware I evaluate the trigger locally :p That's the entire point of the system, it wouldn't work otherwise. It's the same method used in TSync.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Finally i've made it with GameCache sync stuff.


JASS:
library SyncDatas initializer onInit

globals
    private trigger array Trig
    private gamecache array GC
    private timer array Tim
    private force Players
    private integer I
endglobals

private function Actions takes nothing returns nothing
    local integer i = I
    
    if GetLocalPlayer() == Player(i) then
        call StoreReal(GC[0],I2S(i),&quot;0&quot;,GetCameraTargetPositionX())
    endif
    call TriggerSyncStart()
    
    if GetLocalPlayer() == Player(i) then
        call SyncStoredReal(GC[0], I2S(i), &quot;0&quot;)
    endif

    call TriggerSyncReady()

    call BJDebugMsg(&quot;time elapsed == &quot; + R2S(TimerGetElapsed(Tim[0])))
    call BJDebugMsg(&quot;data of Player( &quot; + I2S(i) + &quot;) == &quot; + R2S(GetStoredReal(GC[0],I2S(i),&quot;0&quot;)))
    call BJDebugMsg(&quot; &quot;)
endfunction

private function Callback takes nothing returns nothing
    set I = GetPlayerId(GetEnumPlayer())
    call TriggerExecute(Trig<i>)
endfunction

private function DoTheSync takes nothing returns nothing
    call ForForce(Players,function Callback)
    call TimerStart(Tim[0],10.,false,null)
endfunction

private function onInit takes nothing returns nothing
    local trigger trig = CreateTrigger()
    local integer i = 0
    
    call TriggerRegisterPlayerChatEvent(trig,Player(0),&quot;test&quot;,true)
    call TriggerAddAction(trig,function DoTheSync)
    set Players = CreateForce()
    set GC[0] = InitGameCache(SCOPE_PREFIX)
    set Tim[0] = CreateTimer()
    
    loop
    exitwhen i == 12
    
        if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i)) == MAP_CONTROL_USER then
            call ForceAddPlayer(Players,Player(i))
            set Trig<i> = CreateTrigger()
            call TriggerAddAction(Trig<i>,function Actions)
            
        endif
    
    set i = i+1
    endloop
    

endfunction

endlibrary</i></i></i>


The needed time is about 0.5 s for 2 players and a good host.
It might be higher with a crappy connection and maybe also with more players.
Would be great if someone could test it with more players and a crappy host connection.

Ofc it's just a sample, not a snippet.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top