Hi, i've been doing trigger since sc2 came out and have no programming or wc3 background and have been learning with logic and trial and error.. I don't have time to actually pick up a book and start learning programming language, but I realize I don't understand some very basic stuff in the editor:
- Difference between action and function? I know this has been answered many times saying function returns the value or sometime, but I realize I have no idea what it means by 'returns', which is my next question...
-what does it mean by return? I have a general idea what it is, but I still feel unsure.
- and also, what happens with the variable when you check 'constant'?
A function is something that you can "call" and give parameters, it then returns a value.
Every time you do something like "Triggering Player" you are actually calling a function which is returning the triggering player.
For instance you could make a function that adds two numbers.
Basically you would make two parameters, Number 1 and Number 2 that are Integer values and then the function would make a Number 3 that adds 1 and 2 together and then returns Number 3.
You would then say in an action:
Set Variable X = AddTwoNumbers(2, 5)
At which point X would be set to 7. Obviously making an adding function is pointless since Blizzard already made one for you, but that should give you an idea of how this works.
[My explanation is a little more complex than PhantomInfern, but I'm quite sure they're both still right...]
An action basically is exactly what it implies: It acts upon data and fields you give it. A function, more often than not, is what most of the actions are based upon.
A returning value is just the value that a function returns to whatever it is being called for. For example, let's say you have a trigger that says this:
In that trigger, we have several functions and returning values.
Our killed unit will be a marine, as shown above (a more logical way to put this would have been to remove the if then else statement and put the "(Triggering unit) == Marine" in the Conditions portion, but I went this route). The unit that killed this marine will be a carrier owned by player 10.
Triggering unit is a functions, as well as Killing unit, and Owner of x. Each of them return a specific value.
Triggering unit returns a specific Unit type (this means that it returns the exact unit who caused the trigger. There's another "Unit type value" that relates to any unit of that type (i.e. Marine, Zergling, Zealot), this one returns MarineX or Marine Y or Zergling X or Zergling Y, or Zealot X or Zealot Y, etc.) value, and returns whatever unit caused the event to trigger (in this case, a unit dying caused the trigger to trigger). In the if statement, I was checking to see if that returned value was equal to a Marine, and if so, to continue with the rest of the statement.
Killing Unit returns a specific Unit type based on the specific unit who killed the triggering unit. I used inside of another function, "Owner of Unit," which will return a integer value based on what player owns the "Killing unit," or unit who killed our triggering unit.
I hope that wasn't too confusing. :I
To answer your last question, a constant variable means that whatever you set it to when you create the variable, it can not be changed again unless you edit the variable itself. Trying to change the variable through a trigger will result in errors. These types of variables are useful for variables that will never change, and ones that you want to always stay the same regardless of what you try to do to them.
Functions only provide you with a return value, where as variables store that value.
A simple way to look at it would like this: Think of people who are in your family. The function would be recalling the people who are in your family, and the variables would be who is in your family. Most (if not all) functions have no use without variables.
The answers are correct and great, I just try a different approach to answer your questions :P
An action and a function are practically the same thing. The difference in the GUI is only that functions do not appear in the "general" list when creating a new line in the trigger editor.
When a function returns something it's like when you order a meal in a restaurant. You tell the cook to make 1 salad, 2 piece of meat and a water (the parameters), he works with these informations and returns you a lunch. That means functions do something with the parameters you give to them just like actions do, but finally they return the result of their work so that you can use it.
Constant variables in GUI again don't have any great differences in practical use from normal variables. But you can for example only use constant real variables in the event "Every X seconds of game time".
Edit: Is there an advantage in using a function v. variable to return a value?
-> I do not exactly get this question. Do you mean why and when to use a function? Well, just imagine you do some calculations in this function or some stuff which would take too long to copy and paste it everytime, then it's much easier to use a function.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
Hi, i've been doing trigger since sc2 came out and have no programming or wc3 background and have been learning with logic and trial and error.. I don't have time to actually pick up a book and start learning programming language, but I realize I don't understand some very basic stuff in the editor:
- Difference between action and function? I know this has been answered many times saying function returns the value or sometime, but I realize I have no idea what it means by 'returns', which is my next question...
-what does it mean by return? I have a general idea what it is, but I still feel unsure.
- and also, what happens with the variable when you check 'constant'?
@DrHu: Go
A function is something that you can "call" and give parameters, it then returns a value. Every time you do something like "Triggering Player" you are actually calling a function which is returning the triggering player.
For instance you could make a function that adds two numbers.
Basically you would make two parameters, Number 1 and Number 2 that are Integer values and then the function would make a Number 3 that adds 1 and 2 together and then returns Number 3.
You would then say in an action:
Set Variable X = AddTwoNumbers(2, 5)
At which point X would be set to 7. Obviously making an adding function is pointless since Blizzard already made one for you, but that should give you an idea of how this works.
[My explanation is a little more complex than PhantomInfern, but I'm quite sure they're both still right...]
An action basically is exactly what it implies: It acts upon data and fields you give it. A function, more often than not, is what most of the actions are based upon.
A returning value is just the value that a function returns to whatever it is being called for. For example, let's say you have a trigger that says this:
In that trigger, we have several functions and returning values.
Our killed unit will be a marine, as shown above (a more logical way to put this would have been to remove the if then else statement and put the "(Triggering unit) == Marine" in the Conditions portion, but I went this route). The unit that killed this marine will be a carrier owned by player 10.
Triggering unit is a functions, as well as Killing unit, and Owner of x. Each of them return a specific value.
Triggering unit returns a specific Unit type (this means that it returns the exact unit who caused the trigger. There's another "Unit type value" that relates to any unit of that type (i.e. Marine, Zergling, Zealot), this one returns MarineX or Marine Y or Zergling X or Zergling Y, or Zealot X or Zealot Y, etc.) value, and returns whatever unit caused the event to trigger (in this case, a unit dying caused the trigger to trigger). In the if statement, I was checking to see if that returned value was equal to a Marine, and if so, to continue with the rest of the statement.
Killing Unit returns a specific Unit type based on the specific unit who killed the triggering unit. I used inside of another function, "Owner of Unit," which will return a integer value based on what player owns the "Killing unit," or unit who killed our triggering unit.
I hope that wasn't too confusing. :I
To answer your last question, a constant variable means that whatever you set it to when you create the variable, it can not be changed again unless you edit the variable itself. Trying to change the variable through a trigger will result in errors. These types of variables are useful for variables that will never change, and ones that you want to always stay the same regardless of what you try to do to them.
Thanks guys, actually do get it after reading it over a few times. I've been using my actions properly then.
Is there an advantage in using a function v. variable to return a value?
@DrHu: Go
Functions only provide you with a return value, where as variables store that value.
A simple way to look at it would like this: Think of people who are in your family. The function would be recalling the people who are in your family, and the variables would be who is in your family. Most (if not all) functions have no use without variables.
The answers are correct and great, I just try a different approach to answer your questions :P
An action and a function are practically the same thing. The difference in the GUI is only that functions do not appear in the "general" list when creating a new line in the trigger editor.
When a function returns something it's like when you order a meal in a restaurant. You tell the cook to make 1 salad, 2 piece of meat and a water (the parameters), he works with these informations and returns you a lunch. That means functions do something with the parameters you give to them just like actions do, but finally they return the result of their work so that you can use it.
Constant variables in GUI again don't have any great differences in practical use from normal variables. But you can for example only use constant real variables in the event "Every X seconds of game time".
Edit: Is there an advantage in using a function v. variable to return a value?
-> I do not exactly get this question. Do you mean why and when to use a function? Well, just imagine you do some calculations in this function or some stuff which would take too long to copy and paste it everytime, then it's much easier to use a function.