I have a couple questions about Variables, to me there very intimidating. >< I just need to know things like, what do they do? How do I know when to use them? Is it like data storage or something?
Variables are something that stores a value of a certain type. They come in 2 types: Global and Local. Global ones are created in the same pane as triggers and folders. They can be used by any trigger. Local variables are made inside triggers under the 'Variable' section. They are used by the trigger then dumped. If you want to save the value of that variable, you will need to set a global var to it before the trigger ends. You can only store int values under int variables etc, although you can convert some to other things.
A variable is a piece of information.
For example, the variable "A" could store the number "15".
That would be an example of an Integer Variable - A variable that stores an integer (Any number without decimal points, negatives included).
However, variables can also store more advanced information such as Words ("Test"), Dialogs, and even game pre-sets.
Variables are very useful. Say you have this trigger:
Trigger "Player"
Events:
Any player types a chat message
Actions:
Send text message to Triggering Player to subtitle with text "Chat message typed".
That's all good, but what if you want to use the player who typed the chat message in another trigger?
That is where variables come in.
After this line -
Send text message to Triggering Player to subtitle with text "Chat message typed".
Add the following:
Set Variable A = Triggering Player.
Now, when you want to get the player who typed in the message, all you would have to do is use the variable "A".
In general, yes, it is data storage.
A variable is a piece of information.
For example, the variable "A" could store the number "15".
That would be an example of an Integer Variable - A variable that stores an integer (Any number without decimal points, negatives included).
However, variables can also store more advanced information such as Words ("Test"), Dialogs, and even game pre-sets.
Variables are very useful. Say you have this trigger:
Trigger "Player"
Events:
Any player types a chat message
Actions:
Send text message to Triggering Player to subtitle with text "Chat message typed".
That's all good, but what if you want to use the player who typed the chat message in another trigger?
That is where variables come in.
After this line -
Send text message to Triggering Player to subtitle with text "Chat message typed".
Add the following:
Set Variable A = Triggering Player.
Now, when you want to get the player who typed in the message, all you would have to do is use the variable "A".
In general, yes, it is data storage.
So what im getting is. If I need to use something more than once, I need to make a variable?
You can think of a variable as a container. It is sized to fit the biggest possibility of a value.
For example a byte is 0 to 255. A total of 256.
Internally the program would handle it as HEX. Smallest being 00 and largest being FF.
So when you define a variable as a byte. You have early bounded this variable to allocate space for FF.
Now if you tried to put let’s say a string into the byte.
Each character in a string is also hex 0 to 255. The alphabet has 26 lower case letters, and 26 upper case characters. It has 0-9, and shift 0-9(!@#$%^&*()). It also have things like (_+-=[]{}:”;’<>,./?\|). Some of these characters are reserved by galaxy editor, so using them in strings causes errors.
Now if our string was let’s say “This is my String, I created it.”
This string has 32 characters, which is 32 bytes. You can see how a 32 byte String “String[31]” won’t fit in 1 byte “byte[0]”.
Early binding your variables is very important, and understanding when you can’t have it. Early binding means the variable containing is already defined to hold the largest value possible. Early binding allows you to speed your program up a lot in most areas. StarCraft II Pretty much everything requires early binding, and when it does not, it fakes late binding. This is because the Scope of Galaxy Script is only the possibilities.
One area where galaxy script fakes late binding is with Dialog Controls. It is multiple different types of dialog controls with only one object handler. The object handler is sized to the biggest type.
This is already long and maybe I’ll post more but.
Size matters, so using the right variable to hold the data is important.
Boolean[0] = True or False. 0 to 1
That’s one bit and very small.
Byte[0] = 0-255. 00 to FF
If you held True or False in a String, then.
“False”[4]Bite’s
“True”[3]Byte’s
As opposed to.
Bit False[0]Bit’s
Bit True[1]Bit’s
Bit False[0]Bit’s
I think he needs a brief introduction in to what "scope" is and how it relates to Global and local variables as well.....
Try googling.... "Global Variable" and "Local variable" You will find a wealth of knowledge. This is basic programing stuffs, its pretty educational.
Scope is very important when programming. SC2 Galixy Scope does not differ that much really.
Scope is a way of managing what can be seen from any point in code. Global variables that are created outside of a trigger are visible to all triggers. A local variable created within a trigger is only visible with the trigger, and when the trigger is finished, the variable is destroyed.
The variable being destroyed when the trigger is finished it a good thing. Knowing this means that we need to decide at any point when a variable needs to be global, or when it should be local to the trigger.
Let’s say you were keeping score. And you have a trigger that adds points to a players score. It would be bad to create the scoring variable local to the trigger. If we did, the score would be created, the points added, and then the score variable would be deleted. The next time the trigger tries to add points to the score, it would start over, and again delete the score.
If we did the same scenario with global variable for score. The trigger would start, it would add points to the score, and then the trigger would destroy itself when done. But our global variable of score is still intact outside of the trigger.
Let’s take this example and include both global and local variable for score.
First we have a global variable that is the total score. Then a trigger fires to add points to the score. It creates a local variable for adding up points. A number of math routines add numbers to the local variable based on how many units were killed, and what type of unit is worth how many points.
After the score for the trigger adding the points has work out its total. It then adds that local variables value of kill total, to the global variable of total score.
The trigger finishes and destroys the local kill total variable before exiting. The players total score as a global variable is left intact.
Back to the topic of scope, a trigger can see(Scope) any variables that are global. A trigger cannot see variables that are local to other triggers. So the scope for any trigger is itself and global.
Without going off topic into ways of sharing other triggers local variable values with other triggers. You should be able to make your map without using those techniques. When you understand these concepts, you can start to look for functions, parameters and such.
Not necessarily, but in most cases, variables help tremendously (mainly if you have two triggers and want to reference the same player in both, you'd have to create a variable).
Not necessarily, but in most cases, variables help tremendously (mainly if you have two triggers and want to reference the same player in both, you'd have to create a variable).
</blockquote>
A global Variable outside of any trigger, that holds the unit, and probably and array of units[16]. You can also use a DataTable. A DataTable is in program terms a Collection. I want to steer away from saying it is a collection, because the Galaxy DataTable is much more simply eposed.
So lets look at the datatable used for Collecting as a matrix.
First of all instead of thinking of the datatable as a square matrix, like an array is. We need to picture the DataTable as a box that contains things we cant touch. Now when aproaching the box, we speak to it, asking for something I know it has. This starts of like the card game go fish, but I am a cheater, and know what the box contains.
A datatable can hold anything. Even as simple as instructions on how to rebuild something. I have myself used datatables to redraw jpg’s from string arrays. So lets concider holding everything in the datatable, and knowing how to find it.
An Array is grid based, and hold one type of variable. A DataTable can do the same, but does not structure as a grid, instead we ask the person in the box to give us what we are looking for.
This being true, I will give an example of how we use, both types in combination.
Players are static, and referenced by 1 integer.
Player as integer[] = New Integer[7]
Please note: 7 is 8 {01234567}
Units are non linear, having units born and die. So an array does not make sense, as a place holder. But I want a quick way to check all my units. So I will still bring down my units to array level.
Lets say each player has 200 unit’s limit, with 8 players. First we will set those units to either Alive, or Not(Yes or No)
(Yes or No) is true or false, and in programming terms Boolean.
So I make an array bolUnits[7][199] as boolean
Within my “Trigger” create unit for player. I will search the array.
Local Variables
Player
Unit
UnitNumber[7] as integer ‘We need to set default value to 10. Giving each player 10 units
For each Player 0-7
For each Unit 0-199
If bolUnits[Player][Unit] is False Then
If UnitNumber[Player] > 0 then
‘Here we need to write our code for creating a unit. Lets look at it after the Next Player
End if
End if
Next Unit
Next Player
We step through each player, looking for if they have any more units to spawn. The rest of the values we are going to create will be in the datatable. We will have to name everything so that we can reverse get something back from the datable.
Lets look at how to 100% get a unit back. In the code area of “Here we need to write our code for creating a unit”. We are doing 2 things.
Visiting a Player, and visiting a free space for unit.
For naming we are going to do 4 things. Name what we are naming for both player and unit.
[“Player”][1][“Unit”][58]
As a string it will look like Player1Unit58
Some others may be Player5Unit32 and Player7Unit68
here is an example code: ‘ will create a unit and store it in a datatable.
Create unit facing whatever
Datatableunit.saveUnit = [Join Strings{[Player][1][Unit][58]}] = Last Created Unit
DatatableInteger.saveInteger = [Join Strings{[Player][1][Unit][58][“Level”]}] = 1
DatatablePoint.savePoint = [Join Strings{[Player][1][Unit][58][“Point”]}] = position of unit
DatatableInteger.saveInteger = [Join Strings{[Player][1][Unit][58][“Money”]}] = 30
DatatableReal.saveReal = [Join Strings{[Player][1][Unit][58][“Boost”]}] = 5.75
UnitNumber[Player] = UnitNumber[Player] -1
‘Now in order to get my unit back I can search the same way I created the unit. I Wont show this, but you will need to make a trigger for when unit dies trigger. And set its Global variable to False.
Trigger fires periodicly every 30 seconds. Unit gains a level.
<Local Variables>
Player
Unit
LocalVariableUnit
LocalVariableLevel
LocalVariableMoney
LocalVariableBoost
For each Player 0-7
For each Unit 0-199
If bolUnits[Player][Unit] is True Then
LocalVariableUnit = Datatableunit.GetUnit [Join Strings{[“Player”][Player][“Unit”][Unit]}]
LocalVariableLevel = DatatableInteger.GetInteger [Join Strings{[Player][1][Unit][58][“Level”]}] + 1
LocalVariableMoney = DatatableInteger.GetInteger [Join Strings{[Player][1][Unit][58][“Money”]}] + 10
LocalVariableBoost = DatatableReal.GetReal [Join Strings{[Player][1][Unit][58][“Boost”]}] + 7.5
Hey guys,
I have a couple questions about Variables, to me there very intimidating. >< I just need to know things like, what do they do? How do I know when to use them? Is it like data storage or something?
Variables are something that stores a value of a certain type. They come in 2 types: Global and Local. Global ones are created in the same pane as triggers and folders. They can be used by any trigger. Local variables are made inside triggers under the 'Variable' section. They are used by the trigger then dumped. If you want to save the value of that variable, you will need to set a global var to it before the trigger ends. You can only store int values under int variables etc, although you can convert some to other things.
Also, how can you not know what a variable is? :/
So what im getting is. If I need to use something more than once, I need to make a variable?
I think he needs a brief introduction in to what "scope" is and how it relates to Global and local variables as well.....
Try googling.... "Global Variable" and "Local variable" You will find a wealth of knowledge. This is basic programing stuffs, its pretty educational.
Scope is very important when programming. SC2 Galixy Scope does not differ that much really.
Scope is a way of managing what can be seen from any point in code. Global variables that are created outside of a trigger are visible to all triggers. A local variable created within a trigger is only visible with the trigger, and when the trigger is finished, the variable is destroyed.
The variable being destroyed when the trigger is finished it a good thing. Knowing this means that we need to decide at any point when a variable needs to be global, or when it should be local to the trigger.
Let’s say you were keeping score. And you have a trigger that adds points to a players score. It would be bad to create the scoring variable local to the trigger. If we did, the score would be created, the points added, and then the score variable would be deleted. The next time the trigger tries to add points to the score, it would start over, and again delete the score. If we did the same scenario with global variable for score. The trigger would start, it would add points to the score, and then the trigger would destroy itself when done. But our global variable of score is still intact outside of the trigger.
Let’s take this example and include both global and local variable for score. First we have a global variable that is the total score. Then a trigger fires to add points to the score. It creates a local variable for adding up points. A number of math routines add numbers to the local variable based on how many units were killed, and what type of unit is worth how many points. After the score for the trigger adding the points has work out its total. It then adds that local variables value of kill total, to the global variable of total score. The trigger finishes and destroys the local kill total variable before exiting. The players total score as a global variable is left intact.
Back to the topic of scope, a trigger can see(Scope) any variables that are global. A trigger cannot see variables that are local to other triggers. So the scope for any trigger is itself and global.
Without going off topic into ways of sharing other triggers local variable values with other triggers. You should be able to make your map without using those techniques. When you understand these concepts, you can start to look for functions, parameters and such.
Cheers, ItsAboutTime: Sldprt
@Reach0: Go
Not necessarily, but in most cases, variables help tremendously (mainly if you have two triggers and want to reference the same player in both, you'd have to create a variable).
<blockquote>
Quote from obliviron:
@Reach0: Go
Not necessarily, but in most cases, variables help tremendously (mainly if you have two triggers and want to reference the same player in both, you'd have to create a variable).
</blockquote>
A global Variable outside of any trigger, that holds the unit, and probably and array of units[16]. You can also use a DataTable. A DataTable is in program terms a Collection. I want to steer away from saying it is a collection, because the Galaxy DataTable is much more simply eposed.
So lets look at the datatable used for Collecting as a matrix.
First of all instead of thinking of the datatable as a square matrix, like an array is. We need to picture the DataTable as a box that contains things we cant touch. Now when aproaching the box, we speak to it, asking for something I know it has. This starts of like the card game go fish, but I am a cheater, and know what the box contains.
A datatable can hold anything. Even as simple as instructions on how to rebuild something. I have myself used datatables to redraw jpg’s from string arrays. So lets concider holding everything in the datatable, and knowing how to find it.
An Array is grid based, and hold one type of variable. A DataTable can do the same, but does not structure as a grid, instead we ask the person in the box to give us what we are looking for.
This being true, I will give an example of how we use, both types in combination.
Players are static, and referenced by 1 integer.
Player as integer[] = New Integer[7]
Please note: 7 is 8 {01234567}
Units are non linear, having units born and die. So an array does not make sense, as a place holder. But I want a quick way to check all my units. So I will still bring down my units to array level.
Lets say each player has 200 unit’s limit, with 8 players. First we will set those units to either Alive, or Not(Yes or No)
(Yes or No) is true or false, and in programming terms Boolean.
So I make an array bolUnits[7][199] as boolean
Within my “Trigger” create unit for player. I will search the array.
Local Variables
Player
Unit
UnitNumber[7] as integer ‘We need to set default value to 10. Giving each player 10 units
For each Player 0-7
For each Unit 0-199
If bolUnits[Player][Unit] is False Then
If UnitNumber[Player] > 0 then
‘Here we need to write our code for creating a unit. Lets look at it after the Next Player
End if
End if
Next Unit
Next Player
We step through each player, looking for if they have any more units to spawn. The rest of the values we are going to create will be in the datatable. We will have to name everything so that we can reverse get something back from the datable.
Lets look at how to 100% get a unit back. In the code area of “Here we need to write our code for creating a unit”. We are doing 2 things.
Visiting a Player, and visiting a free space for unit.
For naming we are going to do 4 things. Name what we are naming for both player and unit.
[“Player”][1][“Unit”][58]
As a string it will look like Player1Unit58
Some others may be Player5Unit32 and Player7Unit68
here is an example code: ‘ will create a unit and store it in a datatable.
Create unit facing whatever
Datatableunit.saveUnit = [Join Strings{[Player][1][Unit][58]}] = Last Created Unit
DatatableInteger.saveInteger = [Join Strings{[Player][1][Unit][58][“Level”]}] = 1
DatatablePoint.savePoint = [Join Strings{[Player][1][Unit][58][“Point”]}] = position of unit
DatatableInteger.saveInteger = [Join Strings{[Player][1][Unit][58][“Money”]}] = 30
DatatableReal.saveReal = [Join Strings{[Player][1][Unit][58][“Boost”]}] = 5.75
UnitNumber[Player] = UnitNumber[Player] -1
‘Now in order to get my unit back I can search the same way I created the unit. I Wont show this, but you will need to make a trigger for when unit dies trigger. And set its Global variable to False.
Trigger fires periodicly every 30 seconds. Unit gains a level.
<Local Variables>
Player
Unit
LocalVariableUnit
LocalVariableLevel
LocalVariableMoney
LocalVariableBoost
For each Player 0-7
For each Unit 0-199
If bolUnits[Player][Unit] is True Then
LocalVariableUnit = Datatableunit.GetUnit [Join Strings{[“Player”][Player][“Unit”][Unit]}]
LocalVariableLevel = DatatableInteger.GetInteger [Join Strings{[Player][1][Unit][58][“Level”]}] + 1
LocalVariableMoney = DatatableInteger.GetInteger [Join Strings{[Player][1][Unit][58][“Money”]}] + 10
LocalVariableBoost = DatatableReal.GetReal [Join Strings{[Player][1][Unit][58][“Boost”]}] + 7.5
LocalVariableUnit.IncreaseSize = LocalVariableUnit.Size + 2
DatatableInteger.saveInteger = [Join Strings{[Player][1][Unit][58][“Level”]}] = LocalVariableLevel
DatatablePoint.savePoint = [Join Strings{[Player][1][Unit][58][“Point”]}] = position of unit
DatatableInteger.saveInteger = [Join Strings{[Player][1][Unit][58][“Money”]}] = LocalVariableMoney
DatatableReal.saveReal = [Join Strings{[Player][1][Unit][58][“Boost”]}] = LocalVariableBoost
End if
Next Unit
Next Player
Oh so much where is the question now?