Raised This Month: $51 Target: $400
 12% 

Useful Functions that Nobody Uses


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-23-2007 , 13:19   Useful Functions that Nobody Uses
Reply With Quote #1

We all know that AMXX has a huge set of functions for almost everything we need.

But a lot of scripters, mostly beginners,don't know a lot of useful functions that can make the code simpler,efficient and more readable.


sizeof var
charsmax var (AMXX 1.8 ONLY)

Not functions, but builtin keywords. Return the size of a variable in cells.
Each array element is a cell. Specially useful to not hardcode string lengths.

Code:
new string[32] sizeof string == 32 // safe string lenght: 31 | sizeof string - 1 | charsmax string

access(id,flag)

Checks if user has a admin flag

Code:
access(id,ADMIN_IMMUNITY)

clamp(num,min,max)

Clamps a number between other two.If it's higher than max,set it to max.Same with min.

Code:
new var = 5 var = clamp(var,0,3) // var > 3 so var is set to 3 var = clamp(var,4,10) // var< 4 so var is set to 4

max(num,max)
min(num,min)

If 'num' is higher/lower then max/min, set it to max/min

Code:
new var=5 var = max(var,4) // var still 5, because 5 > 4 var = min(var,4) // var is now 4,because 5 < 4 var = max(var,0) // make sure value is not negative var = min(var,255) // make sure value is not higher than 255,for write_byte,for example

arrayset(array[],value,size)

Sets an array to 'value' up to element 'size'-1

Code:
new array[3] // all vars are initialized to 0 arrayset(array,-1,2) // sets values to '-1' until element '1' array[0] == -1 array[1] == -1 array[2] == 0 // only set until element 1(2-1)

get_cvar_pointer(cvar[])

Returns a pointer to a cvar by name.Useful when you check for non-AMXX cvars constantly

Code:
new pointer = get_cvar_pointer("sv_alltalk") // pointer now is like any pcvar get_pcvar_num(pointer)

is_user_admin ( id )

Checks if a user has admin rights

Code:
is_user_admin(id)

get_weaponid(name[])
get_weaponname(id,name[],len)

Converts a weapon id to a weapon name or the inverse.

Code:
get_weaponid("weapon_scout") == CSW_SCOUT new name[32] get_weaponname(CSW_SCOUT,name,sizeof name-1) // name == "weapon_scout"
danielkza is offline
P34nut
AMX Mod X Beta Tester
Join Date: Feb 2006
Location: Netherlands
Old 08-23-2007 , 16:53   Re: Useful Functions that Nobody Uses
Reply With Quote #2

www.amxmodx.org/funcwiki.php
There is a lot of information there
__________________
All you need to change the world is one good lie and a river of blood
P34nut is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-23-2007 , 17:41   Re: Useful Functions that Nobody Uses
Reply With Quote #3

Yep,but nobody has time to look at it all,specially beginners,and where did you think I took them?
danielkza is offline
Old 08-24-2007, 20:43
Hawk552
This message has been deleted by BAILOPAN. Reason: abuse
Old 08-25-2007, 08:36
danielkza
This message has been deleted by BAILOPAN. Reason: removed flaming
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-25-2007 , 12:15   Re: Useful Functions that Nobody Uses
Reply With Quote #4

Most of these are stocks and therefore only provide another level of abstraction. Nobody can say with any degree of authority whether or not it is better to use them, because while they keep code concise, they inevitably hide what it is they're doing. Unless you read the include files that is - but those who do that would already know about these very basic functions.

Since a few people tend to hear about what's optimal in some cases and then use it at every possible opportunity, it's worth noting that there's no point in using get_cvar_pointer() if you're going to get or set the value only once, because [s|g]et_cvar_*() would have to do the same work anyway.

Code:
get_weaponname(CSW_SCOUT,name,sizeof name-1)
You don't think you should use charsmax?

Another stock you might think is worthwhile is show_activity().

Last edited by Lee; 08-25-2007 at 12:26.
Lee is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-25-2007 , 14:46   Re: Useful Functions that Nobody Uses
Reply With Quote #5

I do not recommend using charsmax because it's only in 1.8,so it's better to keep compatiblility with previous versions.

Quote:
Originally Posted by danielkza
check for non-AMXX cvars constantly
danielkza is offline
potatis_invalido
BANNED
Join Date: Jul 2007
Location: 0 (world)
Old 08-26-2007 , 09:38   Re: Useful Functions that Nobody Uses
Reply With Quote #6

potatis_invalido is offline
Old 08-28-2007, 09:05
Rolnaaba
This message has been deleted by BAILOPAN. Reason: removed flaming
Old 08-28-2007, 11:51
Hawk552
This message has been deleted by BAILOPAN. Reason: abuse
Old 08-28-2007, 16:22
Rolnaaba
This message has been deleted by BAILOPAN. Reason: removed flaming
Old 09-03-2007, 14:11
Hawk552
This message has been deleted by BAILOPAN. Reason: removed flaming
eXist
Member
Join Date: Apr 2005
Old 09-06-2007 , 21:39   Re: Useful Functions that Nobody Uses
Reply With Quote #7

Quote:
Originally Posted by danielkza View Post

is_user_admin ( id )

Checks if a user has admin rights

Code:
is_user_admin(id)
Does this command check for anyflag and call that person an admin?

For example, if someone has the flag "s" will that function return true?
eXist is offline
pRED*
Join Date: Dec 2006
Old 09-07-2007 , 21:45   Re: Useful Functions that Nobody Uses
Reply With Quote #8

is_user_admin(id) will return true if the user has any admin flags.

You need to use the other admin functions to check for a specific access level
pRED* is offline
Reply



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 21:01.


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