Array help.
#1

Hello, I need help getting some array values to make my code smaller. Here's what I mean:

This is my code
Код:
    if(issuerid != INVALID_PLAYER_ID && weaponid == 34 && bodypart == 9)
    {
        SetPlayerHealth(playerid, 0.0);
 }
    if(issuerid != INVALID_PLAYER_ID && weaponid == 25 && bodypart == 9)
    {
  SetPlayerHealth(playerid, 0.0);
 }
    if(issuerid != INVALID_PLAYER_ID && weaponid == 27 && bodypart == 9)
    {
  SetPlayerHealth(playerid, 0.0);
 }
    if(issuerid != INVALID_PLAYER_ID && weaponid == 26 && bodypart == 9)
    {
  SetPlayerHealth(playerid, 0.0);
 }
 if(issuerid != INVALID_PLAYER_ID && weaponid == 30 && bodypart == 9)
    {
   SetPlayerHealth(playerid, 0.0);
    }
    if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9)
    {
   SetPlayerHealth(playerid, 0.0);
 }
  if(issuerid != INVALID_PLAYER_ID && weaponid == 24 && bodypart == 9)
    {
     SetPlayerHealth(playerid, 0.0);
 }
I want to use something like
Код:
new array[x] = {24,25,34,etc,etc...}
if(issuerid != INVALID_PLAYER_ID && weaponid == arrayvalues && bodypart == 9)
{
}
I've already tried
Код:
 new array[7];
 array = {24, 25, 26, 27, 30, 31, 34};
 new i;

 for (i=0; i<7; i++)
 {
 }

 if(issuerid != INVALID_PLAYER_ID && weaponid == array[i] && bodypart == 9)
    {
        SetPlayerHealth(playerid, 0.0);
 }
But it won't work because the values can only be used inside the for function and I need to use them in the if function.

And I've also tried
Код:
 if(issuerid != INVALID_PLAYER_ID && weaponid == 31 || 30 || 24 || 34 && bodypart == 9)
    {
     SetPlayerHealth(playerid, 0);
 }
But it doesn't work.
Any help? Thanks in advance.
Reply
#2

Use switch it should work fine

pawn Код:
switch(weaponid) { }
Reply
#3

Try one of this:

pawn Код:
if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9) { SetPlayerHealth(playerid, 0); }
pawn Код:
if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9) return SetPlayerHealth(playerid, 0);
pawn Код:
if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9 || issuerid != INVALID_PLAYER_ID && weaponid == 30 && bodypart == 9 || issuerid != INVALID_PLAYER_ID && weaponid == 24 && bodypart == 9)
Edit: Sorry if something is bad, im in phone
Reply
#4

Quote:
Originally Posted by Stanford
Посмотреть сообщение
Use switch it should work fine

pawn Код:
switch(weaponid) { }
Thank you, this worked.

Quote:
Originally Posted by FullCircle
Посмотреть сообщение
Try one of this:

pawn Код:
if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9) { SetPlayerHealth(playerid, 0); }
pawn Код:
if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9) return SetPlayerHealth(playerid, 0);
pawn Код:
if(issuerid != INVALID_PLAYER_ID && weaponid == 31 && bodypart == 9 || issuerid != INVALID_PLAYER_ID && weaponid == 30 && bodypart == 9 || issuerid != INVALID_PLAYER_ID && weaponid == 24 && bodypart == 9)
Edit: Sorry if something is bad, im in phone
The first two were not what I wanted to do, I just wanted to simplify the ammount of "ifs". I don't know if the third one worked but it was still very long, the switch function made everything smaller. Regardless, thanks for trying to help.
Reply
#5

your for loop works fine, you should place the if statement in there, and break it if there's a match, no need to go through the other 7 if there's a match on the first one
Reply
#6

pawn Код:
if(issuerid != INVALID_PLAYER_ID && bodypart == 9)
    {
        switch(weaponid) { case 24 .. 27, 30, 31, 34: SetPlayerHealth(playerid, 0.0); }
    }
Sources:
https://sampwiki.blast.hk/wiki/Switch#switch
Reply
#7

Its Working For Me I Hope its Working For You also Try that

PHP код:
            if(issuerid != INVALID_PLAYER_ID && (weaponid == 34 || weaponid == 35 ||  weaponid == 27 ||weaponid == 26 || weaponid == 30 || weaponid == 31 ||weaponid == 24) && bodypart == 9)
            {
             
SetPlayerHealth(playerid,0);
                
GameTextForPlayer(playerid"~r~Headshotted"30003);
              } 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)