Quote:
Originally Posted by ddhoward
Even this is a little excessive; it's not necessary to hold the user's hand for the range validation, just as it's not necessary to use regex to check for non-numeric characters. Even if they omit an argument, I'd just go with it.
PHP Code:
public Action Command_EnterRGBA(int client, int args) {
if (client == 0 || !IsClientInGame(client)) {
ReplyToCommand(client, "[SM] You must be in-game to use this command!");
return Plugin_Handled;
}
char chRGBA[4][4];
int iRGBA[4];
for (int i; i < sizeof(chRGBA) && i < args; i++) {
GetCmdArg(i+1, chRGBA[i], sizeof(chRGBA[i]));
iRGBA[i] = StringToInt(chRGBA[i]);
if (!(0 <= iRGBA[i] <= 255)) {
iRGBA[i] = 0;
ReplyToCommand(client, "RGBA values must be between 0 and 255.");
}
}
// use RGBA how you want
return Plugin_Handled;
}
|
Every code post turns into a pissing contest. It just depends on if you want to coddle the user, and how simple you’d want the code to be. While it could be done with iteration as you’ve shown, it’s a bit much for someone who doesn’t yet grasp the usage of regex. The approach I posted is nearly equivalent to yours, but I tend to avoid assuming user inputs if they’re invalid. Garbage data in -> garbage data out.