Raised This Month: $7 Target: $400
 1% 

[TUT] Code Styling & Good Programming Habits


Post New Thread Reply   
 
Thread Tools Display Modes
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-28-2012 , 15:43   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #81

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.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-28-2012 at 15:50.
Liverwiz is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-28-2012 , 15:53   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #82

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) 
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 07-28-2012 at 15:53.
ConnorMcLeod is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-28-2012 , 16:04   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #83

Quote:
Originally Posted by ConnorMcLeod View Post
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.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-28-2012 at 16:08.
Liverwiz is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-28-2012 , 17:35   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #84

I suggest you to read the Pawn Language Guide on page 92 "The preprocessor"
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
B l e n d
BANNED
Join Date: Oct 2012
Location: Somewhere on the planet
Old 11-08-2012 , 08:25   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #85

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
}
B l e n d is offline
Send a message via Skype™ to B l e n d
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 11-08-2012 , 12:26   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #86

When instead of player ID you use 0, the function/native (as long as it's designed that way) is applied to all players.
hleV is offline
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 11-08-2012 , 13:08   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #87

Quote:
Originally Posted by B l e n d View Post
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.
__________________
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
xOscar
BANNED
Join Date: Nov 2012
Old 11-21-2012 , 05:41   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #88

Awesome tutorial Hawk.
I also want to see updates!
xOscar is offline
Tippler
New Member
Join Date: Feb 2015
Old 10-06-2022 , 11:41   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #89

Good old days
Tippler is offline
Reply


Thread Tools
Display Modes

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 04:39.


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