SA-MP Forums

SA-MP Forums (
-   Scripting Help (
-   -   [Macro] Mutliple macros in one (

Dayrion 27/07/2017 05:19 PM

[Macro] Mutliple macros in one
Hey there.
I'm trying to solve this problem of "redefining the macro". My english is bad so I will show you the code and I hope you can help me.
PHP Code:

#define MACRO1<%0>[%1]     %0[%1]
#define MACRO1<%0>        %0 

I thought about to make multiple macros but I don't know how to separate if the player enter a specific index (or length if he declares the variable) and when he doesn't enter something.

OneDay 28/07/2017 09:21 AM

Re: [Macro] Mutliple macros in one

Dayrion 28/07/2017 12:41 PM

Re: [Macro] Mutliple macros in one

Originally Posted by OneDay (Post 3907221)

That's not helping me

Misiur 29/07/2017 09:09 AM

Re: [Macro] Mutliple macros in one

#include <a_samp>

#define MACRO1 [email protected]:[email protected]:@MACRO1
#define [email protected]:[email protected]:@MACRO1<%0>[%1]    %0[%1]
#define [email protected]:@MACRO1<%0>            %0 

main() {
    new Foo[32] = { -5, ... };
    new bar = MACRO1<Foo>[1];
    new foobar = MACRO1<5>;
    printf("%d %d", bar, foobar);

Try something like this

Dayrion 29/07/2017 05:39 PM

Re: [Macro] Mutliple macros in one
PHP Code:

   new MACRO1<Test_array>[1];

compile as:
PHP Code:

    new Test_array[1];

Can you add some explications please?

Misiur 29/07/2017 06:46 PM

Re: [Macro] Mutliple macros in one
Sure, but I suck at explaining, however I'll try. So as you've noticed, you can't have two macros with the same name. But wait, they are different! But that doesn't matter, as the name is only the first part until first invalid character. You can read more about that here, under "definitions" and "macros". So, we do a little trick. We are gona exploit tag system and macro parser rules - the ":" character. It's both macro name-ending character, as it is tag operator. As for tags, read up about weak tags. Weak tags don't trigger tag mismatch errors. I've chosen "[email protected]" as a pattern for our tags. Why? I think I saw it in some YSI library, and also it minimizes chance of name collisions later on. Now all the pieces together

#define MACRO1 [email protected]:[email protected]:@MACRO1 // Create our chain. Macro name: "MACRO1"
#define [email protected]:[email protected]:@MACRO1<%0>[%1]    %0[%1] // Macro name "[email protected]"
#define [email protected]:@MACRO1<%0>            %0 // Macro name "[email protected]"

All that's left is our [email protected] tag as you've noticed. It can cause errors, if it does, you can chain tags and only outermost will be considered:

new Float:bar = MACRO1<foo>; //Tag mismatch
new Float:bar = Float:MACRO1<foo>; //All good

That's why you can nest even deeper as only outermost tag counts:

#include <a_samp>

#define MACRO1 [email protected]:[email protected]:[email protected]:@MACRO1
#define [email protected]:@MACRO1$%0$                (%0 + %0)
#define [email protected]:[email protected]:@MACRO1<%0>            %0 
#define [email protected]:[email protected]:[email protected]:@MACRO1<%0>[%1]    %0[%1]

main() {
    new Foo[32] = { -5, ... };
    new bar = MACRO1<Foo>[1];
    new Float:foobar = Float:MACRO1<5>;
    new fizzbar = MACRO1$15$;
    printf("%d %f %d", bar, foobar, fizzbar);

I'm not 100% certain all I said is true and/or relevant, so anyone - feel free to correct me.

Dayrion 29/07/2017 09:50 PM

Re: [Macro] Mutliple macros in one
You explained perfectly. Thanks you so much (and I just noticed you written the tutorial about macro, he is perfect and well explained).
The problem is the weak tag because the macro return the variable with the tag every time (tag:variable). Anyway, that helped me a lot

Dayrion 31/07/2017 12:14 AM

Re: [Macro] Mutliple macros in one
I would never have thought this code by myself, thanks again! :-[

Dayrion 22/11/2017 03:29 PM

Re: [Macro] Mutliple macros in one
Is that possible to use the same trick without the tag? I did the same trick but it applies on a function so it return a warning message. :/

Y_Less 22/11/2017 05:41 PM

Re: [Macro] Mutliple macros in one
Yes and no. No, you need the tags - they are the only way I've ever found of running multiple macros in this way. Most of the time, you can get away with this even on functions because you can use some tricks like giving the function a tag, using the name of the function itself as a macro, or only operating on the parameters. However, I did fairly recently (well, over a year ago now) come up with a way to use an arbitrary number of tag macros on functions. Since this was such a major discovery (for me, as like you I'd wanted the ability to do this for a long time) I called it the "ultimate macro" and chose the name "__" for it - double underscore (a very privaledged name):

PHP Code:

#define [email protected]$ 0]);
#define __:%0$ [email protected](u[_:%0 [email protected]


Also see: (but bear in mind that that code calls it "[email protected]" not "__" for readability.

Edit: I said it was over a year ago, and I was right - but only just. It was almost a year ago to the day!

All times are GMT. The time now is 04:07 PM.

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