Looping through hits -
Ihsan-Cingisiz - 17.03.2011
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!
Re: Looping through hits - [L3th4l] - 17.03.2011
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
}
}
}
Re: Looping through hits -
Ihsan-Cingisiz - 17.03.2011
Quote:
Originally Posted by [L3th4l]
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.
Re: Looping through hits -
maramizo - 18.03.2011
Using foreach.
pawn Code:
Foreach(Player, i)
{
if(PlayerInfo[i][pContracted] == 1)
{
new str[MAX_PLAYER_NAME];
GetPlayerName(i, str, MAX_PLAYER_NAME);
}
}
Re: Looping through hits -
Calgon - 18.03.2011
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.
Re: Looping through hits -
antonio112 - 18.03.2011
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:
It automatically 'spots' if the player is Connected. Right ?
Re: Looping through hits -
Calgon - 18.03.2011
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.
Re: Looping through hits -
maramizo - 18.03.2011
Quote:
Originally Posted by Calg00ne
Yes, but the trouble with downloading an include.
|
Lmfao'd.
Agreed though, some people would rather not download and have a slow moe server...
Re: Looping through hits -
antonio112 - 18.03.2011
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.
Re: Looping through hits -
Ihsan-Cingisiz - 19.03.2011
Quote:
Originally Posted by Calg00ne
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!