...you can't do anything without variables, instead of asking for a save/load-code explanation, ask someone to explain variables.
You won't be able to create a map without using variables, I can assure you that.
BASICS:
Variables are a very easy way to store and load data,
this data cannot be removed unless you have removed it manually.
A simple example is a unit variable called "Hero" in an RPG.
You can store the hero of a player in that variable
and then you can use the variable to easily recall him.
Though that isn't the only use of variables, here are some thing I use them for:
- Leak fixing
- Special effects (changing the offset/degrees as well)
- Triggered abilities
- Every system
- Multiboards/leaderboards
- ... (a lot more).
There are 2 types of variables:
- Globals (variables that can be used in every trigger over the entire map)
- Locals (variables that are defined for a single trigger and do not exist in others).
Globals are for both JASS and GUI, but Locals are only for JASS.
You can also check the box which says "array", for storing a lot of data in an efficient way.
BASIC GLOBAL:
Now we know what a global is, we'll create one.
I am going to use a leak-fixing variable as example, so we will create a unit group variable.
Go to your trigger editor and press CTRL + B (or press the yellow 'X'-icon).
Then press CTRL + N (or press the green 'X'-icon in the varaible editor).
Variable Name: your choice (I will use "Group")
Variable Type: "Unit Group"
Leave the array and initial value as they are, that's for later
Now we need to set the variable first (of course, otherwise there is no data in it)
As I've said before, it's for leak fixing, so I will do just that.
-
Trigger
-
Events
-
Unit - A unit Begins casting an ability
-
Conditions
-
(Ability being cast) equal to Apocalypse
-
Actions
-
Set Group = (Units in (Playable map area) owned by Player 12 (Brown))
-
Unit Group - Pick every unit in Group and do (Actions)
-
Loop - Actions
-
Unit - Kill (Picked unit)
-
Custom script: call DestroyGroup(udg_Group)
Allright, I'll admit it: this is useless...
This immeadiately kills all units owned by player 12 when an ability is cast.
But it's just for the example, right?
I do not need to explain the event and condition I guess, so let's go over the actions
The first action is simple: I set the variable we've created to the data I want.
In this case, the data is "(Units in (Playable map area) owned by Player 12 (Brown))".
Then I will recall that data, which is very simple: I just need to pick the variable.
I do that with the unit group-action and then setting the standard "(Units in (Playable map area))"
to the variable "Group".
The last thing is to fix the leak, I don't know if you know how to do that.
In any case, if you want a good tutorial about leaks, you can search a leak-tutorial on the hive.
BASIC LOCAL:
NOTE: IF YOU DO NOT USE JASS, SKIP THIS PART!
Locals are a very good way to make spells MUI, since locals cannot be overwritten,
unless you overwrite it in the same trigger.
Though if the trigger runs multiple times, the local variable from the first loop can
have different data than the local from the second loop and it will not overwrite.
Maybe that doesn't explain much... well, let's just go over the rest and I'll explain
this principle throughout the rest of this part.
A local is very easy to create, you do not need the variable editor for this.
I will use this local in a useless way, but useful for the tutorial and explanation
of the MUI-part.
JASS:
function Tutorial takes nothing returns nothing
local unit u = GetTriggerUnit()
call TriggerSleepAction(2)
call RemoveUnit(u)
set u = null
endfunction
Yeah, it doesn't have any event/condition and it uses a TriggerSleepAction (a wait).
but it doesn't matter ^^ just think it activates when someone casts a spell.
Now, if 2 or more units activate this function within 2 seconds,
you would think the last one would overwrite the previous ones.
This, however is not true. All units will be removed.
For example:
a paladin casts a spell, he is now set to the variable
1 second later, an archmage casts the same spell and he is also set to the same variable
The normal logic would be that the archmage has overwritten the paladin.
This would happen in Global variables, but with locals they will both be removed.
As you can see, this is really useful and easy to set up,
but you also need to null the variables after usage, it's about the same as leak fixing.
ARRAYS:
Arrays are really useful, I will create a special effect (GUI) so you can see it yourself.
The trigger will still be easy, but it is a pretty basic effect.
In the variable editor (we're using globals again), create 1 variables:
- Name: "Loc" (another name is also good)
- Type: 'Point'
- Array: 'yes'
- Initial Value: None (standard)
-
Trigger
-
Events
-
Unit - A unit Begins casting an ability
-
Conditions
-
(Ability being cast) equal to Apocalypse
-
Actions
-
Set Loc[1] = (Position of (Triggering unit))
-
Special Effect - Create a special effect at Loc[1] using [Special Effect Type]
-
Special Effect - Destroy (Last created special effect)
-
For each (Integer A) from 1 to 12, do (Actions)
-
Loop - Actions
-
Set Loc[2] = (Loc[1] offset by 256.00 towards (30.00 x (Real((Integer A)))) degrees)
-
Special Effect - Create a special effect at Loc[2] using [Special Effect Type]
-
Special Effect - Destroy (Last created special effect)
-
Custom script: call RemoveLocation(udg_Loc[1])
-
Custom script: call RemoveLocation(udg_Loc[2])
Since "point with offset" uses 2 locations, it also has 2 leaks
therefore you will need either an array, or multiple variables to fix the leaks.
The trigger creates a circle of special effects around the caster.
I know that the Integer A-loop isn't as good as Variable-loops,
but GUI is more commonly used...
Local arrays can be created like this: "local unit array u"
So the only thing you need to add is the "array", the rest stays the same.
When calling for a local array, always use the square brackets (e.g.: u[1]).