AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Some simple solutions (https://forums.alliedmods.net/showthread.php?t=295044)

CreativeTR 03-14-2017 16:04

Some simple solutions
 
They are all described with an example.

Correct Definition

PHP Code:

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 

Correct Numerical Data Types

not only
PHP Code:

set_user_gravity(id0.8//this may be error
set_user_gravity(idFloat:0.8//the best way to be sure 

but also
PHP Code:

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) 

Clean Cvar Usage

PHP Code:

p_Money[id] = get_cvar_num("max_money"//this may be error 

correct way
PHP Code:

#define MAX_PLAYERS 33
new cvar_max_money
new p_Money[MAX_PLAYERS]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
cvar_max_money get_cvar_num("max_money")
}
public 
client_connect(id) {
    
p_Money[id] = cvar_max_money


Clean Algorithm

1:

PHP Code:

new p_Name[33]
get_user_name(idp_Name33

to
PHP Code:

#define MAX_PLAYERS 33
new p_Name[MAX_PLAYERS]
get_user_name(idp_Namecharsmax(p_Name)) 

2:

PHP Code:

set_user_health(idget_user_health(id) + 50)
set_user_armor(idget_user_armor(id) + 50)
p_UsrXP[id] = p_UserXP[id] + 50 

to
PHP Code:

new i_Val 50
set_user_health
(idget_user_health(id) + i_Val)
set_user_armor(idget_user_armor(id) + i_Val)
p_UsrXP[id] += i_Val 

That's just me.

You can add what you know.

HamletEagle 03-14-2017 16:49

Re: Some simple solutions
 
Code:

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.

Code:

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.

Code:

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)

First line is wrong, p_Gravityshould be tagged as Float(i.e. new Float:pGravity[MAX_PLAYERS). Also p usually stands from "pointers", so you should use f if you really want to use HN.
Second line is also wrong, float it's a custom tag, the default one is called Float. Again, variable should be tagged as Float, not the value(because the value it's already a float).

Correct way:
Code:

new Float:p_Gravity[MAX_PLAYERS]
I would also like to add something about this kind of initialization:
PHP Code:

new var[33] = 

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:
PHP Code:

new var[33] = {1, ...} 

Code:

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

Code:

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

Code:

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.

Code:

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 

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.

edon1337 03-15-2017 08:02

Re: Some simple solutions
 
Quote:

Originally Posted by CreativeTR (Post 2503648)
PHP Code:

set_user_gravity(id0.8//this may be error
set_user_gravity(idFloat:0.8//the best way to be sure 

but also
PHP Code:

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) 


Makes no sense just like Hamlet said.

Quote:

Originally Posted by CreativeTR (Post 2503648)
PHP Code:

set_user_health(idget_user_health(id) + 50)
set_user_armor(idget_user_armor(id) + 50)
p_UsrXP[id] = p_UserXP[id] + 50 

to
PHP Code:

new i_Val 50
set_user_health
(idget_user_health(id) + i_Val)
set_user_armor(idget_user_armor(id) + i_Val)
p_UsrXP[id] += i_Val 


That's bad practice, creating a variable costs you.

Quote:

Originally Posted by CreativeTR (Post 2503648)
PHP Code:

#define MAX_PLAYERS 33
new cvar_max_money
new p_Money[MAX_PLAYERS


Max Players is 32, you would do
Code:
#define MAX_PLAYERS 32 new cvar_max_money new p_Money[MAX_PLAYERS + 1]

CreativeTR 03-15-2017 11:35

Re: Some simple solutions
 
Quote:

Originally Posted by HamletEagle (Post 2503660)
[code]
Don't get me wrong, I'm not picking on you, I'm trying to explain some stuff that seems to confuse you.

Code:

It won't throw any warning. cs_get_user_team is already tagged as CsTeams. You should take a look at how tags work.
In some cases due to differences in definition tags, becomes an unreadable command array (It's always like this)

Code:

Same thing, tagging a float as a Float makes no sense.
of course, it makes sense for pawn just like c++
if you don't pay attention to the case, it will cause "Tag mismatch warming". You can try this

PHP Code:

MessageBox::Show("message","title, ..." 

diffrence (it will cause fatal fault)
PHP Code:

messageBox::Show("message","title, ..." 


--


Code:

First line is wrong, p_Gravityshould be tagged as Float(i.e. new Float:pGravity[MAX_PLAYERS). Also p usually stands from "pointers", so you should use f if you really want to use HN.
Second line is also wrong, float it's a custom tag, the default one is called Float. Again, variable should be tagged as Float, not the value(because the value it's already a float).

You're right about that. This is my fault

Code:

I would also like to add something about this kind of initialization:
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:
new var[33] = {1, ...}

You can try this will not make a difference.

HTML Code:

Won't throw any error if the cvar is present. Anyway, don't use cvars, use pcvars, they are faster([tt]get_pcvar_num[/tt]).
if plugin send this command to client all time, it will cause SZ_getSpace overflow without FSB_ALLOW OVERFLOW

Code:

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.
The fact that this change in client isn't healthy will lead to mistakes.
For this reason, these are done with "cfg" before the game.

Code:

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.
Amxmodx (1.8.3-dev) after updating "MAX_PLAYERS 32 and [MAX_PLAYERS+1]" is changed.
if you do this, you will encounter a lot of errors

new functuality from AMXX amxconst.inc

MAX_PLAYERS 33 and and without + 1 ([MAX_PLAYERS])

Code:

While you could argue that's for readability it does not provide any performance optimization.
That's right.

I made the necessary explanations for you, thank you for your comment.

klippy 03-15-2017 12:01

Re: Some simple solutions
 
Quote:

Originally Posted by CreativeTR (Post 2503836)
In some cases due to differences in definition tags, becomes an unreadable command array (It's always like this)

What?
Both the return value of cs_get_user_team() and CS_TEAM_T constant are tagged with CsTeams:, a tag override is redundant.


Quote:

of course, it makes sense for pawn just like c++
if you don't pay attention to the case, it will cause "Tag mismatch warming". You can try this

PHP Code:

MessageBox::Show("message","title, ..." 

diffrence (it will cause fatal fault)
PHP Code:

messageBox::Show("message","title, ..." 


Floating point values are implicitly tagged with Float: in Pawn, there's no need to override the tag. Again, redundant.

Quote:

if plugin send this command to client all time, it will cause SZ_getSpace overflow without FSB_ALLOW OVERFLOW

The fact that this change in client isn't healthy will lead to mistakes.
For this reason, these are done with "cfg" before the game.
What command? Getting a cvar value is never sent to clients and you can do it as many times as you want in a single frame. As Hamlet said, use pcvars. In 1.8.3, use cvar binding.

Quote:

Amxmodx (1.8.3-dev) after updating "MAX_PLAYERS 32 and [MAX_PLAYERS+1]" is changed.
if you do this, you will encounter a lot of errors

new functuality from AMXX amxconst.inc

MAX_PLAYERS 33 and and without + 1 ([MAX_PLAYERS])
In 1.8.3 there's
Code:

/**
 * The maximum buffer size required to store a clients name.
 */
#define MAX_NAME_LENGTH 32

use that size for clients' names.

CreativeTR 03-15-2017 12:07

Re: Some simple solutions
 
Quote:

Originally Posted by KliPPy (Post 2503839)
In 1.8.3 there's
Code:

/**
 * The maximum buffer size required to store a clients name.
 */
#define MAX_NAME_LENGTH 32

use that size for clients' names.

I'm not talking about client name. Client ID I'm talking about

HamletEagle 03-15-2017 12:16

Re: Some simple solutions
 
Here you are not coding in c++, keep that in mind. Most of what you say is wrong.

I'm going to answer now only on what Klippy did not.
Quote:

You can try this will not make a difference.
What about you trying it?

PHP Code:

#include <amxmodx>

new Array1[5] = {12, ...}
new 
Array2[5] = 12

public plugin_init()
{
    new 
i;
    for(
0sizeof Array1i++)
    {
        
server_print("Array1[%i] = %i | Array2[%i] = %i"iArray1[i], iArray2[i])
    }


Output:
PHP Code:


Array1
[0] = 12 Array2[0] = 12
Array1
[1] = 12 Array2[1] = 0
Array1
[2] = 12 Array2[2] = 0
Array1
[3] = 12 Array2[3] = 0
Array1
[4] = 12 Array2[4] = 

Quote:

The fact that this change in client isn't healthy will lead to mistakes.
For this reason, these are done with "cfg" before the game.
Lol, that's the entire purpose of a cvar. The cvar is to let server admins to change plugin behaviour on the fly, without needing to recompile or change/restart map.
If something is prone to error and could produce a crash or something just don't expose that value by cvars or enforce proper filtering on it to prevent bad values.

Quote:


MAX_PLAYERS 33 and and without + 1 ([MAX_PLAYERS])
Indexing a string array that should store a player name by a constant used to size arrays for players is confusing. Use 32, make your own define if you are using amxx < 182 or use the proper constant as Klippy said.

Edit:
Quote:

I'm not talking about client name. Client ID I'm talking about
Klippy and I are talking about this:
PHP Code:

#define MAX_PLAYERS 33 
new p_Name[MAX_PLAYERS
get_user_name(idp_Namecharsmax(p_Name)) 

Here you are not using p_Name as an array to store data for each player, it's going to save a player name. Let's say the player name is "ABCDEFGHIJ", then:
PHP Code:

p_Name[0] = 'A'
p_Name[1] = 'B'
p_Name[2] = 'C'
p_Name[3] = 'D'
..and so on 


Emp` 03-15-2017 12:28

Re: Some simple solutions
 
Quote:

Originally Posted by HamletEagle (Post 2503660)
Don't get me wrong, I'm not picking on you, I'm trying to explain some stuff that seems to confuse you.

Seems to be lacking some key knowledge on tags, arrays, and globals.

I'm going to close this thread as it will probably lead to more confusion/arguing. For understanding the issues, see other posts or more info below:
  1. CS_TEAM_* constants are the CsTeams tag, no need to retag.
  2. Retagging floats is unnecessary.
  3. Untagged [int] array created, but forcing float data. Better example:
    PHP Code:

    new Float:gfPlayerGravityMAX_PLAYERS ];  //global float array to cache the last set gravity value by this plugin; for active gravity [from any plugin] use get_user_gravity instead of this array

    SetPlayerGravityiPlayerFloat:fGravity )
    {
        
    gfPlayerGravityiPlayer ] = fGravity//update our cached value for iPlayer
        
    set_user_gravityiPlayerfGravity ); //set gravity with fun module


  4. Pcvar example:
    PHP Code:

    new pcvar_max_money;
    new 
    giMaxMoney;
    new 
    giPlayerMoneyMAX_PLAYERS ];

    public 
    plugin_init()
    {
        
    pcvar_max_money get_cvar_pointer"max_money" );
        
    //get pcvar value
        //giMaxMoney = get_pcvar_num( pcvar_max_money ); // this retrieves the cvar at time of plugin_init
        
    bind_pcvar_numpcvar_max_moneygiMaxMoney ); // binds the cvar to the global variable, so we do not need to retrieve each time
    }
    public 
    client_authorizediPlayer )
    {
        
    giPlayerMoneyiPlayer ] = giMaxMoney//update our cached value for iPlayer
        
    cs_set_user_moneyiPlayergiMaxMoney, .flash 0); //set money with cstrike module


  5. MAX_PLAYERS is being used as a string length instead of an array size. Local string example:

    PHP Code:

    funcRandomiPlayer )
    {
        new 
    strPlayerNameMAX_NAME_LENGTH ];  //creating a string 32 cells long
        
    get_user_nameiPlayerstrPlayerNamecharsmaxstrPlayerName ) );
        
    //...


    Global string example:
    PHP Code:

    new gstrPlayerNameMAX_PLAYERS ][ MAX_NAME_LENGTH ]; //creating a global string array to cache player names

    funcRandomiPlayer )
    {
        
    get_user_nameiPlayergstrPlayerNameiPlayer ], charsmaxgstrPlayerName[] ) );
        
    //...




All times are GMT -4. The time now is 13:29.

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