View Single Post
headline
SourceMod Moderator
Join Date: Mar 2015
Old 02-25-2018 , 04:56   Re: Snippet: Setting RGB/RGBA values via a command
Reply With Quote #4

Quote:
Originally Posted by ddhoward View Post
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 clientint args) {

    if (
client == || !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 isizeof(chRGBA) && argsi++) {
        
GetCmdArg(i+1chRGBA[i], sizeof(chRGBA[i]));
        
iRGBA[i] = StringToInt(chRGBA[i]);
        if (!(
<= 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.
headline is offline