• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Synchronization problems :<

Status
Not open for further replies.
Level 13
Joined
Sep 13, 2010
Messages
550
JASS:
library ValSync

    globals
        gamecache SyncA = InitGameCache( "SyncA" )
        integer AsyncP = 0
        boolean IsSync = false
    endglobals
    
    function ReadSyncVal takes string StoreCode returns integer
        debug if not IsSync then
            debug call DisplayTimedTextToPlayer( GetLocalPlayer( ) , 0 , 0 , 60 , "|cffffff00[Synchronization]: You should sync before reading data.|r" )
        debug endif
        return GetStoredInteger( SyncA , "0" , I2S( GetStoredInteger( SyncA , StoreCode , "0" ) ) )
    endfunction
    
    function ExecSync takes player varOwner returns nothing
        if not IsSync then
            set IsSync = true
            call TriggerSyncStart( )
                if GetLocalPlayer( ) == varOwner then
                    loop
                        exitwhen AsyncP == 0
                        call SyncStoredInteger( SyncA , "0" , I2S( AsyncP ) )
                        set AsyncP = AsyncP - 1
                    endloop
                endif
            call TriggerSyncReady( )
        debug else
            debug call DisplayTimedTextToPlayer( GetLocalPlayer( ) , 0 , 0 , 60 , "|cffff0000[Synchronization]: You have already synchronized!|r" )
        endif
    endfunction
    
    function AddSyncVal takes string StoreCode , integer Var returns nothing
        if not IsSync then
            if "0" != StoreCode then
                set AsyncP = AsyncP + 1
                call StoreInteger( SyncA , StoreCode , "0" , AsyncP )
                call StoreInteger( SyncA , "0" , I2S( AsyncP ) , Var )
            debug else
                debug call DisplayTimedTextToPlayer( GetLocalPlayer( ) , 0 , 0 , 60 , "|cffff0000[Synchronization]: You are unable to use the \"0\" as a store string.|r" )
            endif
        debug else
            debug call DisplayTimedTextToPlayer( GetLocalPlayer( ) , 0 , 0 , 60 , "|cffff0000[Synchronization]: Sync table is read only! Clear it before start adding values.|r" )
        endif
    endfunction
    
    function ClearSync takes nothing returns nothing
        set AsyncP = 0
        set IsSync = false
        call FlushGameCache( SyncA )
        set SyncA = InitGameCache( "SyncA" )
    endfunction
    
    // Safe calls
    
    globals
        player ValSync_TempP = null
    endglobals
    
    function ExecSyncFunc takes nothing returns nothing
        if ValSync_TempP != null then
            call ExecSync( ValSync_TempP )
        endif
    endfunction
    
    function ExecSync_Safe takes player VarOwner returns nothing
        set ValSync_TempP = VarOwner
        call ExecuteFunc( "ExecSyncFunc" )
    endfunction

endlibrary
This shoulda work.. But ( how surprising ) it doesn't. It doesn't work if I remove TriggerSyncStart things or try other variations... Variables remain local as they was.( Multiplayer tested )
 
Level 13
Joined
Sep 13, 2010
Messages
550
Share also some test code which use this library.

JASS:
scope TEST initializer Start

globals
    private boolean TES = false
    private integer array AT
    private integer Inte = 0
endglobals

private function SyncP takes nothing returns nothing
    if TES then
        call ExecSync_Safe( Player( 1 ) )
    else
        call ExecSync_Safe( Player( 0 ) )
    endif
    loop
        if ReadSyncVal( I2S( Inte ) ) == Inte:AT then
            call BJDebugMsg( "Matching integer: " + I2S( Inte:AT ) )
        else
            call BJDebugMsg( "|cffff0000NOT Matching integer: " + I2S( Inte:AT ) + " != " + I2S( ReadSyncVal( I2S( Inte ) ) ) )
        endif
        set Inte:AT = 0
        set Inte = Inte - 1
        exitwhen Inte == 0
    endloop
    set TES = not TES
    call ClearSync( )
endfunction

private function ASEFA takes nothing returns nothing
    local integer i = GetRandomInt( 1 , 10000 )
    set Inte = Inte + 1
    set Inte:AT = i
    if TES then
        if GetLocalPlayer( ) == Player( 0 ) then
            set i = 0
        endif
    else
        if GetLocalPlayer( ) == Player( 1 ) then
            set i = 0
        endif
    endif
    call AddSyncVal( I2S( Inte ) , i )
endfunction

//===========================================================================
private function Start takes nothing returns nothing
    call TimerStart( CreateTimer( ) , 0.03 , true , function ASEFA )
    call TimerStart( CreateTimer( ) , 0.6 , true , function SyncP )
endfunction

endscope
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
I'm not sure the TriggerSync... functions work correctly if it's not called inside a trigger action, even through an ExecuteFunc call.
That won't work in a trigger condition at least, since you can't wait there.

Plus, for the first attempt, i suppose it would be better to inline the sync stuff without any library required, and if it works then try to make a sync library.
 
Last edited:
Status
Not open for further replies.
Top