library Thread /* v1.0.0.0
*************************************************************************************
*
* This script allows one to sync up threads between players. It is most useful for
* preventing players from desyncing due to heavy local code blocks as well as syncing
* up large streams of data.
*
************************************************************************************
*
* SETTINGS
*/
globals
/*
* This unit can be retrieved from the map attached to this thread. Copy and paste
* the unit in the map into target map and then put the unit type id of that unit here.
*/
private constant integer UNIT_SYNC_ID = 'h001'
endglobals
/*
************************************************************************************
*
* struct Thread
*
* readonly boolean synced
* - Loop until this is true
*
* static method create takes nothing returns Thread
* method destroy takes nothing returns nothing
*
* method sync takes nothing returns nothing
* - Call from local player when that local player's operation is complete
*
* Examples
*
* local Thread thread = Thread.create()
*
* loop
* if (GetLocalPlayer() == targetPlayer) then
* //the hefty operation here should segment how much it does per iteration
* //too much per iteration and slower computers will desync from the sheer magnitude
* //of the operation
* //this will have to be fine tuned based on the size of the operation
* if (doHeftyOperation()) then
* call thread.sync()
* endif
* endif
*
* call TriggerSyncReady()
* exitwhen thread.synced
* endloop
*
* call thread.destroy()
*
*************************************************************************************/
struct Thread
private unit threadSyncer
private static trigger syncThreadTrigger
readonly boolean synced
static method create takes nothing returns thistype
local thistype this = allocate()
if threadSyncer == null then
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = false
endif
set threadSyncer = CreateUnit(Player(0), UNIT_SYNC_ID, WorldBounds.maxX, WorldBounds.maxY, 0)
call SetUnitUserData(threadSyncer, this)
call PauseUnit(threadSyncer, true)
call SetUnitPosition(threadSyncer, 99999, 99999)
call TriggerRegisterUnitEvent(syncThreadTrigger, threadSyncer, EVENT_UNIT_SELECTED)
static if LIBRARY_UnitIndexer then
set UnitIndexer.enabled = true
endif
endif
return this
endmethod
method destroy takes nothing returns nothing
set synced = false
call deallocate()
endmethod
/*
* Call within a local block
*/
method sync takes nothing returns nothing
call SelectUnit(threadSyncer, true)
call SelectUnit(threadSyncer, false)
endmethod
private static method syncThread takes nothing returns boolean
local thistype this = GetUnitUserData(GetTriggerUnit())
set synced = true
return false
endmethod
/*
* Struct Initialization
*/
private static method onInit takes nothing returns nothing
set syncThreadTrigger = CreateTrigger()
call TriggerAddCondition(syncThreadTrigger, Filter(function thistype.syncThread))
endmethod
endstruct
endlibrary