14 years
edited 8 years
This is a less verbose way to handle local code stuff than the usual one, if GetLocalPlayer() == ...
It doesn't use any handle comparing to the alternative ForceAddPlayer / IsPlayerInForce.
It's also faster, but who care ...
Anyway if you care that much about efficiency you would simply inline it and use a not array global variable.
It doesn't use any handle comparing to the alternative ForceAddPlayer / IsPlayerInForce.
It's also faster, but who care ...
Anyway if you care that much about efficiency you would simply inline it and use a not array global variable.
1 library LocalHelper
2 3 globals
4 private boolean array Is_for_player
5 endglobals
6
7 function IsForPlayer takes player which_player returns boolean
8 return Is_for_player[GetPlayerId(which_player)]
9 endfunction
10
11 function IsForPlayerId takes integer which_player_id returns boolean
12 return Is_for_player[which_player_id]
13 endfunction
14
15 struct Local
16
17 readonly static player p // = GetLocalPlayer()
18 readonly static integer id // = GetPlayerId(thistype.p)
19 readonly static string s // = I2S(thistype.id)
20 boolean bool // no need to set it to false, since it's an array in the background (vjass -> jass)
21 22 static method create takes nothing returns thistype
23 return thistype.allocate()
24 endmethod
25 26 method destroy takes nothing returns nothing
27 set this.bool = false
28 call this.deallocate()
29 endmethod
30
31 method addPlayer takes player which_player returns nothing
32 if which_player == thistype.p then
33 set this.bool = true
34 endif
35 endmethod
36
37 method addPlayerId takes integer which_player_id returns nothing
38 if which_player_id == thistype.id then
39 set this.bool = true
40 endif
41 endmethod
42
43 method removePlayer takes player which_player returns nothing
44 if which_player == thistype.p then
45 set this.bool = false
46 endif
47 endmethod
48
49 method removePlayerId takes integer which_player_id returns nothing
50 if which_player_id == thistype.id then
51 set this.bool = false
52 endif
53 endmethod
54
55 method addAllPlayers takes nothing returns nothing
56 set this.bool = true
57 endmethod
58
59 method removeAllPlayers takes nothing returns nothing
60 set this.bool = false
61 endmethod
62
63 static method onInit takes nothing returns nothing
64 // you can't use GetLocalPlayer() in a global variable initial value, or wc3 will crash
65 set thistype.p = GetLocalPlayer()
66 set thistype.id = GetPlayerId(thistype.p)
67 set thistype.s = I2S(thistype.id)
68 set Is_for_player[thistype.id] = true
69 endmethod
70
71 endstruct
72
73 endlibrary
1 scope Sample initializer init // use LocalHelper
2 3 globals4 Local My_force
5 endglobals6 7 private function init takes nothing returns nothing
8 set My_force = Local.create()
9 call My_force.addPlayer(Player(0))
10 call My_force.addPlayer(Player(1))
11 // 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 instead12 if My_force.bool then
13 // local block only for the red and blue player14 call DisplayTextToPlayer(Local.p,0,0,"hello red and blue")
15 endif16 if IsForPlayer(Player(0)) then
17 // local block only for Player(0)18 call DisplayTimedTextFromPlayer(Local.p,0,0,42,"this is only for you %s")
19 endif20 endfunction21
22 endscope