boolean command - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: boolean command (
/showthread.php?tid=611859)
boolean command -
JXF - 11.07.2016
Код:
CMD:radiusfreeze(playerid, params[])
{
new string[128], bool:toggle, admin[MAX_PLAYER_NAME], radi;
if( Player[playerid][pAdmin] < 1 ) return SCM(pid, ADMIN_COLOR, ADMIN_MESSAGE);
if( radi > 100 && Player[playerid][pAdmin] < 1337 ) return SCM(pid, ADMIN_COLOR, " You cannot use more than 100!");
if(sscanf(params, "f", radi)) return SCM(pid, -1, "{00E6FF}USAGE:{FFFFFF} /radiusfreeze [Range] {FF0000}(( USE AGAIN TO UNFREEZE ))");
GetPlayerName(playerid, admin, sizeof(admin));
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(i == playerid) continue;
if( bool:toggle == false )
{
RadiusDetector(radi, playerid, i);
TogglePlayerControllable(i, 1);
format(string, sizeof(string), "{FF6347}[EVENT]{33CCFF} Admin {FF0000}%s {33CCFF}has unfrozen you", admin);
SCM(i, -1, string);
toggle = false;
}
else
{
RadiusDetector(radi, playerid, i);
TogglePlayerControllable(i, 0);
format(string, sizeof(string), "{FF6347}[EVENT]{33CCFF} Admin {FF0000}%s {33CCFF}has frozen you.", admin);
SCM(i, -1, string);
toggle = true;
}
}
return 1;
}
I want to have this command to toggle the "radiusfreeze" command - if it is entered once it will freeze the player, and if it is entered again it will unfreeze the player. I tried to make this command to make my learning of booleans expand - I know that they are 2 switches - true and false, and it is automatically set to false.
What is wrong with this script? Whenever I type "radiusfreeze" twice it sends the message with "Admin %s has unfrozen you" , and unfreezes the player.
Re: boolean command -
Freaksken - 11.07.2016
You set toggle to false when it is equal to false. Do this instead:
Replace:
With:
or with:
Re: boolean command -
Vince - 11.07.2016
Local variables are discarded when the function ends. Which means every time the command is invoked, a new instance is created that is automatically false. To retain a local variable in memory between subsequent calls use static instead of new.
Re: boolean command -
JXF - 11.07.2016
Very nice useful tips. However, the command still does not toggle.
I think this is because the server doesn't know which one is false and which one is true - when typing "radiusfreeze" it does not toggle to the true part.
Re: boolean command -
Freaksken - 11.07.2016
You should also make the boolean per player:
Код:
//Globals
new bool:toggle[MAX_PLAYERS];
//OnPlayerConnect
toggle[playerid] = false;
//CMD:radiusfreeze
...
for(new otherplayerid = 0, playerCount = GetPlayerPoolSize(); otherplayerid <= playerCount; otherplayerid++) {
if(otherplayerid != playerid) {
if(toggle[otherplayerid]) {
//Unfreeze code
toggle[otherplayerid] = false;
} else {
//Freeze code
toggle[otherplayerid] = true;
}
}
}
...
Re: boolean command -
JXF - 11.07.2016
seems like the trick was in the loop and global bool. it works nicely.
Thank you everyone for your effort, I appreciate it.