Facebook Twitter YouTube Frictional Games | Forum | Newsletter | Dev Blog | Dev Wiki | Support


Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple conditions in one "if" statement
Author Message
Roenlond Offline
Senior Member

Posts: 331
Joined: Apr 2011
Reputation: 0
Post: #1
Multiple conditions in one "if" statement

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:
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.
(This post was last modified: 07-21-2011 11:35 AM by Roenlond.)
07-20-2011 03:27 PM
Find all posts by this user Quote this message in a reply
Kyle Offline
Posting Freak

Posts: 910
Joined: Sep 2010
Reputation: 7
Post: #2
RE: Multiple conditions in one "if" statement

Congratz! Well, first off, you can simply try this:

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.

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.

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

(This post was last modified: 07-20-2011 03:37 PM by Kyle.)
07-20-2011 03:31 PM
Find all posts by this user Quote this message in a reply
Roenlond Offline
Senior Member

Posts: 331
Joined: Apr 2011
Reputation: 0
Post: #3
RE: Multiple conditions in one "if" statement

Thanks a lot Kyle, the last part was just what I wanted Smile Forgot two brackets...
07-20-2011 03:38 PM
Find all posts by this user Quote this message in a reply
palistov Offline
Posting Freak

Posts: 1,213
Joined: Mar 2011
Reputation: 57
Post: #4
RE: Multiple conditions in one "if" statement

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

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.

(This post was last modified: 07-20-2011 03:56 PM by palistov.)
07-20-2011 03:54 PM
Find all posts by this user Quote this message in a reply
convolution223 Offline
Member

Posts: 78
Joined: Jul 2011
Reputation: 0
Post: #5
RE: Multiple conditions in one "if" statement

(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

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
07-20-2011 05:43 PM
Find all posts by this user Quote this message in a reply
Kyle Offline
Posting Freak

Posts: 910
Joined: Sep 2010
Reputation: 7
Post: #6
RE: Multiple conditions in one "if" statement

This should help you. http://www.cprogramming.com/tutorial/lesson5.html

07-20-2011 05:53 PM
Find all posts by this user Quote this message in a reply
convolution223 Offline
Member

Posts: 78
Joined: Jul 2011
Reputation: 0
Post: #7
RE: Multiple conditions in one "if" statement

Thanks Kyle, that tutorial helped a lot! Smile
07-20-2011 06:01 PM
Find all posts by this user Quote this message in a reply
Kyle Offline
Posting Freak

Posts: 910
Joined: Sep 2010
Reputation: 7
Post: #8
RE: Multiple conditions in one "if" statement

(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

07-20-2011 06:02 PM
Find all posts by this user Quote this message in a reply
xiphirx Offline
Senior Member

Posts: 662
Joined: Nov 2010
Reputation: 5
Post: #9
RE: Multiple conditions in one "if" statement

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
Find all posts by this user Quote this message in a reply
Kyle Offline
Posting Freak

Posts: 910
Joined: Sep 2010
Reputation: 7
Post: #10
RE: Multiple conditions in one "if" statement

(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.

07-20-2011 06:23 PM
Find all posts by this user Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)