Some simple solutions

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Some simple solutions

Post by Archive-Poster »

They are all described with an example.

Correct Definition

Code: Select all

if(cs_get_user_team(id) == CS_TEAM_T) //this may be error
if(cs_get_user_team(id) == CsTeams:CS_TEAM_T) //the best way to be sure 
It won't throw any warning. cs_get_user_team is already tagged as CsTeams. You should take a look at how tags work.
Correct Numerical Data Types

not only

Code: Select all

set_user_gravity(id, 0.8) //this may be error
set_user_gravity(id, Float:0.8) //the best way to be sure 
Same thing, tagging a float as a Float makes no sense.
but also

Code: Select all

new p_Gravity[MAX_PLAYERS] = 0.8 //this may be error
new p_Gravity[MAX_PLAYERS] = float:0.8 //the best way to be sure ("float" for defination, "Float" for function - upper case) 
I would also like to add something about this kind of initialization:

Code: Select all

new var[33] = 1 
This won't set all array cells to 1, only the first one(var[0] is 1, but var[1],var[2], ... are 0). If you want to initialize the entire array with 1, you should do:

Code: Select all

new var[33] = {1, ...}
Clean Cvar Usage

Code: Select all

p_Money[id] = get_cvar_num("max_money") //this may be error 
Won't throw any error if the cvar is present. Anyway, don't use cvars, use pcvars, they are faster(get_pcvar_num).
correct way

Code: Select all

#define MAX_PLAYERS 33
new cvar_max_money
new p_Money[MAX_PLAYERS]

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
    cvar_max_money = get_cvar_num("max_money")
}
public client_connect(id) {
    p_Money[id] = cvar_max_money
} 
What you did here is basically to cache the cvar value, so if it's changed in-game it won't take affect until the next map change.
Clean Algorithm

1:

Code: Select all

new p_Name[33]
get_user_name(id, p_Name, 33) 
p_Name should be 32, not 33 and in get_user_name you should put size - 1, so 31. Indexing pName by MAX_PLAYERS is confusing. You would do that when you want to have an array for players, but here you are using a char array to store the name of ONLY ONE player at a time. So just put 32.
The usage of charsmax() is correct.
to

Code: Select all

#define MAX_PLAYERS 33
new p_Name[MAX_PLAYERS]
get_user_name(id, p_Name, charsmax(p_Name)) 
2:

Code: Select all

set_user_health(id, get_user_health(id) + 50)
set_user_armor(id, get_user_armor(id) + 50)
p_UsrXP[id] = p_UserXP[id] + 50 
to

Code: Select all

new i_Val = 50
set_user_health(id, get_user_health(id) + i_Val)
set_user_armor(id, get_user_armor(id) + i_Val)
p_UsrXP[id] += i_Val 
While you could argue that's for readability it does not provide any performance optimization.

Don't get me wrong, I'm not picking on you, I'm trying to explain some stuff that seems to confuse you.

Who is online

Users browsing this forum: No registered users and 0 guests