- Joined
- Apr 27, 2008
- Messages
- 2,455
Honestly, i'm not sure it's worth a submission, comparing to just use a force and the associated natives function.
It could also simply be inlined.
But i've realized that most of people are not aware that we can use a simple global boolean variable for local stuff.
It could also simply be inlined.
But i've realized that most of people are not aware that we can use a simple global boolean variable for local stuff.
JASS:
library LocalHelper
//http://www.hiveworkshop.com/forums/submissions-414/snippet-localhelper-215108/#post2152752
globals
private boolean array Is_for_player
endglobals
function IsForPlayer takes player which_player returns boolean
return Is_for_player[GetPlayerId(which_player)]
endfunction
function IsForPlayerId takes integer which_player_id returns boolean
return Is_for_player[which_player_id]
endfunction
struct Local
readonly static player p // = GetLocalPlayer()
readonly static integer id // = GetPlayerId(thistype.p)
readonly static string s // = I2S(thistype.id)
boolean bool // no need to set it to false, since it's an array in the background (vjass -> jass)
static method create takes nothing returns thistype
return thistype.allocate()
endmethod
method destroy takes nothing returns nothing
set this.bool = false
call this.deallocate()
endmethod
method addPlayer takes player which_player returns nothing
if which_player == thistype.p then
set this.bool = true
endif
endmethod
method addPlayerId takes integer which_player_id returns nothing
if which_player_id == thistype.id then
set this.bool = true
endif
endmethod
method removePlayer takes player which_player returns nothing
if which_player == thistype.p then
set this.bool = false
endif
endmethod
method removePlayerId takes integer which_player_id returns nothing
if which_player_id == thistype.id then
set this.bool = false
endif
endmethod
method addAllPlayers takes nothing returns nothing
set this.bool = true
endmethod
method removeAllPlayers takes nothing returns nothing
set this.bool = false
endmethod
static method onInit takes nothing returns nothing
// you can't use GetLocalPlayer() in a global variable initial value, or wc3 will crash
set thistype.p = GetLocalPlayer()
set thistype.id = GetPlayerId(thistype.p)
set thistype.s = I2S(thistype.id)
set Is_for_player[thistype.id] = true
endmethod
endstruct
endlibrary
JASS:
scope Sample initializer init // use LocalHelper
globals
Local My_force
endglobals
private function init takes nothing returns nothing
set My_force = Local.create()
call My_force.addPlayer(Player(0))
call My_force.addPlayer(Player(1))
// or just : set My_force.bool = (Local.id < 2) , but be aware that it will work only for "static initial add", if you want add/remove players later, use the methods instead
if My_force.bool then
// local block only for the red and blue player
call DisplayTextToPlayer(Local.p,0,0,"hello red and blue")
endif
if IsForPlayer(Player(0)) then
// local block only for Player(0)
call DisplayTimedTextFromPlayer(Local.p,0,0,42,"this is only for you %s")
endif
endfunction
endscope
Last edited by a moderator: