Looping through hits
#1

Hello,

I made a simple /contract system for hitmans using Dini.
I made a variable to all the players where the script can
see if the player is contracted or not. It's working good.
I want to make a command, something like /checkhits.
Only the hitmans can use this. I want it to loop through
all the players and check who is Contracted and how much.

All the players has got these variables:
--------------------------------------
PlayerInfo[playerid][pContracted]
PlayerInfo[playerid][pContractPrice]
--------------------------------------

So how can I make a command that it loops through all the
players and find out who is contracted or not?
Anyone any tips?

Thanks!
Reply
#2

pawn Code:
for(new i = 0; i < MAX_PLAYERS; ++i) // foreach(Player, i) < recommended
{
    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
    {
        if(PlayerInfo[i][pContracted] == 1)
        {
             // Code
        }
    }
}
Reply
#3

Quote:
Originally Posted by [L3th4l]
View Post
pawn Code:
for(new i = 0; i < MAX_PLAYERS; ++i) // foreach(Player, i) < recommended
{
    if(IsPlayerConnected(i) && !IsPlayerNPC(i))
    {
        if(PlayerInfo[i][pContracted] == 1)
        {
             // Code
        }
    }
}
Hmm.. Okay, thanks..
I used this but I don't know what to do in the // code place..
I tried making new variable and GetPlayerName(i, name, sizeof(name));
but it didn't work.. How can I do it otherwise?

Thanks.
Reply
#4

Using foreach.
pawn Code:
Foreach(Player, i)
{
    if(PlayerInfo[i][pContracted] == 1)
    {
        new str[MAX_PLAYER_NAME];
        GetPlayerName(i, str, MAX_PLAYER_NAME);
    }
}
Reply
#5

Here you go:

pawn Code:
stock listAllContracts(playerid) {
    new
        szPlayerName[MAX_PLAYER_NAME],
        szMessage[128];

    for(new i = 0; i < MAX_PLAYERS; i++) {
        if(IsPlayerConnected(i)) {
            if(PlayerInfo[i][pContracted] != 0) {
                GetPlayerName(i, szPlayerName, MAX_PLAYER_NAME);
                format(szMessage, sizeof(szMessage), "%s has a contract with the value of $%d.", szPlayerName, PlayerInfo[i][pContractPrice]);
                SendClientMessage(playerid, 0, szMessage);
            }
        }
    }
   
    return 1;
}
You can use this function to list all contracts which will display the contract price and person's name who has the contract for everyone who has a contract on them, the 'playerid' in the function is who will see the list of contracts.

Obviously I'd encourage the usage of foreach, but it's easier for you to keep this simple for now.
Reply
#6

Well, foreach isn`t that hard either ... I mean, it`s like:
pawn Code:
for(new i = 0; i < MAX_PLAYERS; i++)
{
        if(IsPlayerConnected(i))
        {
         
        }
}
changes to:
pawn Code:
foreach (Player,i)
{

}
It automatically 'spots' if the player is Connected. Right ?
Reply
#7

Yes, but the trouble with downloading an include when all the OP wants is a working loop displaying a result. Despite the fact that the ones of you who are promoting usage of foreach are probably doing it for the best, that's not what the OP asked for and normal loops still work and aren't THAT slow.
Reply
#8

Quote:
Originally Posted by Calg00ne
View Post
Yes, but the trouble with downloading an include.
Lmfao'd.
Agreed though, some people would rather not download and have a slow moe server...
Reply
#9

Yes, Calg00ne, I see your point but we all trying to help each other, learn new things. I mean, to be honest, I would prefer to be helped in understanding how to do something instead of just be given exactly what I asked for. If I ask for a specific thing and someone comes with a faster, better and easier way of doing it, why wouldn`t I use that way?

At least, that`s just me...

Anyway, sorry for my off topic / spam post.
Reply
#10

Quote:
Originally Posted by Calg00ne
View Post
Here you go:

pawn Code:
stock listAllContracts(playerid) {
    new
        szPlayerName[MAX_PLAYER_NAME],
        szMessage[128];

    for(new i = 0; i < MAX_PLAYERS; i++) {
        if(IsPlayerConnected(i)) {
            if(PlayerInfo[i][pContracted] != 0) {
                GetPlayerName(i, szPlayerName, MAX_PLAYER_NAME);
                format(szMessage, sizeof(szMessage), "%s has a contract with the value of $%d.", szPlayerName, PlayerInfo[i][pContractPrice]);
                SendClientMessage(playerid, 0, szMessage);
            }
        }
    }
   
    return 1;
}
You can use this function to list all contracts which will display the contract price and person's name who has the contract for everyone who has a contract on them, the 'playerid' in the function is who will see the list of contracts.

Obviously I'd encourage the usage of foreach, but it's easier for you to keep this simple for now.
Thanks, this worked for me!!

And thank you all guys!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)