Snippet Unit States

Romek

Super Moderator
Reaction score
963
Unit States
By Romek

Introduction:
A short, simple little library of method operators to make setting a units life, mana, strength, agility, intelligence and UserData much easier.
All the functions are inlined by JassHelper, so this doesn't add any code to your final script. It just makes it much easier to code in the first place.

How to Use:
Make a trigger (name doesn't matter), and go to Edit -> Convert to Custom Text.
Then paste the entire library into the trigger.

To utilize the system while making a map, simply use:
JASS:
set WhichUnit:WhichState = Value

And
JASS:
set <var> = WhichUnit:WhichState


You can also use WhichState[WhichUnit] everywhere. Although using the colon looks cleaner.

You can change the following:
  • Life (Health, HP)
  • Mana (MP)
  • Strength (Str)
  • Agility (Agi)
  • Intelligence (Intel, Int)
  • User Data (Value, Data)
  • X Position
  • Y Position
  • Z Position (Height)
  • Unit Facing
  • Animation (Returns "")
  • Owner
  • Move Speed

Examples:
JASS:
set GetTriggerUnit():Life = 600.
set someUnit:Str = 666
local integer val = Unit<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />ata
set WhichUnit:Owner = Player(6)


The Code:
JASS:
//           +------------------------------------------+
//           |     -=  U N I T   S T A T E S  =-        |
//           |             By Romek (v1.5)              |
// +---------+------------------------------------------+---------+
// | A simple library which makes setting unit states much easier |
// | than it has ever been, in a simple efficient manner.         |
// +--------------------------------------------------------------+
// | Instead of using GetUnitState(..) or SetUnitState(..) use:   |
// | set unit:State = value or State[unit] = value                |
// | For example:                                                 |
// |  - set GetTriggerUnit():Life = 700.                          |
// |  - set whichUnit:Mana = 300.                                 |
// |  - local integer i = Unit:Strength                           |
// |  - set SomeUnit:Owner = Player(5)                            |
// +--------------------------------------------------------------+
// | You can use the following &#039;States&#039;:  (Name | Alt. Name)      |
// |  - Life              - Health        - HP                    |
// |  - Mana              - MP                                    |
// |  - Strength          - Str                                   |
// |  - Agility           - Agi                                   |
// |  - Intelligence      - Intel         - Int                   |
// |  - Data              - Value         (UnitUserData)          |
// |  - X                                 (X Position)            |
// |  - Y                                 (Y Position)            |
// |  - Loc               - Location                              |
// |  - Z                 - Height        (Z Position)            |
// |  - Face              - Facing        - Degrees               |
// |  - Radians                           (Facing in Radians)     |
// |  - Animation         - Anim          (Returns &quot;&quot;)            |
// |  - Owner                                                     |
// |  - MoveSpeed         - Speed                                 |
// +--------------------------------------------------------------+
// |       All functions are inlined by JassHelper.               |
// +--------------------------------------------------------------+

library UnitStates
    //! textmacro State__UState takes name, type, in, out
    struct $name$ extends array        
        static method operator []= takes unit u, $type$ val returns nothing
            call $in$
        endmethod
        static method operator [] takes unit u returns $type$
            return $out$
        endmethod
    endstruct
    //! endtextmacro
    //! runtextmacro State__UState(&quot;Life&quot;, &quot;real&quot;, &quot;SetWidgetLife(u, val)&quot;, &quot;GetWidgetLife(u)&quot;)
    //! runtextmacro State__UState(&quot;Health&quot;, &quot;real&quot;, &quot;SetWidgetLife(u, val)&quot;, &quot;GetWidgetLife(u)&quot;)
    //! runtextmacro State__UState(&quot;HP&quot;, &quot;real&quot;, &quot;SetWidgetLife(u, val)&quot;, &quot;GetWidgetLife(u)&quot;)
    //! runtextmacro State__UState(&quot;Mana&quot;, &quot;real&quot;, &quot;SetUnitState(u, UNIT_STATE_MANA, val)&quot;, &quot;GetUnitState(u, UNIT_STATE_MANA)&quot;)
    //! runtextmacro State__UState(&quot;MP&quot;, &quot;real&quot;, &quot;SetUnitState(u, UNIT_STATE_MANA, val)&quot;, &quot;GetUnitState(u, UNIT_STATE_MANA)&quot;)
    //! runtextmacro State__UState(&quot;Strength&quot;, &quot;integer&quot;, &quot;SetHeroStr(u, val, false)&quot;, &quot;GetHeroStr(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Str&quot;, &quot;integer&quot;, &quot;SetHeroStr(u, val, false)&quot;, &quot;GetHeroStr(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Agilty&quot;, &quot;integer&quot;, &quot;SetHeroAgi(u, val, false)&quot;, &quot;GetHeroAgi(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Agi&quot;, &quot;integer&quot;, &quot;SetHeroAgi(u, val, false)&quot;, &quot;GetHeroAgi(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Intelligence&quot;, &quot;integer&quot;, &quot;SetHeroInt(u, val, false)&quot;, &quot;GetHeroInt(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Intel&quot;, &quot;integer&quot;, &quot;SetHeroInt(u, val, false)&quot;, &quot;GetHeroInt(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Int&quot;, &quot;integer&quot;, &quot;SetHeroInt(u, val, false)&quot;, &quot;GetHeroInt(u, true)&quot;)
    //! runtextmacro State__UState(&quot;Data&quot;, &quot;integer&quot;, &quot;SetUnitUserData(u, val)&quot;, &quot;GetUnitUserData(u)&quot;)
    //! runtextmacro State__UState(&quot;Value&quot;, &quot;integer&quot;, &quot;SetUnitUserData(u, val)&quot;, &quot;GetUnitUserData(u)&quot;)
    //! runtextmacro State__UState(&quot;X&quot;, &quot;real&quot;, &quot;SetUnitX(u, val)&quot;, &quot;GetUnitX(u)&quot;)
    //! runtextmacro State__UState(&quot;Y&quot;, &quot;real&quot;, &quot;SetUnitY(u, val)&quot;, &quot;GetUnitY(u)&quot;)
    //! runtextmacro State__UState(&quot;Location&quot;, &quot;location&quot;, &quot;SetUnitPositionLoc(u, val)&quot;, &quot;GetUnitLoc(u)&quot;)
    //! runtextmacro State__UState(&quot;Loc&quot;, &quot;location&quot;, &quot;SetUnitPositionLoc(u, val)&quot;, &quot;GetUnitLoc(u)&quot;)
    //! runtextmacro State__UState(&quot;Z&quot;, &quot;real&quot;, &quot;SetUnitFlyHeight(u, val, 0.)&quot;, &quot;GetUnitFlyHeight(u)&quot;)
    //! runtextmacro State__UState(&quot;Height&quot;, &quot;real&quot;, &quot;SetUnitFlyHeight(u, val, 0.)&quot;, &quot;GetUnitFlyHeight(u)&quot;)
    //! runtextmacro State__UState(&quot;Face&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)&quot;)
    //! runtextmacro State__UState(&quot;Facing&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)&quot;)
    //! runtextmacro State__UState(&quot;Degrees&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)&quot;)
    //! runtextmacro State__UState(&quot;Radians&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)*bj_DEGTORAD&quot;)
    //! runtextmacro State__UState(&quot;Anim&quot;, &quot;string&quot;, &quot;SetUnitAnimation(u, val)&quot;, &quot;\&quot;\&quot;&quot;)
    //! runtextmacro State__UState(&quot;Animation&quot;, &quot;string&quot;, &quot;SetUnitAnimation(u, val)&quot;, &quot;\&quot;\&quot;&quot;)
    //! runtextmacro State__UState(&quot;Owner&quot;, &quot;player&quot;, &quot;SetUnitOwner(u, val, true)&quot;, &quot;GetOwningPlayer(u)&quot;)
    //! runtextmacro State__UState(&quot;Speed&quot;, &quot;real&quot;, &quot;SetUnitMoveSpeed(u, val)&quot;, &quot;GetUnitMoveSpeed(u)&quot;)
    //! runtextmacro State__UState(&quot;MoveSpeed&quot;, &quot;real&quot;, &quot;SetUnitMoveSpeed(u, val)&quot;, &quot;GetUnitMoveSpeed(u)&quot;)
endlibrary


Capital Names Edition:
JASS:
//           +------------------------------------------+
//           |     -=  U N I T   S T A T E S  =-        |
//           |             By Romek (v1.5)              |
//           |        -&gt;  CAPITAL Edition  &lt;-           |
// +---------+------------------------------------------+---------+
// | A simple library which makes setting unit states much easier |
// | than it has ever been, in a simple efficient manner.         |
// +--------------------------------------------------------------+
// | Instead of using GetUnitState(..) or SetUnitState(..) use:   |
// | set unit:State = value or State[unit] = value                |
// | For example:                                                 |
// |  - set GetTriggerUnit():LIFE = 700.                          |
// |  - set whichUnit:MANA = 300.                                 |
// |  - local integer i = Unit:STRENGTH                           |
// |  - set SomeUnit:OWNER = Player(5)                            |
// +--------------------------------------------------------------+
// | You can use the following &#039;States&#039;:  (Name | Alt. Name)      |
// |  - LIFE              - HEALTH        - HP                    |
// |  - MANA              - MP                                    |
// |  - STRENGTH          - STR                                   |
// |  - AGILITY           - AGI                                   |
// |  - INTELLIGENCE      - INTEL         - INT                   |
// |  - DATA              - VALUE         (UnitUserData)          |
// |  - X                                 (X Position)            |
// |  - Y                                 (Y Position)            |
// |  - LOC               - LOCATION                              |
// |  - Z                 - HEIGHT        (Z Position)            |
// |  - FACE              - FACING        - DEGREES               |
// |  - RADIANS                           (Facing in Radians)     |
// |  - ANIMATION         - ANIM          (Returns &quot;&quot;)            |
// |  - OWNER                                                     |
// |  - MOVESPEED         - SPEED                                 |
// +--------------------------------------------------------------+
// |       All functions are inlined by JassHelper.               |
// +--------------------------------------------------------------+

library UnitStatesCaps
    //! textmacro State__UCState takes name, type, in, out
    struct $name$ extends array    
        static method operator []= takes unit u, $type$ val returns nothing
            call $in$
        endmethod
        static method operator [] takes unit u returns $type$
            return $out$
        endmethod
    endstruct
    //! endtextmacro
    //! runtextmacro State__UCState(&quot;LIFE&quot;, &quot;real&quot;, &quot;SetWidgetLife(u, val)&quot;, &quot;GetWidgetLife(u)&quot;)
    //! runtextmacro State__UCState(&quot;HEALTH&quot;, &quot;real&quot;, &quot;SetWidgetLife(u, val)&quot;, &quot;GetWidgetLife(u)&quot;)
    //! runtextmacro State__UCState(&quot;HP&quot;, &quot;real&quot;, &quot;SetWidgetLife(u, val)&quot;, &quot;GetWidgetLife(u)&quot;)
    //! runtextmacro State__UCState(&quot;MANA&quot;, &quot;real&quot;, &quot;SetUnitState(u, UNIT_STATE_MANA, val)&quot;, &quot;GetUnitState(u, UNIT_STATE_MANA)&quot;)
    //! runtextmacro State__UCState(&quot;MP&quot;, &quot;real&quot;, &quot;SetUnitState(u, UNIT_STATE_MANA, val)&quot;, &quot;GetUnitState(u, UNIT_STATE_MANA)&quot;)
    //! runtextmacro State__UCState(&quot;STRENGTH&quot;, &quot;integer&quot;, &quot;SetHeroStr(u, val, false)&quot;, &quot;GetHeroStr(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;STR&quot;, &quot;integer&quot;, &quot;SetHeroStr(u, val, false)&quot;, &quot;GetHeroStr(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;AGILITY&quot;, &quot;integer&quot;, &quot;SetHeroAgi(u, val, false)&quot;, &quot;GetHeroAgi(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;AGI&quot;, &quot;integer&quot;, &quot;SetHeroAgi(u, val, false)&quot;, &quot;GetHeroAgi(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;INTELLIGENCE&quot;, &quot;integer&quot;, &quot;SetHeroInt(u, val, false)&quot;, &quot;GetHeroInt(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;INTEL&quot;, &quot;integer&quot;, &quot;SetHeroInt(u, val, false)&quot;, &quot;GetHeroInt(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;INT&quot;, &quot;integer&quot;, &quot;SetHeroInt(u, val, false)&quot;, &quot;GetHeroInt(u, true)&quot;)
    //! runtextmacro State__UCState(&quot;DATA&quot;, &quot;integer&quot;, &quot;SetUnitUserData(u, val)&quot;, &quot;GetUnitUserData(u)&quot;)
    //! runtextmacro State__UCState(&quot;VALUE&quot;, &quot;integer&quot;, &quot;SetUnitUserData(u, val)&quot;, &quot;GetUnitUserData(u)&quot;)
    //! runtextmacro State__UCState(&quot;X&quot;, &quot;real&quot;, &quot;SetUnitX(u, val)&quot;, &quot;GetUnitX(u)&quot;)
    //! runtextmacro State__UCState(&quot;Y&quot;, &quot;real&quot;, &quot;SetUnitY(u, val)&quot;, &quot;GetUnitY(u)&quot;)
    //! runtextmacro State__UCState(&quot;LOCATION&quot;, &quot;location&quot;, &quot;SetUnitPositionLoc(u, val)&quot;, &quot;GetUnitLoc(u)&quot;)
    //! runtextmacro State__UCState(&quot;LOC&quot;, &quot;location&quot;, &quot;SetUnitPositionLoc(u, val)&quot;, &quot;GetUnitLoc(u)&quot;)
    //! runtextmacro State__UCState(&quot;Z&quot;, &quot;real&quot;, &quot;SetUnitFlyHeight(u, val, 0.)&quot;, &quot;GetUnitFlyHeight(u)&quot;)
    //! runtextmacro State__UCState(&quot;HEIGHT&quot;, &quot;real&quot;, &quot;SetUnitFlyHeight(u, val, 0.)&quot;, &quot;GetUnitFlyHeight(u)&quot;)
    //! runtextmacro State__UCState(&quot;FACE&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)&quot;)
    //! runtextmacro State__UCState(&quot;FACING&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)&quot;)
    //! runtextmacro State__UCState(&quot;DEGREES&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)&quot;)
    //! runtextmacro State__UCState(&quot;RADIANS&quot;, &quot;real&quot;, &quot;SetUnitFacing(u, val)&quot;, &quot;GetUnitFacing(u)*bj_DEGTORAD&quot;)
    //! runtextmacro State__UCState(&quot;ANIM&quot;, &quot;string&quot;, &quot;SetUnitAnimation(u, val)&quot;, &quot;\&quot;\&quot;&quot;)
    //! runtextmacro State__UCState(&quot;ANIMATION&quot;, &quot;string&quot;, &quot;SetUnitAnimation(u, val)&quot;, &quot;\&quot;\&quot;&quot;)
    //! runtextmacro State__UCState(&quot;OWNER&quot;, &quot;player&quot;, &quot;SetUnitOwner(u, val, true)&quot;, &quot;GetOwningPlayer(u)&quot;)
    //! runtextmacro State__UCState(&quot;SPEED&quot;, &quot;real&quot;, &quot;SetUnitMoveSpeed(u, val)&quot;, &quot;GetUnitMoveSpeed(u)&quot;)
    //! runtextmacro State__UCState(&quot;MOVESPEED&quot;, &quot;real&quot;, &quot;SetUnitMoveSpeed(u, val)&quot;, &quot;GetUnitMoveSpeed(u)&quot;)
endlibrary
 

Prometheus

Everything is mutable; nothing is sacred
Reaction score
589
I think this is a great little snippit. +rep man!
 

Romek

Super Moderator
Reaction score
963
> Dude OMG best thing ever where did you get the idea!?? IT"S SOO AWESOME!!!!
..Oh. I didn't know that even existed. :(
I had a quick glance at the code. That's a long system. This is a 50-lined snippet.

(All the best ideas just have to be taken, don't they? ¬_¬")
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
Yeah. Actually your systems are different, in that his is actually a system, and yours is a bunch of wrappers. Thus, it seems to make sense that this one not be... alive, because people here will see it and say 'COOL it's ON TEH HELPRER!!!', and this will become sort of widespread, and then, when people see <unit>:AttackSpeed, they'll think of Litany's, not yours, and freak out when the two aren't working with each other. In the very least, provide a link to that thread and state that yours is a direct wrapper and his is a system with wrappers (his = better, yours = faster, but not of any importance).
 

UndeadDragon

Super Moderator
Reaction score
447
Very useful :thup:
 

Kenny

Back for now.
Reaction score
202
Very cool little snippet from what i have read so far. One question. It can be used like that as well right?

JASS:


What i mean is, it can use function calls and stuff too ayy.

Anyways I've got a few ideas to put this to use. Good work. + rep.
 

Romek

Super Moderator
Reaction score
963
@ Waaaaaaaaaaa[etc]gh
I agree. This one is completely inlined by JassHelper.
I won't post a link though >.<

> Very useful
Thanks :)

> It can be used like that as well right?
Yes. Actually, I might change the Life method to use GetWidgetLife, and allow items and desctructables.
 

Romek

Super Moderator
Reaction score
963
You know, you don't have to do the :, you can also just do the normal [].
Genius! Congratulations for finding that out. It's actually in the first post though.
 

Andrewgosu

The Silent Pandaren Helper
Reaction score
716
It's just a thing of preference, but I suggest using capital letters for the stats you can change.

I mean,

set unit:STR = value
set unit:INT = value
set unit:AGI = value

etc.

It would just look more Blizzard-styled, where unit states, etc. are all in capital letters.

More convenient for those who are familiar with that naming convention (e.g me...).
 

GoGo-Boy

You can change this now in User CP
Reaction score
40
So is it possible to add a certain % extra hp permanently to a unit when it enters the map?
In case, that would be nice, because I'm planning to have a different difficulties to be selected in my map. And one part of that is to increase all units life by a certain percent amount, and that isn't possible with upgrades (or is it?).
 

Romek

Super Moderator
Reaction score
963
Hmm.. Fine. :)
Did a quick find+replace.

"Capital Edition" put into first post. ^_^

So is it possible to add a certain % extra hp permanently to a unit when it enters the map?
In case, that would be nice, because I'm planning to have a different difficulties to be selected in my map. And one part of that is to increase all units life by a certain percent amount, and that isn't possible with upgrades (or is it?).
Yeah, that's possible. Though this doesn't help much with it. These are just wrappers. :)

Hmm. I'll add MaxMana and MaxLife in the future. :D
 

Romek

Super Moderator
Reaction score
963
Bump.
 

Flare

Stops copies me!
Reaction score
662
// | - MoveSpeed - Speed |
Missing ms.

Uhm... it's not missing?
JASS:
 //! runtextmacro State__UState(&quot;MoveSpeed&quot;, &quot;real&quot;, &quot;SetUnitMoveSpeed(u, val)&quot;, &quot;GetUnitMoveSpeed(u)&quot;)

JASS:
//! runtextmacro State__UCState(&quot;MOVESPEED&quot;, &quot;real&quot;, &quot;SetUnitMoveSpeed(u, val)&quot;, &quot;GetUnitMoveSpeed(u)&quot;)
 

WolfieeifloW

WEHZ Helper
Reaction score
372
I'm pretty sure he meant it's missing the 'alt name'?
Which in the code posted, it doesn't seem to have ms as an alt name.
 

Romek

Super Moderator
Reaction score
963
Yeah, he meant an alternate name.

I'll probably add it soon.
This 'snippet' is getting quite lengthy. :p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top