Roenlond
Senior Member
Posts: 331
Threads: 3
Joined: Apr 2011
Reputation:
0
|
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?
Thanks in advance.
(This post was last modified: 07-21-2011, 11:35 AM by Roenlond.)
|
|
07-20-2011, 03:27 PM |
|
Kyle
Posting Freak
Posts: 911
Threads: 36
Joined: Sep 2010
Reputation:
7
|
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.
(This post was last modified: 07-20-2011, 03:37 PM by Kyle.)
|
|
07-20-2011, 03:31 PM |
|
Roenlond
Senior Member
Posts: 331
Threads: 3
Joined: Apr 2011
Reputation:
0
|
RE: Multiple conditions in one "if" statement
Thanks a lot Kyle, the last part was just what I wanted Forgot two brackets...
|
|
07-20-2011, 03:38 PM |
|
palistov
Posting Freak
Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation:
57
|
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 |
|
convolution223
Member
Posts: 78
Threads: 15
Joined: Jul 2011
Reputation:
0
|
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 |
|
Kyle
Posting Freak
Posts: 911
Threads: 36
Joined: Sep 2010
Reputation:
7
|
RE: Multiple conditions in one "if" statement
|
|
07-20-2011, 05:53 PM |
|
convolution223
Member
Posts: 78
Threads: 15
Joined: Jul 2011
Reputation:
0
|
RE: Multiple conditions in one "if" statement
Thanks Kyle, that tutorial helped a lot!
|
|
07-20-2011, 06:01 PM |
|
Kyle
Posting Freak
Posts: 911
Threads: 36
Joined: Sep 2010
Reputation:
7
|
RE: Multiple conditions in one "if" statement
(07-20-2011, 06:01 PM)convolution223 Wrote: Thanks Kyle, that tutorial helped a lot!
No problem, I try to help all I can.
|
|
07-20-2011, 06:02 PM |
|
xiphirx
Senior Member
Posts: 662
Threads: 16
Joined: Nov 2010
Reputation:
5
|
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 |
|
Kyle
Posting Freak
Posts: 911
Threads: 36
Joined: Sep 2010
Reputation:
7
|
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 |
|
|