AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Correct way to do this? (https://forums.alliedmods.net/showthread.php?t=305937)

redivcram 03-10-2018 08:13

Correct way to do this?
 
You cease amxx scripting for months and already forget a lot...

I've tried different ways that I already forgot and that take up more lines and still don't give the result I want. Here's a code that generates a serial code'ish code.

PHP Code:

new const gSChars[][] = {"AKA""AKB""AKC"};

public 
CmdGenSC(id)
{
    new 
SCode[20];
    
    
format(SCodecharsmax(SCode), 
        
"^x04Generation:^x03 %s%d/%d%/%d/%d"
        
gSChars[random_num(0charsmax(gSChars))], 
        
random_num(09), 
        
random_num(100999), 
        
random_num(1099), 
        
random_num(1000099999)
    );
    
    
ColorChat(idGREY"%s"SCode);
    
    return 
PLUGIN_CONTINUE;


What it should print: "Generation: AKA5/342/23/17063" (numbers are randomly generated as you can tell from the code)

What it prints: "Generation: A"

Don't judge :nono:

indraraj striker 03-10-2018 08:24

Re: Correct way to do this?
 
what BonusCode is doing there ?

ColorChat(id, GREY, "%s", BonusCode);.

it should be

ColorChat(id, GREY, "%s", SCode);

just change this

PHP Code:

new SCode[20]; 

PHP Code:

new SCode[100]; 


Bugsy 03-10-2018 14:03

Re: Correct way to do this?
 
I do not use ColorChat but it appears it has built-in formatting so there is no need for using format(). Also, you should use formatex() when you do need to format a string.

fysiks 03-10-2018 14:29

Re: Correct way to do this?
 
FYI,

While this may work, it is uses improper (confusing) functions to do it:

Code:

gSChars[random_num(0, charsmax(gSChars))]
In this case, you are not doing anything with characters, you are selecting an index of an array (where the result happens to be a string). To make this more proper (i.e. less confusing) you would do this:

Code:

gSChars[random(sizeof gSChars)]
Also, if your format string is ever just "%s" then there is no need to have a format string, simply pass the variable directly to the function:

Code:

ColorChat(id, GREY, SCode);

redivcram 03-20-2018 11:04

Re: Correct way to do this?
 
@indraraj striker

That did the job, thanks!

@Bugsy && Fysiks

At first, I thought charsmax() was the same as sizeof() - 1, that's what everyone said until I found
Quote:

Originally Posted by fysiks (Post 1276130)
You use charsmax() for strings because the last character in a string must be "^0".

sizeof() can be used in many many places. Depends on what you need.

Although, it gave me an index out of bounds error when I used sizeof(). So I suppose using sizeof() - 1 instead of charsmax().

Also I should have made that a global variable, because I need that code to be stored and used somewhere else, my mistake. That's why I used format.

Thanks for the replies!

HamletEagle 03-20-2018 11:15

Re: Correct way to do this?
 
Quote:

Originally Posted by redivcram (Post 2583827)
@indraraj striker

That did the job, thanks!

@Bugsy && Fysiks

At first, I thought charsmax() was the same as sizeof() - 1, that's what everyone said until I found


Although, it gave me an index out of bounds error when I used sizeof(). So I suppose using sizeof() - 1 instead of charsmax().

Also I should have made that a global variable, because I need that code to be stored and used somewhere else, my mistake. That's why I used format.

Thanks for the replies!

charsmax IS sizeof -1. The charsmax macro was added for readability purpose.

fysiks 03-20-2018 21:46

Re: Correct way to do this?
 
Quote:

Originally Posted by redivcram (Post 2583827)
@Bugsy && Fysiks

At first, I thought charsmax() was the same as sizeof() - 1, that's what everyone said until I found


Although, it gave me an index out of bounds error when I used sizeof(). So I suppose using sizeof() - 1 instead of charsmax().

The point is that the first dimension of gSChars is used as an array, not a string. The second dimension is used as strings. So, the two proper uses would be like this:

Code:

new size_of_array = sizeof gSChars // Gets the size of the first dimension
new max_characters_in_string = charsmax(gSChars[]) // gets the maximum characters of the strings in the array

As I said in my post, your code technically worked because charsmax is the same as sizeof -1 but it's confusing code. Making your code less confusing now will help you when you forget about the plugin and you come back to it later (and for when you ask for help). I have to work with poor coding all the time at work and things like this makes life more frustrating.

redivcram 03-23-2018 09:14

Re: Correct way to do this?
 
Yes, I see. Thanks again.


All times are GMT -4. The time now is 22:45.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.