- Joined
- Nov 7, 2014
- Messages
- 571
only for player - "convenient" ONLY_FOR* block for doing stuff for only a specific player or everyone but a specific player
It simply caches the result of GetLocalPlayer() == Player(i) into hopefully well named arrays.
It tries to also work for both "player nambers" (set pn = GetPlayerId(p)) and player variables of course (set p = Player(i)).
So this in my opinion improves readability and saves some unneeded GetLocalPlayer() calls and comparisons.
Usage:
It simply caches the result of GetLocalPlayer() == Player(i) into hopefully well named arrays.
It tries to also work for both "player nambers" (set pn = GetPlayerId(p)) and player variables of course (set p = Player(i)).
So this in my opinion improves readability and saves some unneeded GetLocalPlayer() calls and comparisons.
Usage:
JASS:
if ONLY_FOR_PN[i] then
// if ONLY_FOR_P[GetTriggerPlayer()] then
call MultiboardDisplay(...)
call StartSound(...)
set tt = CreateTextTag(...)
call CameraSetupApply(...)
call SetCameraBounds(...)
call ClearSelection()
call SelectUnit(...)
// etc.
endif
set fx_path = "Abilities\\..."
// if FOR_EVERYONE_BUT_PN[i] then
if FOR_EVERYONE_BUT_P[GetTriggerPlayer()] then
set fx_path = ""
endif
call AddSpecialEffect(fx_path, x, y)
JASS:
library OnlyForPlayer initializer only_for_player_init
globals
player array PLAYER
player LOCAL_PLAYER_P
integer LOCAL_PLAYER_PN
boolean array ONLY_FOR_PN
boolean array FOR_EVERYONE_BUT_PN
endglobals
struct ONLY_FOR_P extends array
static method operator[] takes player p returns boolean
return ONLY_FOR_PN[GetPlayerId(p)]
endmethod
endstruct
struct FOR_EVERYONE_BUT_P extends array
static method operator[] takes player p returns boolean
return FOR_EVERYONE_BUT_PN[GetPlayerId(p)]
endmethod
endstruct
function only_for_player_init takes nothing returns nothing
local integer i
set i = 0
loop
exitwhen i >= bj_MAX_PLAYERS
set PLAYER[i] = Player(i)
set ONLY_FOR_PN[i] = GetLocalPlayer() == PLAYER[i]
set FOR_EVERYONE_BUT_PN[i] = not ONLY_FOR_PN[i]
set i = i + 1
endloop
set LOCAL_PLAYER_P = GetLocalPlayer()
set LOCAL_PLAYER_PN = GetPlayerId(LOCAL_PLAYER_P)
endfunction
endlibrary