For loop must nests if and else
#1

I was scripting my stuff, but it didn't work at all, then I figured and created that but the problem didn't solve at all My Problem Is This:
I use a for loop for checking a variable arrays like this
PHP код:
for (new i5i++)
{
          if(
GetPlayerVehicleid(playerid) == i) {
                       
//Some code here
           
}
           else 
sendclientmessage(playerid, -1"Not right");

Now i want to check that if the player's vehicle is from any of the 5 numbers whether 1,2,3 etc but for ex: the player's vehicle id is 3. He will get 3 msgs saying "Not right" because the for loop is in work and if if nest for loop before else statement it will give me error:: Invalid Expression, assumed zero.
I nest like this:
PHP код:
for (new i5i++)
{
          if(
GetPlayerVehicleid(playerid) == i) {
                       
//Some code here
           
}
}         else  
//some code here 
Reply
#2

PHP код:
new bool:found=false;
for(new 
i=1,id=GetPlayerVehicleid(playerid);i<=5;i++)
{
      if(
id == i
        { 
              
found=true;
              break;
        } 
}
if(
found)
{
//some code
}
else
//send not right 
Is that ^^ you meant?
Reply
#3

PHP код:
new pass;
for (new 
i5i++)
{
    if(
GetPlayerVehicleid(playerid) == i)
    {
        
//Some code here
        
pass 1;
        break;
     }
}
if(
pass == 0SendClientMessage(playerid, -1"Not right"); 
// oops, too late
Reply
#4

Thanks.
Reply
#5

Create a variable called invehicle or something and change its value if the player's vehicle ID matches. After the loop check if the invehicle variable is still 0, if it is send that "not right" message. Also vehicle ID's start from 1.
Something like that:
Код:
new vehicleid = GetPlayerVehicleID(playerid), invehicle;

for (new i = 1; i < 5; i++)
{
          if (vehicleid == i) 
          {
                   invehicle = i;
                   //other stuff
          }
}
if (!invehicle) SendClientMessage(playerid, -1, "Not right");
EDIT: I'm late.
Reply
#6

Right, this also occurs with me also. Whatever +rep.
Reply
#7

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Create a variable called invehicle or something and change its value if the player's vehicle ID matches. After the loop check if the invehicle variable is still 0, if it is send that "not right" message. Also vehicle ID's start from 1.
Something like that:
Код:
new vehicleid = GetPlayerVehicleID(playerid), invehicle;

for (new i = 1; i < 5; i++)
{
          if (vehicleid == i) 
          {
                   invehicle = i;
                   //other stuff
          }
}
if (!invehicle) SendClientMessage(playerid, -1, "Not right");
EDIT: I'm late.
IMHO it's bad practice to treat an integer like it is a boolean "!number", ofc it works, but it might be confusing
Reply
#8

Quote:
Originally Posted by [Bios]Marcel
Посмотреть сообщение
IMHO it's bad practice to treat an integer like it is a boolean "!number", ofc it works, but it might be confusing
There's no difference between bools and integers, only that you can only set the bool to true (1) and false (0) while you can set the integer to another value. You can also do
Код:
integer = true;
integer = false;
so there's absolutely no difference between bools and integers. I always do that true/false thing with integers, I never use bools. And yes
Код:
if (!integer)
is the same thing as
Код:
if (integer == 0)
Nothing confusing there.
Reply
#9

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
There's no difference between bools and integers, only that you can only set the bool to true (1) and false (0) while you can set the integer to another value. You can also do
Код:
integer = true;
integer = false;
so there's absolutely no difference between bools and integers. I always do that true/false thing with integers, I never use bools. And yes
Код:
if (!integer)
is the same thing as
Код:
if (integer == 0)
Nothing confusing there.
Reread my reply ... i said it works but it's bad practise
Reply
#10

Quote:
Originally Posted by [Bios]Marcel
Посмотреть сообщение
Reread my reply ... i said it works but it's bad practise
How is it bad if bools and integers are exactly the same things?
Also I forgot to mention that if you do
Код:
if (integer)
then it's the same thing as
Код:
if (integer < 0 || integer > 0)
Let's say integer is 5 or -1 and if you do
Код:
if (integer)
it will do the code under it as integer is less or more than 0.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)