I was having trouble with a trigger activated by an event that occurred more than once. I'm new to the editor.
Through analyzing my code, dropping in text outputs to see what some of my variables are when they're used, and a little luck, I learned that an important variable or two that needed to be different every time the trigger ran were the same every time the trigger ran.
That's when I realized that local variables are destroyed when the trigger ends, and created with their initial values again when the trigger starts again.
This is a heads up for anyone else who might be having problems caused by their local variables. The increments you do will disappear when the trigger is finished. You have to use global variables, as far as I know, if you want your increments and changes to be saved when the trigger ends.
EDIT: In a way, then, a trigger is like a function. Variables in its scope are only there for that one shot that it runs.
@Kueken531: Go
if you set first value to random int it does
-edit-
ok did tests and it's wrong, BUT i had some cases where this happened. that's why i always set the var in actions now, needs further testing maybe because of recursion in custom actions-
-edit- ok did tests and it's wrong, BUT i had some cases where this happened. that's why i always set the var in actions now, needs further testing maybe because of recursion in custom actions-
Maybe you checked the fixed random seed option in the editor? In this case, you would always get the same order of random numbers everytime you start the map. Other than that, I cannot think of a way you would always get the same random number besides an error in your code. So if it happens again, please post an example map.
@Amaroq64: Go
I might be using stone old strategies, but I use the same as I used in the Warcraft 3 editor, MUI.
Modify Integer[0] +1
Modify Integer[1] +1
Set Unit[Integer[0]] = Triggering Unit
Set Heal[Integer[0]] = 15.0
Set Time[Integer[0]] = 3.0
If ((Trigger Loop) is on) != true) Then
Turn (Trigger Loop) On
Then you make a new function, a loop. (The "Trigger Loop")
Every 1.00
Local Integer
For ever (Local Integer) from 1 to (Integer[0]) increasing by 1
If ((Unit[Loop Integer] != No Unit) Then
Modify Time[Loop Integer]: - 1.0 (minus one)
Set (Unit[Loop Integer]) Life to ((Life of (Unit[Loop Integer]) (Current)) + (Heal[Loop Integer]))
If (Time[Loop Integer] <= 0.0) Then
Modify Integer[1]: - 1
Set Unit[Loop Integer] = No Unit
If (Integer[1] == 0) Then
Set Integer[0] == 0
Turn (current trigger) Off
This sets a new integer each time you use the spell and stores values into a seperate Array each time, then it does its actions in a loop that goes through all the arrays made. The Integer[1] is to check if they are still going, when an action is finnished, the time reaches 0, the unit valiarble is set to "no unit" and it will therefor not trigger anymore in the loop as there is a condition that checks if there is a Unit[Loop Integer]. The Integer[1] goes up eachtime someone uses a spell and goes down each time the spell is finished, so if noone is using the effect atm. the loop will therefor end and the Integer[0] reset to 0 to then start over.
If I understand you correctly, you're running a loop in a function to preserve the local variables. You call the function, and the loop starts and checks conditions and keeps looping. That instance of the function continues to exist while the loop is going, and the local variables to that function get preserved. Then when you don't need them anymore, you can let the loop end, the instance of the function is destroyed, and the local variables are destroyed.
That seems really elegant. The programmer in me didn't like the idea that I was using a global variable that I may only need a few times. But it was the only solution I saw.
I haven't tried messing around with functions in the editor yet. I've just been using data for everything, and triggers for what the data can't do.
Would the function run in its own thread so it doesn't block anything while it's looping? Or I suppose a trigger could call it. Triggers can run asynchronously I think. (The action to run a trigger can let execution of the current trigger continue without waiting for the other trigger to finish.)
You can also pass local variables into parameters for action definitions, which can be set to run in their own thread. This would be another way to maintain the values stored in local variables outside of the function which contains them, though any changes to the local variables which occur after passing them as parameters would be lost.
I was having trouble with a trigger activated by an event that occurred more than once. I'm new to the editor.
Through analyzing my code, dropping in text outputs to see what some of my variables are when they're used, and a little luck, I learned that an important variable or two that needed to be different every time the trigger ran were the same every time the trigger ran.
That's when I realized that local variables are destroyed when the trigger ends, and created with their initial values again when the trigger starts again.
This is a heads up for anyone else who might be having problems caused by their local variables. The increments you do will disappear when the trigger is finished. You have to use global variables, as far as I know, if you want your increments and changes to be saved when the trigger ends.
EDIT: In a way, then, a trigger is like a function. Variables in its scope are only there for that one shot that it runs.
Thats exactly the point of local variables :D
jeah and also if you have a local int = random int it will be init as the same number again and again.
nope, that should not happen.
@Kueken531: Go if you set first value to random int it does
-edit- ok did tests and it's wrong, BUT i had some cases where this happened. that's why i always set the var in actions now, needs further testing maybe because of recursion in custom actions-Maybe you checked the fixed random seed option in the editor? In this case, you would always get the same order of random numbers everytime you start the map. Other than that, I cannot think of a way you would always get the same random number besides an error in your code. So if it happens again, please post an example map.
@Amaroq64: Go I might be using stone old strategies, but I use the same as I used in the Warcraft 3 editor, MUI.
Modify Integer[0] +1
Modify Integer[1] +1
Set Unit[Integer[0]] = Triggering Unit
Set Heal[Integer[0]] = 15.0
Set Time[Integer[0]] = 3.0
If ((Trigger Loop) is on) != true) Then
Turn (Trigger Loop) On
Then you make a new function, a loop. (The "Trigger Loop")
Every 1.00
Local Integer
For ever (Local Integer) from 1 to (Integer[0]) increasing by 1
If ((Unit[Loop Integer] != No Unit) Then
Modify Time[Loop Integer]: - 1.0 (minus one)
Set (Unit[Loop Integer]) Life to ((Life of (Unit[Loop Integer]) (Current)) + (Heal[Loop Integer]))
If (Time[Loop Integer] <= 0.0) Then
Modify Integer[1]: - 1
Set Unit[Loop Integer] = No Unit
If (Integer[1] == 0) Then
Set Integer[0] == 0
Turn (current trigger) Off
This sets a new integer each time you use the spell and stores values into a seperate Array each time, then it does its actions in a loop that goes through all the arrays made. The Integer[1] is to check if they are still going, when an action is finnished, the time reaches 0, the unit valiarble is set to "no unit" and it will therefor not trigger anymore in the loop as there is a condition that checks if there is a Unit[Loop Integer]. The Integer[1] goes up eachtime someone uses a spell and goes down each time the spell is finished, so if noone is using the effect atm. the loop will therefor end and the Integer[0] reset to 0 to then start over.
If I understand you correctly, you're running a loop in a function to preserve the local variables. You call the function, and the loop starts and checks conditions and keeps looping. That instance of the function continues to exist while the loop is going, and the local variables to that function get preserved. Then when you don't need them anymore, you can let the loop end, the instance of the function is destroyed, and the local variables are destroyed.
That seems really elegant. The programmer in me didn't like the idea that I was using a global variable that I may only need a few times. But it was the only solution I saw.
I haven't tried messing around with functions in the editor yet. I've just been using data for everything, and triggers for what the data can't do.
Would the function run in its own thread so it doesn't block anything while it's looping? Or I suppose a trigger could call it. Triggers can run asynchronously I think. (The action to run a trigger can let execution of the current trigger continue without waiting for the other trigger to finish.)
@Amaroq64: Go
You can also pass local variables into parameters for action definitions, which can be set to run in their own thread. This would be another way to maintain the values stored in local variables outside of the function which contains them, though any changes to the local variables which occur after passing them as parameters would be lost.
Ooh, that's a great idea too. Thank you.