- Joined
- Dec 12, 2008
- Messages
- 7,385
Unit Flying Ability
(The Right Way)
(The Right Way)
Table of Contents
Introduction
This is quite a simple tutorial. I made it quite short and straight to the point because there's
not much to say. Basically, what we've been doing to give units the ability to fly is wrong.
Some Jass coders already know this, but most GUI coders don't.
The Problem
Most GUI coders do this to give a unit the ability to fly:
- Unit - Add Crow Form to SomeUnit
- Unit - Remove Crow Form from SomeUnit
Why? Because:
Assume you have a unit that has the ability 'Arav' (Raven Form/Crow Form).
Now, if we were to add the ability to this unit, the function would fail this attempt and
UnitAddAbility
will return false. After we remove the ability (In the second line of the GUI method), the original 'Arav' ability will be removed.
We can conclude that the GUI method is unsafe, bug-prone and WRONG!
The Solution
As I mentioned before,
UnitAddAbility
will return false if the ability adding has failed.We can take advantage of this to write a snippet that attempts to add the ability, checks if the ability
was added, and if it was, the ability will be removed. Luckily, we can do this with no overhead at all.
Or, you can simply check the level of the ability. If it's 0, add and remove the abilities.
If not, do nothing. (No! Don't use the action 'Do Nothing' because all it does is slow you down!)
-
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
-
If - Conditions
- (Level of Crow Form for Unit) Equal to 0
-
Then - Actions
- Unit - Add Crow Form to Unit
- Unit - Remove Crow Form from Unit
- Else - Actions
-
If - Conditions
- Custom script: if UnitAddAbility(udg_Unit,'Amrf') then
- Custom script: call UnitRemoveAbility(udg_Unit,'Amrf')
- Custom script: endif
If the condition (UnitAddAbility) returns false, the ability won't be removed. If it returns true,
the ability will be removed.
Tips
- If you're making a map, you can totally avoid these checks by
creating an alternate crow form ability that you'll never use. (Credits to noob) - Some people use 'Amrf', some use 'Arav', there's pretty much
no difference. If you're a GUIer and you're not sure what those are,
they're codes that correspond to each ability. - This method should be applied for ALL abilities.
Wrap-Up
That's pretty much it. I hope you like this short but informing tutorial and I hope you
won't be making the mistake of blindly adding and removing unit abilities again.
Thank you for reading.
~Magtheridon96
Last edited: