Optional Parameter -
mrcoolballs - 26.03.2011
EDIT: No one seemed to answer my last question so I'm going to attempt to explain it better.
Is there any possible way to check if the ID part of a command is not there?
For example a /sethealth <Name> <Health> command would set the health of the name given to the health specified, but if I don't add a name in there I want it to set the health of me to the health i type
pawn Код:
dcmd_sethealth(playerid,params[])
{
new id, health;
if(sscanf(params,"ii",id,health)) return 0;
if(!strlen(id))
{
SetPlayerHealth(playerid,health);// The <Name> part of the command has not been entered, so by default it will choose the player who typed the command
}
else
{
SetPlayerHealth(id,health);// The <Name> part of the command has been entered so the health of the ID entered will be set
}
return 1;
}
But obviously this wont work because "id", is not a string, if anyone can understand what I'm trying to make here, could you please give me a hand
Re: How do I dertermine the size of an integar. -
mrcoolballs - 26.03.2011
BUMP - I edited the post so now my question is more clear.
Re: How do I dertermine the size of an integar. -
antonio112 - 26.03.2011
Try using '
I' instead of '
i', if I`m not wrong, capital letter means optional parameter.
ps: By the way, you shouldn`t use '
i' for playerid's. Use '
u' or '
U' in this case.
Re: How do I dertermine the size of an integar. -
Mike Garber - 26.03.2011
Use sscanf;
https://sampforum.blast.hk/showthread.php?tid=120356
Sorry i see you're doing that xD
Use
pawn Код:
if(sscanf(params, "u",id)) id = playerid;
Re: How do I dertermine the size of an integar. -
mrcoolballs - 26.03.2011
Oh yes, I usually do use "u", but I wrote that command so fast I didn't realise my mistake.
I changed it to this:
pawn Код:
dcmd_sethealth(playerid,params[])
{
new id, health;
sscanf(params,"ui",id,health);
if(sscanf(params,"u",id))
{
SetPlayerHealth(playerid,health);
}
else
{
SetPlayerHealth(id,health);
}
return 1;
}
When I type /sethealth 50, nothing happens but if I type /sethealth 0 50 (0 being my ID), my health gets set to 50
Re: How do I dertermine the size of an integar. -
antonio112 - 26.03.2011
I already told you. Use capital '
U' for optional parameters. Optional means, it`s not necessary to use. Try like that.
Re: How do I dertermine the size of an integar. -
mrcoolballs - 26.03.2011
I tried that, but how would I specifiy what happens if "U", is not there?
pawn Код:
dcmd_sethealth(playerid,params[])
{
new id, health;
sscanf(params,"Ui",id,health);
SetPlayerHealth(id,health);
return 1;
}
/sethealth 0 50 works, but /sethealth 50 does not.
Re: How do I dertermine the size of an integar. -
antonio112 - 26.03.2011
Ahh ... I have no idea but I`ll go in ******'s topic and try to find it out. I`ll come back with an edit when I find anything. :P
Re: How do I dertermine the size of an integar. -
Calgon - 26.03.2011
You could always perform a check to see if the ID is valid or invalid, then as a fail-safe, set the health to 'playerid':
pawn Код:
dcmd_sethealth(playerid,params[])
{
new id, Float: health;
if(sscanf(params,"Uf",id,health)) {
// Also, health should be a float.
if(id != INVALID_PLAYER_ID) return SetPlayerHealth(id, health);
return SetPlayerHealth(playerid, health);
}
return 1;
}
Untested. I've never needed to use the optional 'U' parameter, so I'm unsure of it's behaviour.
Re: How do I dertermine the size of an integar. -
TeamPublic - 26.03.2011
pawn Код:
dcmd_sethealth(playerid,params[])
{
new id, Float: health;
if(sscanf(params,"df",id,health)) return SendClientMessage(playerid, 0xFFFFFFFF, "Correct usage: /sethealth [id] [health]");
if(!IsPlayerConnected(id)) return SendClientMessage(playerid, 0xFFFFFFFF, "Player id isn't used");
return SetPlayerHealth(playerid, health);
}
Try that it should work. If not or you need something else you can PM me.