AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] Code Styling & Good Programming Habits (https://forums.alliedmods.net/showthread.php?t=85274)

Liverwiz 07-28-2012 16:43

Re: [TUT] Code Styling & Good Programming Habits
 
Because the post didn't mention it, and i didn't read all 9 pages of comments I shall say something (very short) on macros

Macros rock. Basically a really quick way of writing a static (as in nothing in it will change) one-line function. To make calculations easier to read, easier to put into your code, and even grab parts of code required to be used many times.

If you are using a macro with more than one parameter you cannot separate them with commas (my main point) And each parameter will be prefixed with a % followed by an identifier. I'm not sure if this convention can change....but it seems to be accepted, so i'm going with it.
For instance....you have three arrays. One holds indexes to each of the two other arrays. You can combine these two array indexes using a macro with some simple math in it. Then two other macros to retrieve them. See my code....

PHP Code:

new g_PlayersData[33]
new 
g_PlayerType[15]
new 
g_PlayerSize[154]

#define PRECISION 10000        // this makes sure the numbers don't run together
#define SET_DATA(%1 %2)        %1 * PRECISION + %2
#define GET_INDEX1(%1)        %1 / PRECISION 
#define GET_INDEX2(%1)        %1 % PRECISION 


    // type and size are indexes to the playerType/Size arrays
public doPlayerThing(idtypesize)
    
g_PlayersData[id] = SET_DATA(type size)
    
public 
getPlayerData(id, &type, &size)
{
    new 
data g_PlayersData[id]
    
type GET_INDEX1(data)
    
size GET_INDEX2(data)
    
    return 
data


As you can see to combine the two numbers i used a macro to multiply the first index by a large number (i used 10000 for the easy math) any number greater than that of the second will do, really, then add the second. Then to retrieve each number i used another macro for each. To get the first you do some simple integer division (which will leave off the remainder, returning all whole numbers in the division) this gives you that first index. Then for the second i used modulus (%) to get the remainder of the division. This gives you that second index you added on. All combined successfully give you a sleek, efficient way of storing two numbers into one cell.

Hope i helped.
Good day.

ConnorMcLeod 07-28-2012 16:53

Re: [TUT] Code Styling & Good Programming Habits
 
Macros with commas work well, you can't put spaces in them that's all.

See :

PHP Code:

#define m_iMenu 205
#define cs_get_user_menu(%0)     get_pdata_int(%0, m_iMenu)
#define cs_set_user_menu(%0,%1)  set_pdata_int(%0, m_iMenu, %1) 


Liverwiz 07-28-2012 17:04

Re: [TUT] Code Styling & Good Programming Habits
 
Quote:

Originally Posted by ConnorMcLeod (Post 1760055)
Macros with commas work well, you can't put spaces in them that's all.

See :

PHP Code:

#define m_iMenu 205
#define cs_get_user_menu(%0)     get_pdata_int(%0, m_iMenu)
#define cs_set_user_menu(%0,%1)  set_pdata_int(%0, m_iMenu, %1) 


OH! It wasn't working for me when i did SET_DATA(%1, %2) but worked without the comma. whatever reason....my code compiles and works properly. Though i, personally, prefer using the commas. So i think i'ma switch back to that in my code.

Also note: you need to keep the same delimiter throughout your code.
If you were to choose SET_DATA(%1,%2) you would not be able to call it with SET_DATA(%1 %2)
and same goes the other way around.

ConnorMcLeod 07-28-2012 18:35

Re: [TUT] Code Styling & Good Programming Habits
 
I suggest you to read the Pawn Language Guide on page 92 "The preprocessor"

B l e n d 11-08-2012 09:25

Re: [TUT] Code Styling & Good Programming Habits
 
Hey, why write "0"?

PunishUser( id )
{
#if defined KILL_USER
user_kill( id )
client_print( 0, print_chat, "[Kill] The user has been killed")
#else // KILL_USER
user_slap( id, 0 )
client_print( 0, print_chat, "[Kill] The user has been slapped")
#endif // KILL_USER
}

hleV 11-08-2012 13:26

Re: [TUT] Code Styling & Good Programming Habits
 
When instead of player ID you use 0, the function/native (as long as it's designed that way) is applied to all players.

Neeeeeeeeeel.- 11-08-2012 14:08

Re: [TUT] Code Styling & Good Programming Habits
 
Quote:

Originally Posted by B l e n d (Post 1834593)
Hey, why write "0"?

PunishUser( id )
{
#if defined KILL_USER
user_kill( id )
client_print( 0, print_chat, "[Kill] The user has been killed")
#else // KILL_USER
user_slap( id, 0 )
client_print( 0, print_chat, "[Kill] The user has been slapped")
#endif // KILL_USER
}

0 is server index, so it is a global message.

xOscar 11-21-2012 06:41

Re: [TUT] Code Styling & Good Programming Habits
 
Awesome tutorial Hawk.
I also want to see updates!

Tippler 10-06-2022 12:41

Re: [TUT] Code Styling & Good Programming Habits
 
Good old days :)


All times are GMT -4. The time now is 12:01.

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