Author Topic: [vJass] LocalHelper  (Read 13936 times)

  • Rookie - level 1
  • *
  • Posts: 48
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 8
  • cool != useful
    • View Profile
[vJass] LocalHelper
« on: August 02, 2012, 12:36:01 PM »
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]
  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

Code: jass  [Select]
  1. scope Sample initializer init // use LocalHelper
  2.  
  3.     globals
  4.         Local My_force
  5.     endglobals
  6.  
  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 instead
  12.         if My_force.bool then
  13.             // local block only for the red and blue player
  14.             call DisplayTextToPlayer(Local.p,0,0,"hello red and blue")
  15.         endif
  16.         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.         endif
  20.     endfunction
  21.    
  22. endscope
« Last Edit: August 02, 2012, 09:18:07 PM by Troll-Brain »
vJass is already a jass preprocessor, no need to (ab)use vJass features in order to make some inferior vJass preprocessor coded "by hand".

  • Rookie - level 1
  • *
  • Posts: 41
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 13
    • View Profile
Re: [vJass] LocalHelper
« Reply #1 on: August 02, 2012, 07:19:17 PM »
Nice interface, and glad to see you on this site. :)

One thing:
Code: jass  [Select]
  1.         static method create takes nothing returns thistype
  2.             return thistype.allocate()
  3.         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.

  • Rookie - level 1
  • *
  • Posts: 48
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 8
  • cool != useful
    • View Profile
Re: [vJass] LocalHelper
« Reply #2 on: August 02, 2012, 09:21:13 PM »
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.
vJass is already a jass preprocessor, no need to (ab)use vJass features in order to make some inferior vJass preprocessor coded "by hand".

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 976
  • WC3 Models: 5
  • WC3 Tutorials: 11
  • WC3 Tools: 9
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Re: [vJass] LocalHelper
« Reply #3 on: August 04, 2012, 03:04:26 PM »
I agree with PurgeandFire, it sools very nice, so to the database :)


We can give you full hosting for your projects. Not only a forum, a complete page!!.

A custom altered melee map where you can play Naga and Demons. Featuring a nice additions in features and game style. Check it out!!
Save your important files fast and easy

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 83
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
Re: [vJass] LocalHelper
« Reply #4 on: August 05, 2012, 09:59:13 AM »
An incredibly useful resource. I double approve >:D

  • Newbie - level 2
  • **
  • Posts: 21
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 1
    • View Profile
Re: [vJass] LocalHelper
« Reply #5 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.

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 976
  • WC3 Models: 5
  • WC3 Tutorials: 11
  • WC3 Tools: 9
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Re: [vJass] LocalHelper
« Reply #6 on: September 01, 2015, 09:25:58 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 GetLocalPlayer and manage stuff like you suggest in an easier way.


We can give you full hosting for your projects. Not only a forum, a complete page!!.

A custom altered melee map where you can play Naga and Demons. Featuring a nice additions in features and game style. Check it out!!
Save your important files fast and easy

  • Newbie - level 2
  • **
  • Posts: 21
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 1
    • View Profile
Re: [vJass] LocalHelper
« Reply #7 on: September 01, 2015, 12:29:43 PM »
So basically this is to get around the compilation crashing?

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 976
  • WC3 Models: 5
  • WC3 Tutorials: 11
  • WC3 Tools: 9
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Re: [vJass] LocalHelper
« Reply #8 on: September 02, 2015, 10:03:04 AM »
So basically this is to get around the compilation crashing?
More than compilation crashing, game crashing.


We can give you full hosting for your projects. Not only a forum, a complete page!!.

A custom altered melee map where you can play Naga and Demons. Featuring a nice additions in features and game style. Check it out!!
Save your important files fast and easy

 

[vJass] Rect Utils

Started by WareditorBoard Rejected Codes & Snippets

Replies: 7
Views: 13058
Last post August 31, 2015, 02:41:01 AM
by sankaku
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!