Issue with SetPlayerHealth to parameter (y_commands)
#1

Hi, I have been trying to create a /sethp command, where you would type
Код:
/sethp [number] example: /sethp 42
The command system I am using is y_commands, though I don't think that would change much of the result.

I have tried several ways of doing it, probably not the best ways any of them... since none worked :P

Here is my first attempt (I tried with and without the [3] array, since I want max number to be 100 really, I would have to create a check for that, but I thought I should get the actual thing to work first)
pawn Код:
//===[Set Health]===
YCMD:sethp(playerid, hp[], help)
{
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Set the player health to wanted number");
    }
    else
    {
        SetPlayerHealth(playerid, hp[3]);
    }
    return 1;
}
I also tried this (with and without new Float:health)
pawn Код:
//===[Set Health]===
YCMD:sethp(playerid, hp[], help)
{
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Set the player health to wanted number");
    }
    else
    {
        new health;
        health = hp[3];
        SetPlayerHealth(playerid, health);
    }
    return 1;
}
I don't receive any errors in the compile, but when I type /sethp 42 I get the "Unknown command" message, but if I use the built in /commands or /help sethp that comes with y_commands it shows the correct information.

Thanks for any suggestions!

EDIT: Link to fix
Reply
#2

Try using strval to convert hp[3] to an integer.
pawn Код:
new health = strval(hp[3]);
SetPlayerHealth(playerid, health);
Reply
#3

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
Try using strval to convert hp[3] to an integer.
pawn Код:
new health = strval(hp[3]);
SetPlayerHealth(playerid, health);
hp[3] is already an integer :P.

Anyway, just change
pawn Код:
SetPlayerHealth(playerid, hp[3]);
To

pawn Код:
SetPlayerHealth(playerid, floatstr(hp));
It might also be good if you checked the float value first .
Reply
#4

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
Try using strval to convert hp[3] to an integer.
pawn Код:
new health = strval(hp[3]);
SetPlayerHealth(playerid, health);
This seems to instantly crash the server. Nice if I ever need a emergency shut down button, but not for setting the health :/
pawn Код:
//===[Set Health]===
YCMD:sethp(playerid, hp[], help)
{
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Set the player health to wanted number");
    }
    else
    {
        new health = strval(hp[3]);
        SetPlayerHealth(playerid, health);
    }
    return 1;
}
Reply
#5

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
hp[3] is already an integer :P.

Anyway, just change
pawn Код:
SetPlayerHealth(playerid, hp[3]);
To

pawn Код:
SetPlayerHealth(playerid, floatstr(hp));
It might also be good if you checked the float value first .
Yeah, this works like a charm!
Here is the final code if that will help anyone out in the futuuure:
pawn Код:
//===[Set Health]===
YCMD:sethp(playerid, hp[], help)
{
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Set the player health to wanted number");
    }
    else
    {
        SetPlayerHealth(playerid, floatstr(hp)); //anything not a number above 1 seems to kill you, a check above here is probably a good idea!
    }
    return 1;
}
Reply
#6

Quote:
Originally Posted by ******
Посмотреть сообщение
Just for future reference, most people now just a system called "sscanf" for extracting values from strings. In this case you don't need it as there is only one parameter, but as you start writing commands with multiple parameters (the eponymous example being "/givecash <player> <amount>") you might want to start looking in to it.
Just a fast question, do you got any nice tutorial where sscanf is explaned? I don't really know how it works yet :S
Reply
#7

Quote:
Originally Posted by Ranama
Посмотреть сообщение
Just a fast question, do you got any nice tutorial where sscanf is explaned? I don't really know how it works yet :S
check the sscanf thread
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)