SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 19/01/2020, 10:08 PM   #1
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Macros

pawn Code:
#define     IncreaseWantedLevel(%1, %2)         pWantedLevel[%1] += %2
#define     SetWantedLevel(%1, %2)              pWantedLevel[%1] = %2

Trying to use these, but keep on getting errors.
FunnyBear is offline   Reply With Quote
Old 19/01/2020, 10:30 PM   #2
d3Pedro
Huge Clucker
 
Join Date: Apr 2012
Location: darkness
Posts: 294
Reputation: 91
Default Re: Macros

Why not a function
pawn Code:
IncreaseWantedLevel(player, amount)
{
    pWantedLevel[player] += amount;
}

and then you can do...

pawn Code:
#define     SetWantedLevel(%1, %2)              IncreaseWantedLevel(%1, %2)
d3Pedro is offline   Reply With Quote
Old 19/01/2020, 11:05 PM   #3
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Re: Macros

Quote:
Originally Posted by d3Pedro View Post
Why not a function
pawn Code:
IncreaseWantedLevel(player, amount)
{
    pWantedLevel[player] += amount;
}

and then you can do...

pawn Code:
#define     SetWantedLevel(%1, %2)              IncreaseWantedLevel(%1, %2)
Aha, no. They have two different functions, I should have explained clearer. IncreaseWantedLevel is to increase it by a certain amount, SetWantedLevel is to set it to a certain amount.

If I use SetWantedLevel in a normal stock. Can I atleast..

pawn Code:
#define     IncreaseWantedLevel(%1, %2)              pWantedLevel[%1] = pWantedLevel[%1] + %2
FunnyBear is offline   Reply With Quote
Old 19/01/2020, 11:08 PM   #4
Hassel
Little Clucker
 
Join Date: Dec 2019
Posts: 35
Reputation: 12
Default Re: Macros

pawn Code:
#define     IncreaseWantedLevel(%1,%2)         pWantedLevel[%1] += %2
#define     SetWantedLevel(%1,%2)              pWantedLevel[%1] = %2
Hassel is offline   Reply With Quote
Old 19/01/2020, 11:34 PM   #5
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 512
Reputation: 15
Default Re: Macros

Quote:
Originally Posted by Hassel View Post
pawn Code:
#define     IncreaseWantedLevel(%1,%2)         pWantedLevel[%1] += %2
#define     SetWantedLevel(%1,%2)              pWantedLevel[%1] = %2
This is exactly what I already wrote?
FunnyBear is offline   Reply With Quote
Old 19/01/2020, 11:43 PM   #6
Kasichok
Big Clucker
 
Kasichok's Avatar
 
Join Date: Sep 2018
Location: weed farm
Posts: 64
Reputation: 24
Default Re: Macros

Marco doesnt support space so it should be

PHP Code:
#define     IncreaseWantedLevel(%1,%2)         pWantedLevel[%1] += %2
#define     SetWantedLevel(%1,%2)              pWantedLevel[%1] = %2 
Hassel is correct he done it without space you did it with space.
__________________



STRESSED? TRY KASICHOK AND RELAX


Kasichok is offline   Reply With Quote
Old 19/01/2020, 11:46 PM   #7
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,518
Reputation: 1635
Default Re: Macros

^
After a space it assumes you are already defining what it should be:

Code:
#define Say Hi; print("Hello world");
Instead of Say Hi; being translated to print("Hello world");, Say would be translated to Hi; print("Hello world");
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Kwarde is offline   Reply With Quote
Old 20/01/2020, 04:09 AM   #8
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,893
Reputation: 1321
Default Re: Macros

You know, I've done this myself and yeah it can be useful but a lot of the time it is cumbersome and you know if you are building a gamemode it is a good idea to focus on encapsulating data which means using functions typically is a better approach. If the macros are explicit to the module they are used in that would seem like a better use to me.
Pottus is offline   Reply With Quote
Old 22/01/2020, 09:11 AM   #9
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,518
Reputation: 1635
Default Re: Macros

@Pottus: What I'm about to say I'm pretty sure you know, but I'm posting it here for those who eventually don't

Functions are compiled into the AMX files (and thus in the memory). Macro's are translated to their defined value by the preprocessor (before compiling).

Now how this script consumes 113 bytes in the .amx file:
Code:
native print(const string[]);

#define HelloWorld() print("Hello world!")

main()
	HelloWorld();
And this script consumes 123 bytes in the .amx file -and acts exactly the same as the previous script in the server's output:
Code:
native print(const string[]);

main()
	HelloWorld();
	
HelloWorld()
	print("Hello world!");
Try compiling them both with option -a (Put '#pragma option -a' in the script or run pawncc with option -a) and see the difference in both created .asm (assembler code) files.

I've seen scripts using something like this:
Code:
new p_AdminLevel[MAX_PLAYERS], bool:p_Logged[MAX_PLAYERS], p_Money[MAX_PLAYERS];

stock GetPlayerAdminLevel(playerid)
    return p_AdminLevel[playerid];

stock IsPlayerLogged(playerid)
    return p_Logged[playerid];

stock GetPlayerCash(playerid)
    return p_Money[playerid];

stock GivePlayerCash(playerid, cash)
    return p_Money[playerid] += cash;

stock SetPlayerCash(playerid, cash)
    return p_Money[playerid] = cash;
These could all easily be defined. These are now all compiled into the script and into the memory. Calling the function GetPlayerAdminLevel(playerid) would first address the memory entry for GetPlayerAdminLevel, which would retrieve the value of memory entry where p_AdminLevel is, passing the value to the function that called GetPlayerAdminLevel. Note how it is passed via-via instead of directly to the calling function which is totally unneeded. By using:
Code:
new p_AdminLevel[MAX_PLAYERS], bool:p_Logged[MAX_PLAYERS], p_Money[MAX_PLAYERS];

#define GetPlayerAdminLevel(%0) p_AdminLevel[%0]
#define IsPlayerLogged(%0) p_Logged[%0]
//etc
This script:
Code:
public OnPlayerSpawn(playerid)
{
    if (!IsPlayerLogged(playerid))
        Kick(playerid);
}
The pre-processes would make this of it before compiling the script:
Code:
public OnPlayerSpawn(playerid)
{
    if (!p_Logged[playerid])
        Kick(playerid);
}
which is wáy better for 'functions' like that, both for memory usage, scripting convenience and runtime (though you'd never notice the difference -especially on servers/PCs these days, it's just a real waste to use functions for such things).
Also, such functions really don't need to have "stock " in front of them since you're sure you're using them anyway. One should never use stock unless if you're creating an include with functions that are optional to use.
What if you have a huge gamemode, a feature gets removed but you forget to remove stocked functions? They'd linger in the script forever without you knowing since you'll never get unused symbols warnings.

Note how this explaination was quite bluntly but this is basically how it works.
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/

Last edited by Kwarde; 22/01/2020 at 09:46 AM.
Kwarde is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macros? KingOfStarfox Scripting Help 2 13/08/2016 03:55 AM
[Ajuda] Macros AndrePls Português/Portuguese 11 25/12/2012 08:05 PM
Need some help with macros Meta Scripting Help 2 04/11/2011 03:12 PM
Help Macros TheArcher Help Archive 4 24/07/2011 12:25 PM


All times are GMT. The time now is 02:07 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.