How do I dertermine the size of an integar.
#1

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
Reply
#2

BUMP - I edited the post so now my question is more clear.
Reply
#3

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

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

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
Reply
#6

I already told you. Use capital 'U' for optional parameters. Optional means, it`s not necessary to use. Try like that.
Reply
#7

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

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
Reply
#9

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)