PDA

View Full Version : Need help with code


Qbao
11/08/2017, 07:33 AM
Sorry for my bad english
I'm a code code ( wiki ):
public OnPlayerGiveDamage(playerid, damagedid, Float: amount, weaponid, bodypart)
{
new string[128], victim[MAX_PLAYER_NAME], attacker[MAX_PLAYER_NAME];
new weaponname[24];
GetPlayerName(playerid, attacker, sizeof (attacker));
GetPlayerName(damagedid, victim, sizeof (victim));

GetWeaponName(weaponid, weaponname, sizeof (weaponname));
format(string, sizeof(string), "%s has made %.0f damage to %s, weapon: %s, bodypart: %d", attacker, amount, victim, weaponname, bodypart);
SendClientMessageToAll(0xFFFFFFFF, string);
return 1;
}
Who help me a command can be turned off or on (if turned off is not displayed)
- thanks :p:p:p:p:p

OsteeN
11/08/2017, 07:47 AM
So you want a command that will toggle the damage messages?
Is this supposed to be a command that toggles it globally or personally? As in, if I toggle it, will it only turn it off for me, or for everyone?

FailerZ
11/08/2017, 07:50 AM
On top of the script declare a new boolean variable like this to hold the state
new bool:InfoMessage = true; //This is the default value of it when first decalred

Manage that in your command:
CMD:toggleinfo(playerid, params[])
{
if(InfoMessage == false)
{
SendClientMessageToAll(-1, "The information message is now on!");

InfoMessage = true; //We toggle it on (set to true)
}
else
{
SendClientMessageToAll(-1, "The information message is now off!");

InfoMessage = false; //We toggle it off (set to false)
}
return 1;
}

Now last put all your code in that OnPlayerGiveDamage call back inside an if statement
if(InfoMessage == true)
{
//Put your codes here
}

Qbao
11/08/2017, 08:41 AM
Just turn off the player to use the command

Qbao
11/08/2017, 08:48 AM
On top of the script declare a new boolean variable like this to hold the state
new bool:InfoMessage = true; //This is the default value of it when first decalred

Manage that in your command:
CMD:toggleinfo(playerid, params[])
{
if(InfoMessage == false)
{
SendClientMessageToAll(-1, "The information message is now on!");

InfoMessage = true; //We toggle it on (set to true)
}
else
{
SendClientMessageToAll(-1, "The information message is now off!");

InfoMessage = false; //We toggle it off (set to false)
}
return 1;
}

Now last put all your code in that OnPlayerGiveDamage call back inside an if statement
if(InfoMessage == true)
{
//Put your codes here
}
Thank you

Qbao
11/08/2017, 08:56 AM
So you want a command that will toggle the damage messages?
Is this supposed to be a command that toggles it globally or personally? As in, if I toggle it, will it only turn it off for me, or for everyone?

Just turn off the player to use the command

FailerZ
11/08/2017, 09:03 AM
If you want it per player you need to make an array to hold the ids of the players.
Do the following changes I colored them in red:
new bool:InfoMessage[MAX_PLAYERS] = true;

Then change every variable of InfoMessage to InfoMessage[playerid]

but then you don't need to send message to all the players under onplayergivedamage.
Instead you need to create a loop to check who has it on or off.
Like this:
for(new i, j = GetPlayerPoolSize(); i <= j; i++) //This will loop through all the players in the server
{
if(InfoMessage[i] == true) //This will check the state of the info message for each player (i) in if statement to check if it is on (true)
{
//DO the codes here but SendClientMessage to i (the player who has the info message on)
SendClientMessage(i, -1, "Player bla bla gave damage......"); //Send formatted message instead of this
}
}

EDIT: Don't forget to set the variable to false/true when the player disconnect because if player joined with the same id of the previous player and the prev player has it on he will have it on too so just default value when the player connects/disconnects like

Under OnPlayerConnect or OnPlayerDisconnect doesn't matter really.
InfoMessage[playerid] = true; //True will make it show by default. change to false if you want otherwise

Qbao
11/08/2017, 09:26 AM
If you want it per player you need to make an array to hold the ids of the players.
Do the following changes I colored them in red:
new bool:InfoMessage[MAX_PLAYERS] = true;

Then change every variable of InfoMessage to InfoMessage[playerid]

but then you don't need to send message to all the players under onplayergivedamage.
Instead you need to create a loop to check who has it on or off.
Like this:
for(new i, j = GetPlayerPoolSize(); i <= j; i++) //This will loop through all the players in the server
{
if(InfoMessage[i] == true) //This will check the state of the info message for each player (i) in if statement to check if it is on (true)
{
//DO the codes here but SendClientMessage to i (the player who has the info message on)
SendClientMessage(i, -1, "Player bla bla gave damage......"); //Send formatted message instead of this
}
}

EDIT: Don't forget to set the variable to false/true when the player disconnect because if player joined with the same id of the previous player and the prev player has it on he will have it on too so just default value when the player connects/disconnects like

Under OnPlayerConnect or OnPlayerDisconnect doesn't matter really.
InfoMessage[playerid] = true; //True will make it show by default. change to false if you want otherwise
I do not understand the "for (new i," Add it in part

FailerZ
11/08/2017, 10:22 AM
I do not understand the "for (new i," Add it in part
The for is the way to initiate a loop -> Syntax: for(initial; condition; increment)
the new i part is to declare an integer varible that holds the number that will get increased/decresed depends on the increment (It will get 0 by default so no need for i = 0 and stuff)
The j is also an integer variable but this one will hold the numbers of all the player slots that you have set in your server,
We then assigned j to GetPlayerPoolSize() which is a native function that return the number of player slots as i explained above so we can go through them all
The second part is the condition here we set which values to go through. in our case it will go to all the player slots that we found in our GetPlayerPoolSize()
And last we decide what is the increment. we want it to increase (it will increase the i variable)after each iteration is done

You might wonder why we used j = GetPlayerPoolSize(); i <= j instead of just putting i <= GetPlayerPoolSize.
Infact it works this way too but the first is more effective because it doesn't get executed for each iterate

for(new i, j = GetPlayerPoolSize(); i < j; i++)
{
if(IsPlayerConnected(i)) //Add this func. I didn't add this part before. My bad this is more effective as it does check if the player is connected before checking whether the infomessage is true
{

}
}

Qbao
11/08/2017, 10:24 AM
The for is the way to initiate a loop -> Syntax: for(initial; condition; increment)
the new i part is to declare an integer varible that holds the number that will get increased/decresed depends on the increment (It will get 0 by default so no need for i = 0 and stuff)
The j is also an integer variable but this one will hold the numbers of all the player slots that you have set in your server,
We then assigned j to GetPlayerPoolSize() which is a native function that return the number of player slots as i explained above so we can go through them all
The second part is the condition here we set which values to go through. in our case it will go to all the player slots that we found in our GetPlayerPoolSize()
And last we decide what is the increment. we want it to increase (it will increase the i variable)after each iteration is done

You might wonder why we used j = GetPlayerPoolSize() instead of just putting i <= GetPlayerPoolSize.
Infact it works this way too but the first is more effective because it doesn't get executed for each iterate

for(new i, k = GetPlayerPoolSize(); i < j; i++)
{
if(IsPlayerConnected(i)) //I didn't add this part before. My bad this is more effective as it does check if the player is connected before checking whether the infomessage is true
{

}
}
Thank you, I did succeed :p:p:p:p:p