So, a lot of people come to these boards looking for how to learn Galaxy, and how they're going to learn C or Cxx, or Java, or some other language that's "similar" but in reality is a load of more work necessary.
So consider this Galaxy 101. I assume if you're reading this you're familiar with some basic programming concepts. If not, well, learn some.
Galaxy is a fairly simple language, and the majority of the work you'll be doing is in functions. I find it helps to illustrate examples of how things work and then explain them rather than write giant paragraphs about how they work. So here we go.
So here we have a basic Galaxy function. All of your code will exist inside of a function's scope (with the exceptions of global variables).
For those completely new, "void" basically means here that there will be no return type. We'll get into that later, but you can assume it's the "default".
UIDisplayMessage is a native function provided by Blizzard to write text to the screen. The header of this native is something like:
voidUIDisplayMessage(playergroup,int,text);
This header provides the parameters you can put into the function.
PlayerGroupAll() is another native provided by Blizzard that returns a playergroup containing all of the players in the game.
The int parameter is actually what the Galaxy Editor has as "presets" in GUI. You're meant to put in one of the c_messageAreaX constants. These constants are in another file entirely. We use "c_messageAreaChat" here to write to the chat area. And lastly, there's text. Text is essentially an immutable string. StringToText(string) converts the given string into a text. Because a text is immutable, you can't do anything with it, except for concatenate and set it to a new variable. You also cannot do comparisons with texts.
Next, we have a more complicated example, a function to find the factorial of a number.
This makes use of something called recursion, which is possible in Galaxy. For more on recursion: http://en.wikipedia.org/wiki/Recursion.
In this example, you see the basic workings of an if statement, the comparison operator ==, and return values.
The way return values work is simple. If I had another block of code later that read:
voidtest(){intx=factorial(3);// x now contains 3 * 2 * 1 = 6.}
Ideally, any kind of code that's reuseable like that should end up in it's own function. By making the things you do into functions you decrease the likelihood of a bug and make your code easier to debug and read. Throwing pages of code into one function is a very bad idea.
Now you may be wondering how you can actually make some triggers, rather than this correct but mostly useless code. Okay then, let's try to make a trigger that outputs the name of a unit when it dies.
When you're going to start with something like this, it's a good idea to find the functions you'll probably need.
Let's open the Galaxy Editor and search for them.
First, we need an event to register the trigger that fires when a unit dies.
Finding the "Unit Dies" event in the GUI is simple. From there, hit View -> Raw Data to see what the actual function and parameters are.
voidTriggerAddEventUnitDied(unit);
That's all the GUI provides for us. The GUI lies to us though. There's a hidden first parameter in all events that is the trigger to call when the event activates. So it's really:
voidTriggerAddEventUnitDied(trigger,unit);
We also need to figure out how to write to the chat area. Let's pretend we don't already know and find the "Text Message" function in Galaxy.
Here it is:
UIDisplayMessage(playergroup,int,text);
From looking at the GUI we know the second parameter (in regular view) is a Message Area preset. So let's find the Preset for that...
Find the Message Area preset and find the option you want. Then switch to Raw View again and it'll say "Script Value: c_messageAreaChat" for example. That's a constant for the integer.
So, lastly, we need a function that returns the name of a unit and one to return the unit we're actually working with (the one that died).
A little searching with the same method, and ...
unitEventUnit();textUnitTypeGetName(string);
It appears we need a Game Link for UnitTypeGetName(string) so we'll have to find one to get a unit's Game Link as well.
stringUnitGetType(unit);
Now we have all the functions we need to make this trigger. As you learn and program more Galaxy, you'll get better at remembering the functions and their parameters.
voidRegisterTriggers(){triggert=TriggerCreate("OnUnitDeath");TriggerAddEventUnitDied(t,null);// null here means any unit.}boolOnUnitDeath(boolcheckConds,boolrunActions){unitdyingUnit=EventUnit();// the unit that just died.stringunitType=UnitGetType(dyingUnit);textunitName=UnitTypeGetName(unitType);UIDisplayMessage(PlayerGroupAll(),c_messageAreaChat,unitName);returntrue;}
This is the basic format for a trigger. We'll assume you use a custom script block or other to call "RegisterTriggers()" at the start of the map.
The cool part about Galaxy is you can use this format to actually register events at run-time in the game.
The basic format for the trigger is bool myTrigger(bool checkConds, bool runActions). Should the function return true, the trigger is assumed to have run successfully. false, otherwise. The "checkConds" and "runActions" parameters are useful if you plan on using "Check Conditions on Trigger" or other kind of functions but I mostly ignore them for simplicity. They are however, required.
Hopefully this guide's helped you get a basic grasp of Galaxy. If not, well, keep trying. I didn't learn overnight and neither will you.
Another helpful way to figure out how to do things would be to do what you want to do in Galaxy, except in GUI. Once you have the desired trigger in GUI, go to Data -> View Script to view the resulting code. It will be messy and somewhat difficult to read (GUI, eww!) but you can gain valuable insights from it.
Also be sure to check out the language overview on Mapster. I won't lie, a lot of the other resources (or all of them) for galaxy are outdated, but this one is very good and correct.
You also can check out http://paste.sc2mapster.com/1985/ for a list of all the somewhat cryptic natives. You should use the GUI though to find out what the parameters mean. (Unless it's blatantly obvious)
So, a lot of people come to these boards looking for how to learn Galaxy, and how they're going to learn C or Cxx, or Java, or some other language that's "similar" but in reality is a load of more work necessary.
So consider this Galaxy 101. I assume if you're reading this you're familiar with some basic programming concepts. If not, well, learn some.
Galaxy is a fairly simple language, and the majority of the work you'll be doing is in functions. I find it helps to illustrate examples of how things work and then explain them rather than write giant paragraphs about how they work. So here we go.
So here we have a basic Galaxy function. All of your code will exist inside of a function's scope (with the exceptions of global variables).
For those completely new, "void" basically means here that there will be no return type. We'll get into that later, but you can assume it's the "default".
UIDisplayMessage is a native function provided by Blizzard to write text to the screen. The header of this native is something like:
This header provides the parameters you can put into the function.
PlayerGroupAll() is another native provided by Blizzard that returns a playergroup containing all of the players in the game.
The int parameter is actually what the Galaxy Editor has as "presets" in GUI. You're meant to put in one of the c_messageAreaX constants. These constants are in another file entirely. We use "c_messageAreaChat" here to write to the chat area. And lastly, there's text. Text is essentially an immutable string. StringToText(string) converts the given string into a text. Because a text is immutable, you can't do anything with it, except for concatenate and set it to a new variable. You also cannot do comparisons with texts.
Next, we have a more complicated example, a function to find the factorial of a number.
This makes use of something called recursion, which is possible in Galaxy. For more on recursion: http://en.wikipedia.org/wiki/Recursion.
In this example, you see the basic workings of an if statement, the comparison operator ==, and return values.
The way return values work is simple. If I had another block of code later that read:
Ideally, any kind of code that's reuseable like that should end up in it's own function. By making the things you do into functions you decrease the likelihood of a bug and make your code easier to debug and read. Throwing pages of code into one function is a very bad idea.
Now you may be wondering how you can actually make some triggers, rather than this correct but mostly useless code. Okay then, let's try to make a trigger that outputs the name of a unit when it dies.
When you're going to start with something like this, it's a good idea to find the functions you'll probably need.
Let's open the Galaxy Editor and search for them.
First, we need an event to register the trigger that fires when a unit dies.
Finding the "Unit Dies" event in the GUI is simple. From there, hit View -> Raw Data to see what the actual function and parameters are.
That's all the GUI provides for us. The GUI lies to us though. There's a hidden first parameter in all events that is the trigger to call when the event activates. So it's really:
We also need to figure out how to write to the chat area. Let's pretend we don't already know and find the "Text Message" function in Galaxy.
Here it is:
From looking at the GUI we know the second parameter (in regular view) is a Message Area preset. So let's find the Preset for that...
Find the Message Area preset and find the option you want. Then switch to Raw View again and it'll say "Script Value: c_messageAreaChat" for example. That's a constant for the integer.
So, lastly, we need a function that returns the name of a unit and one to return the unit we're actually working with (the one that died).
A little searching with the same method, and ...
It appears we need a Game Link for UnitTypeGetName(string) so we'll have to find one to get a unit's Game Link as well.
Now we have all the functions we need to make this trigger. As you learn and program more Galaxy, you'll get better at remembering the functions and their parameters.
This is the basic format for a trigger. We'll assume you use a custom script block or other to call "RegisterTriggers()" at the start of the map.
The cool part about Galaxy is you can use this format to actually register events at run-time in the game.
The basic format for the trigger is bool myTrigger(bool checkConds, bool runActions). Should the function return true, the trigger is assumed to have run successfully. false, otherwise. The "checkConds" and "runActions" parameters are useful if you plan on using "Check Conditions on Trigger" or other kind of functions but I mostly ignore them for simplicity. They are however, required.
Hopefully this guide's helped you get a basic grasp of Galaxy. If not, well, keep trying. I didn't learn overnight and neither will you.
Another helpful way to figure out how to do things would be to do what you want to do in Galaxy, except in GUI. Once you have the desired trigger in GUI, go to Data -> View Script to view the resulting code. It will be messy and somewhat difficult to read (GUI, eww!) but you can gain valuable insights from it.
Also be sure to check out the language overview on Mapster. I won't lie, a lot of the other resources (or all of them) for galaxy are outdated, but this one is very good and correct.
You also can check out http://paste.sc2mapster.com/1985/ for a list of all the somewhat cryptic natives. You should use the GUI though to find out what the parameters mean. (Unless it's blatantly obvious)
Good luck!