AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   get_user_name question. (https://forums.alliedmods.net/showthread.php?t=170848)

Evaldas.Grigas 10-29-2011 14:33

get_user_name question.
 
I searched funcwiki, but I still doesn't knwo what len means in this code:
PHP Code:

get_user_name indexname[], len 

FuncWiki

Napoleon_be 10-29-2011 14:44

Re: get_user_name question.
 
PHP Code:

new szName[33]
get_user_name(idszNamecharsmax(szName)) 

or

PHP Code:

new szName[33]
get_user_name(id szName32


drekes 10-29-2011 15:02

Re: get_user_name question.
 
Len stands for the max length of the string.
The max length is the size of your array - 1 (for the NULL terminator);

Bugsy 10-29-2011 15:09

Re: get_user_name question.
 
Always use charsmax() and not a hard-coded number. You almost always need to set a 'max-characters' value to a function putting info into a string. charsmax( StringVariable ) retrieves the correct value.

Flipper_SPb 10-29-2011 15:13

Re: get_user_name question.
 
Quote:

Originally Posted by Bugsy (Post 1585893)
Always use charsmax() and not a hard-coded number.

Why I need to use charsmax? I know, I'll never change the maxlength of the username. And 32 is enough.

fysiks 10-29-2011 15:17

Re: get_user_name question.
 
Quote:

Originally Posted by Flipper_SPb (Post 1585897)
Why I need to use charsmax? I know, I'll never change the maxlength of the username. And 32 is enough.

Because if you get in the [good] habit of using it then you will less likely make mistakes with other strings when changing the array length. That is my reason maybe others have their own reasons.

Evaldas.Grigas 10-29-2011 15:20

Re: get_user_name question.
 
Why can't I use it like this?
PHP Code:

if(get_user_name idEvaldas.Grigas )) 

I want to check if player name is Evaldas.Grigas.

Flipper_SPb 10-29-2011 15:21

Re: get_user_name question.
 
How fast is charsmax()? Is it good to use it in ClientPreThink instead of hard-coded values?

Flipper_SPb 10-29-2011 15:23

Re: get_user_name question.
 
Quote:

Originally Posted by Evaldas.Grigas (Post 1585905)
Why can't I use it like this?
PHP Code:

if(get_user_name idEvaldas.Grigas )) 

.

Because it totally wrong.

PHP Code:

new szName[33]
get_user_name(idszNamecharsmax(szName))  

if(
equal(szName"Evaldas.Grigas"))
{

   
//your code




Bugsy 10-29-2011 15:24

Re: get_user_name question.
 
There's no reason not to plus its a good habit that can prevent errors and provide easier debugging on future revisions. I am now rewriting a plugin I wrote years ago and it took me a while to realize the cause of one of my errors where I added items to an array and in my for-loop I had the size hard-coded, had I used sizeof() my code would have adjusted itself for this change.

Quote:

Originally Posted by Flipper_SPb (Post 1585909)
How fast is charsmax()? Is it good to use it in ClientPreThink instead of hard-coded values?

It is fine to use anywhere in code. It is a macro so when your code is compiled the compiler replaces each occurrence of sizeof/charsmax with the numerical value. You are thinking of a native function which calls a dll which are good to avoid using when possible.


All times are GMT -4. The time now is 14:15.

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