Frictional Games Forum (read-only)

Full Version: Multiple conditions in one "if" statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
So, basically I'm moving into DIFFERENT! scripting for me and I've struck a dead end. Without spoiling too much, I need to be able to check two conditions in one if statement. Is there any way to accomplish this?

Consider this script:
Code:
void Function(string &in asEntityName, int alState)
{
    if (GetLocalVarInt("Var01") == 1) && (GetLocalVarInt("Var02") == 1)
    {    
               //Do stuff
        return;
    }

    else if (GetLocalVarInt("Var01") == 1) && (GetLocalVarInt("Var02") == 2)
    {    
               //Do other stuff
        return;
    }
}

Apparently, the && doesn't work (I read that it does in C, probably just not in angelscript) or did I do it all wrong? Smile

Thanks in advance.
Congratz! Well, first off, you can simply try this:

Code:
void Function(string &in asEntity, int alState)
{
     if (GetLocalVarInt("Var01") == 1)
     {
          //Do stuff
          return;
     }
     else if (GetLocalVarInt("Var01") == 2)
     {
          //Do stuff
          return;
     }
}

There are many ways to do it.

Code:
void Function(string &in asEntity, int alState)
{
     if (GetLocalVarInt("Var01") == 1)
     {
          //Do stuff
          return;
     }
     else
     {
          //Do stuff
          return;
     }
}

The "&&" (and) and "||" (or) work perfectly the same way as of C++ and Angelscript. It's just that you weren't using it correctly. You can try this if the local variable "var01" equals 1 and the local variable "var02" equals 1.

Code:
void Function(string &in asEntity, int alState)
{
     if ((GetLocalVarInt("Var01") == 1) && (GetLocalVarInt("Var02") == 1))
     {
          //Do stuff
          return;
     }
}

And also to add a little note here, the "else if" checks to see to check something to see if it's true, if the first part, the "if", is false. To be honest, this is intermediate scripting. Tongue
Thanks a lot Kyle, the last part was just what I wanted Smile Forgot two brackets...
Yes, && is the proper operator.

Also, there are lots of ways to use if statements, especially with multiple and dynamic conditions like this case.

You can do

Code:
if(GetLocalVarInt("Var01") == 1) {
    switch(GetLocalVarInt("Var02")) {
        case 1:
            //Cool stuff
        break;
        case 2:
            //Other cool stuff
        break;
    }}

There are at least 3 other neat ways to do that too, but this construct makes it easy to edit things as a result of the second variable's value.
(07-20-2011, 03:54 PM)palistov Wrote: [ -> ]Yes, && is the proper operator.

Also, there are lots of ways to use if statements, especially with multiple and dynamic conditions like this case.

You can do

Code:
if(GetLocalVarInt("Var01") == 1) {
    switch(GetLocalVarInt("Var02")) {
        case 1:
            //Cool stuff
        break;
        case 2:
            //Other cool stuff
        break;
    }}

There are at least 3 other neat ways to do that too, but this construct makes it easy to edit things as a result of the second variable's value.

:o what does "switch" do? and is that the proper way of writing that? with "case 1:" and "case2:" i mean. i never used cases and "switch" before
Thanks Kyle, that tutorial helped a lot! Smile
(07-20-2011, 06:01 PM)convolution223 Wrote: [ -> ]Thanks Kyle, that tutorial helped a lot! Smile

No problem, I try to help all I can. Smile
Sorry, this is not advanced scripting, you're still at the beginner level.

It would help if you knew C/++ beforehand, but it looks like you're learning as you go.

The first two examples that Kyle posted in his first post are incorrect as they do not have the same effect. Also, you do not need the return statement really, its a void function to begin with.
(07-20-2011, 06:14 PM)xiphirx Wrote: [ -> ]Sorry, this is not advanced scripting, you're still at the beginner level.

It would help if you knew C/++ beforehand, but it looks like you're learning as you go.

The first two examples that Kyle posted in his first post are incorrect as they do not have the same effect. Also, you do not need the return statement really, its a void function to begin with.

Excuse you, I know C++ already and I know they don't have the same effect. Of course I know that the second one has the effect of, in this case, that if the first "if" statement is incorrect, then the second one works nevertheless. I just didn't point out that in the second case that it could be applied to a statement that includes more than 2 values. Also, please try to be nice, I know that it's not advanced, I just want him to feel good and confident to actually reach that level and not sink back and become careless by no motivation. :/

About that "return" statement, it could be needed if the "if" statement is added on with the factor of having something in the case if none of the "if" statements are true.
Pages: 1 2