Jump to content
Sign in to follow this  
igneous01

need help updating a variable to reuse within a switch structure

Recommended Posts

ok so basically im having a hard time trying to get my variable, counter to keep its value at the end of a certain script.

basically its a script that is triggered by addaction from the menu, a variable called _counter = 0

from then on

_counter = _counter + 1

; this switch structure is executed numerically from 1 to 10, meaning the more times the action is activated, a different code is executed, until 10, where it returns to code for 1 at the next action

switch (_counter) do

{

case 1:

{code for 1}

case 2:

{code for 2}

....

case 10:

{

code for 10

_counter = 0 ; the counter resets itself

}

; these lines meant for debugging

case >10:

{hint "number is outside of case range"}

else

hint "number is unknown"

_______END OF CODE________

eventually i clued in on the fact that the variable is destroyed when the script finishes, so i tried using a global variable afterwards and giving it a value of 0 in the init.sqf and doing the same thing

but i keep getting the hint from my debug "number is unknown" as soon as i use the action, the code from the first case goes through, but nothing happens afterwards.

i assume that the value becomes nil since it is erased at the end of the script, but how do i keep this value throughout the game so that the rest of the case statements are executed?

from my past experiences with programming i thought this would be a simple task, variable declared 0 at the beginning, use a loop function to process all commands and code, and keep the variable constantly counting when the action is pressed.

except when i use the while loop, the script is processed multiple times at the same time, and i still got the same error message "number is unknown"

so can anyone help me with this really easy fix? is there a command that i didnt notice that can pass the value from the script into the global variable? because i never got it to update after the first time.

thanks for any help you can offer

Share this post


Link to post
Share on other sites

SQS... eeeww.

Init of soldier:

myCounter = 0; this addAction["Counter", "counter.sqf"];

counter.sqf:

switch (myCounter) do
{
case 0: {hintSilent "Case 0"};
case 1: {hintSilent "Case 1"};
case 2: {hintSilent "Case 2"};
case 3: {hintSilent "Case 3"};
case 4: {hintSilent "Case 4"};
case 5: {hintSilent "Case 5"};
case 6: {hintSilent "Case 6"};
case 7: {hintSilent "Case 7"};
case 8: {hintSilent "Case 8"};
case 9: {hintSilent "Case 9"};									
case 10: {myCounter = -1; hint "resetting"};
};

myCounter = myCounter + 1;

Share this post


Link to post
Share on other sites
SQS... eeeww.

Init of soldier:

myCounter = 0; this addAction["Counter", "counter.sqf"];

counter.sqf:

switch (myCounter) do
{
case 0: {hintSilent "Case 0"};
case 1: {hintSilent "Case 1"};
case 2: {hintSilent "Case 2"};
case 3: {hintSilent "Case 3"};
case 4: {hintSilent "Case 4"};
case 5: {hintSilent "Case 5"};
case 6: {hintSilent "Case 6"};
case 7: {hintSilent "Case 7"};
case 8: {hintSilent "Case 8"};
case 9: {hintSilent "Case 9"};									
case 10: {myCounter = -1; hint "resetting"};
};

myCounter = myCounter + 1;

so was it because its sqs that it didnt work?

if thats true then i guess im moving onto sqf permanently then if stuff like this doesnt process

thanks alot for this, u have no idea how ive been pondering how to make this work

Share this post


Link to post
Share on other sites

Nah, it would work just about the same in SQS, but SQS is obsolete now except for very few uses so you'll want to be learning SQF and using it for your scripts. :)

There's other ways to do what you're trying to do as well, that's just one example.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×