Raised This Month: $123 Target: $400
 30% 

Solved Correct way to do this?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
redivcram
Veteran Member
Join Date: Jul 2014
Old 03-10-2018 , 08:13   Correct way to do this?
Reply With Quote #1

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
__________________
I'm a Goldsource level designer. "Private Message" me for contact info.

Last edited by redivcram; 03-20-2018 at 11:04.
redivcram is offline
indraraj striker
Veteran Member
Join Date: Mar 2014
Location: Under the water
Old 03-10-2018 , 08:24   Re: Correct way to do this?
Reply With Quote #2

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]; 

Last edited by indraraj striker; 03-10-2018 at 08:31.
indraraj striker is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-10-2018 , 14:03   Re: Correct way to do this?
Reply With Quote #3

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

Last edited by Bugsy; 03-10-2018 at 14:04.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-10-2018 , 14:29   Re: Correct way to do this?
Reply With Quote #4

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);
__________________

Last edited by fysiks; 03-10-2018 at 14:32.
fysiks is offline
redivcram
Veteran Member
Join Date: Jul 2014
Old 03-20-2018 , 11:04   Re: Correct way to do this?
Reply With Quote #5

@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 View Post
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!
__________________
I'm a Goldsource level designer. "Private Message" me for contact info.

Last edited by redivcram; 03-20-2018 at 11:09.
redivcram is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-20-2018 , 11:15   Re: Correct way to do this?
Reply With Quote #6

Quote:
Originally Posted by redivcram View Post
@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.
__________________

Last edited by HamletEagle; 03-20-2018 at 11:17.
HamletEagle is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-20-2018 , 21:46   Re: Correct way to do this?
Reply With Quote #7

Quote:
Originally Posted by redivcram View Post
@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.
__________________
fysiks is offline
redivcram
Veteran Member
Join Date: Jul 2014
Old 03-23-2018 , 09:14   Re: Correct way to do this?
Reply With Quote #8

Yes, I see. Thanks again.
__________________
I'm a Goldsource level designer. "Private Message" me for contact info.
redivcram is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:56.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode