[snippet] LocalHelper

Scripts

13 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.

Code (JASS) Select
library LocalHelper

    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


Code (jass) Select
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
13 years
Nice interface, and glad to see you on this site. :)

One thing:
Code (jass) Select
        static method create takes nothing returns thistype
            return thistype.allocate()
        endmethod


Can be removed, no? .create exists by default. (unless you extend array)

Also, you still have the [JASS] tags written in the code, lol. But otherwise it looks good.
13 years
Copy/paste errors fixed  ::)

The explicit method create is not necessary, i've just added it for completness reason since i needed to write the destroy method.
When it's converted in jass with or without that's exactly the same anyway.
13 years
I agree with PurgeandFire, it sools very nice, so to the database :)
13 years
An incredibly useful resource. I double approve >:D
10 years
Not sure if I understand full implication of this. Why would I use this over my custom message system? The only time so far I've needed this for something outside of messages was when I wanted to force multiple players to select the same neutral unit so they could all hire a unit at the beginning of the game. Could someone list other things this could be used for since I'm not an expert on the GetLocalPlayer() function?

Things I've thought of:
Selecting Units
Displaying Messages
Setting the Camera (practically the same thing as selecting units, but it's still different)
...and that's all I know.
10 years
Quote from: sankaku on August 31, 2015, 03:50:20 AM
Not sure if I understand full implication of this. Why would I use this over my custom message system? The only time so far I've needed this for something outside of messages was when I wanted to force multiple players to select the same neutral unit so they could all hire a unit at the beginning of the game. Could someone list other things this could be used for since I'm not an expert on the GetLocalPlayer() function?

Things I've thought of:
Selecting Units
Displaying Messages
Setting the Camera (practically the same thing as selecting units, but it's still different)
...and that's all I know.
This is more like a wrapper to handle easily the buggy [lcode=jass]GetLocalPlayer[/lcode] and manage stuff like you suggest in an easier way.
10 years
So basically this is to get around the compilation crashing?