boolean command
#1

Код:
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.
Reply
#2

You set toggle to false when it is equal to false. Do this instead:
Replace:
Код:
if(toggle == false)
With:
Код:
if(toggle == true)
or with:
Код:
if(toggle)
Reply
#3

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.
Reply
#4

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.
Reply
#5

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;
        }
    }
}
...
Reply
#6

seems like the trick was in the loop and global bool. it works nicely.
Thank you everyone for your effort, I appreciate it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)