Counting how many variables are set to true or false

Does CS have a command that can run through a bunch of true/false variables, and count how many are set to true (or false)?

For example, if I have this bunch:

*create variable1 false
*create variable2 false
*create variable3 false
*create variable4 false

Is there a command that does this? I’m thinking of a code (or its output) that can be used to quickly check for “if at least 2 out of 4” are true. (or 1 out of 4, or 3 out of 5, etc)

Or is it something that has to be done manually?

*temp counter
*if (variable1 = true)
    *set counter +1

and so on?

1 Like

You have to do it manually, exactly as you suggested.


Yep, that’s how I’d do it.

Wouldn’t use *else, though. Just *ifs.


Just ifs? Isn’t with every if, you also need an elseif or an else that covers the other variables too?
So far, if I have an if true statement, there is always an elseif false and/or just else following it (and vice versa, same thing goes with a text string). I don’t have ICF turned on, if that makes a difference.

Nope. *ifs can stand alone. Flow drops right out of them in that case, with or without ICF. That lets you write simpler code.

This case, though, with variables named so similarly, is a good candidate to use a simple loop to minimize the chance of error.


*create variable_1 false
*create variable_2 false
*create variable_3 false
*create variable_4 false
*create MAX_VARS 4

scene file:

*temp counter 0
*temp x 0

*label Top_Of_Count_Vars
*set x + 1
*if variable[x]
   *set counter + 1
*if x < MAX_VARS
   *goto Top_Of_Count_Vars

You have @{(counter+1) zero|one|two|three|all four} variable@{(counter = 1) |s} set to 'true'.

*if, *elseif, and *else mean “If this condition is met do this. However, if that doesn’t work, do this instead. And if nothing else works, do this.”

They’re exclusive – only one command would trigger. You want the game to run through all of them and check them individually. With elseif etc., it would only check one and then skip the rest.


What Will said, but also, elseif false specifically is identical in meaning to just else, which makes it redundant. For example:

*create door_open false

*if (door_open)
    "The door is open, come right in!" 
    You knock politely on the closed door and wait for an answer. 

You would use elseif for string or numeric variables, that can have more than one value. For example:

*create final_grade 100

*if (final_grade >= 85)
    You did amazingly well! 
*elseif (final_grade >= 55)
    You passed. 
    You failed. 
1 Like

Got it. I should be able to streamline some stuff going forward then.

I’ve never thought of using multireplace. Those variable names are just examples in this case, but if I ever name my variable like that, I’ll keep this in mind!

And for the elseif/else example… Perhaps I should’ve chosen something that isn’t true/false for that - I often use else after elseifs to check if I’ve forgotten anything and made a bug when doing a string comparison.


This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.