Quote:Using while loops rather than for loops dramatically decreases your script size, decreases the overload and increases map performance. 9 out of the top 10 mappers agree that you should never ever use for loops, the other one is a noob, you don't want to be a noob do you?
for loops got fixed by blizzard a few versions back.
they are well usable now :)
but didnt know that either some weeks ago.
overall a nice guide that will be helpful for alot of beginners but also advanced mappers.





DogmaiSEA Regular ShmoeCommon Sense 101 (Triggering Edition)
I personally like direct to the point threads, this is not some breastfed tutorial thread, this is for like-minded technical people who want to improve their GUI scripting skills, listing out a basic set of rules that I myself follow to keep my scripting smart and consistent.
No bullsh!t, just straight to the point radioactive baby formula for the masses.
The three basic rules of Scripting smartly (Using the GUI Trigger Editor).
Otherwise this can be translated into ...
If you did any of the three above, I sincerely suggest it is time for a re-write and to do things properly this time, the sooner you start the sooner you will finish and you do not want to wait until this get too complicated, you will thank me later.
Some Additional Things.
LABEL THINGS, have prefixes for things such as Action Definitions that will allow you to find them faster later.
LEARN ARITHMETIC, you know, the 1+2=3 stuff in primary school. For most people it is common knowledge but quite a few people expect to be able to script without any mathematic skills.
Algebra in a nutshell, x=1, y=2, z=? ... x+y=z, therefore Z must be 3. Ta da.
Use LOOPs whenever you can, you creating a dialog for each player? Do not create a dialog for each player, create one dialog for everyone to share, then update it using a loop.
(There are always exceptions, when in doubt use common sense.)
Variables
A variable can be anything, hence the name, you can change what it is at anytime. There are many many different types, but the two you will use most often as Boolean (True or False) and Integer (1,2,3 etc).
Players ARE Integers.
This means each player is given a unique number in the game, between 1 and 14. Neutral is 0, and Hostile is 15 (This is a total of 16 slots).
Always put variables into Records, do not create a huge list of Global Variables outside records.
Functions
Parameters are information you put in.
Return is the information you receive back.
ALWAYS create local variables = parameters of the same name, do not use parameters in coding. Why? So you can copy and paste code obviously.
There are two types of functions that I will quickly touch on, Functions return a value, Action Definitions do not, they are simply a trigger without an event.
Below is a simple function that I would like you to recreate, it does not need any global variables.
Name Of Player in Colour Options: Function Return Type: Text Parameters player = 0 <Integer> Grammar Text: Name Of Player in Colour(player) Hint Text: (None) Custom Script Code Local Variables player = player <Integer> text = No Text <Text> Actions Variable - Set text = (Combine ("<c val="", (Text((Convert color (Color((Current player player color))) to string))), "">", (Name of player player), "</c>")) General - Return textCreating the Function shown above
Right click on the trigger list, create new function, name it "Name of Player in Colour".
Change the return type to text.
Create a parameter called player, of an integer type.
Create two variables, one called player set to the parameter player, the other called text of text type.
Then set Text as Text Combine Multiple (you will need five combines).
Then Return text
To see this function in action, you should create a new Action - Text Message and then go down to Name of Player in Colour (not Name of Player) and set the player as 1. It should show your name in your colour when you test the map, make sure the text message is set to Chat Area.
So instead of you having to waste time every time you want to show a player's name in their colour having to create a text multiple, then <c val, then colour etc etc, you can just use this one function. Every technical thing you do more than once should be in a function to save you time!
Action Definitions
Action Definitions are basically a trigger without an event, use these for everything, think of them like Folders but only for triggers. For instance, my Map Initialization in Tofu has one line.
Which in turn is another list of Action Definitions etc (I will only show the top three lines cause it's a long list.
You will see I label them different codes for what they do, in this case ti_ stands for Tofu Initialization, this is common sense, it makes finding them very fast.
For trouble shooting this also allows you to turn off things one at a time to see what is going wrong.
Action Definitions can also use Parameters, so you can create an Action Definition that updates a dialog for a specific player.
Records
People explained to me that records are like a folder you put variables in, but no one every told me how to create one. Stupid in hindsight? Correct.
In Wc3 I labelled variables like player_kills(15) player_deaths(15) etc. But when you reach over a couple of hundred variables, in my case 800, then finding them becomes quite slow. Records allow you to streamline this process and keep everything neat and tidy.
Creating a Record
Right click in the trigger pane and click Create new Record, name it Player Data.
Straight under that Right Click in the trigger pane and click Create new Variable, name it Player and set the type to - Record (Player Data), make it an array set to 15.
Wow that was easy wasn't it? Damn everyone who never told me you needed to create a variable to use a record. At least I am telling you, feel special? <3
Now create two variables inside the Player Data Record, named kills and deaths of type integer.
Now go outside to any trigger and do Set Variable, there you will see it has the record called Player, click that then set the Player to 1, and then kills. So instead of having variables such as:
You have a nice little record that moves the [15] to after the player and before whatever it is.
w00t w00t.
You can put records inside records, but they must be above the master record which stores the variables of type record, for instance.
Inside the Player Data record I have at the top.
Remember, the "master" record must always be BELOW the ones you want to put inside it.
While Loops, Never ever use For Loops
Using while loops rather than for loops dramatically decreases your script size, decreases the overload and increases map performance. 9 out of the top 10 mappers agree that you should never ever use for loops, the other one is a noob, you don't want to be a noob do you?
X is the loop, Y is the starting value, and Z is the cut off value.
Alright, so For Loops basically give you;
For Each Integer (x) from (y) to (z) do blah.
While loops take a few seconds longer to set up. You need three lines to set up a While Loop. I will use the same variable names (x,y,z) below as I did above so you can see where it fits in.
Understand? The amounts, x,y,z are still there! Just in a different format.
So you need to set the loop variable X before the while loop to the starting amount. The IF condition is the cut off amount, in this case while X is less than or equal to Z do blah, and then at the very end of the while loop you need a Modify X + 1.
While loops are also handy as you can do a very simple timer on them, and have an extra condition say playersready=false, that when set to true will finish the timer.
A very basic while wait timer.
This one counts down because the modify is now negative. There are a few different schools of thought about using wait timers, I do not personally see the harm in them, and think in moderation, say used at the beginning of the game to countdown a selection dialog box is fine.
Limit your Triggers
The only time you need a trigger is when you need an event to fire one. You should not have multiple triggers for each player, instead use one trigger for everyone and either use Triggering Player or a loop to get the information you need. The more triggers you have firing the more overhead you create and the sooner you will hit that limit. Tofu will perhaps become one of the most polished maps ever seen, but I believe most people will be shocked at how simply and efficient it is written, with next to no triggers, and the payload mainly consisting of functions.
I have seen some maps (ie Phantom Mode) have 14 triggers to allow people to write a command such as -players. You only need one trigger, with multiple events, that uses trigger playing to get the player. This whilst not effecting the game so much, is bad practice in keeping clean code.
Also using Action Definitions allows you to Create Threads when needed, especially with loops, to reduce the overhead of the running script.
Laziness
Copying and pasting code is bad. Reading someone else's code out of a map that has been unprotected is bad. Not purely because because of ethical reasons, but because chances are they have really badly written code. You will be amazed at how badly written most of the top 20 maps are, and whilst you do not need perfect coding to create a successful map, you should want your map to 'be all that it can be'.
Open Source vs Unprotecting
Whilst I do not want Tofu open sourced, I know that without a doubt that it will be available unprotected, and whilst I do not really have a problem with that because that it will only be available in Galaxy, and people will not be able to easily edit it in GUI until there is a parser available, there is nothing that I have done that you cannot do with some time. I am not hiding secrets, I have no problem with sharing my knowledge to those willing to learn, but if you are even so lazy as to not write your own code from an example given above, and would rather copy and paste it into your map, then it doesn't matter :) Cause your map will only ever be as good as your ability, and you are shooting yourself in the ass, nananana ... <3
TL;DR
Read it all.
Suggestions / Agreements / Disagreements
Spam me below and I will update this post if appliciable.