SA-MP Forums Archive
[Tutorial] Macro Example VIA Distance Formula - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Macro Example VIA Distance Formula (/showthread.php?tid=268533)



Macro Example VIA Distance Formula - dowster - 12.07.2011

After reading ******' thread on macros i decided to make an example by making a simple distance formula macro. This is a 3 dimensional distance formula, because i needed to use one in making my custom chat radius. For more info on how this works you can view the thread by ******.

Код:
#define DISTANCE(%1,%2,%3,%4,%5,%6) floatsqroot((%1-%4)*(%1-%4) + (%2-%5)*(%2-%5) + (%3-%6)*(%3-%6))
That is the macro, as you can see %1 - %3 are they X, Y, Z, of the first location. Where %4 - %6 are the X, Y, Z of the second location. You could also have a macro with just two variables that gets the sum of said variables, like this one.
Код:
#define SUM(%1,%2) (%1+%2)
Which could be used in the following code to get the sum of A and B, and write it to C.
Код:
C = SUM( A, B);
Making the macro is faster and uses less code also, to create the exact same function for that macro you would need to do.
Код:
stock SUM( A, B)
{
      new c = A + B;
      return C;
}
Which takes up 5 lines VS. 1. Finally, an example command that uses the Distance Formula Macro could look as follows, using Y_command as my command processor of choice.
Код:
YCMD:distancefromstart(playerid, params[], help)
{
	if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Gets the distance between you and the only spawn point in this script");
    }
	else
	{
		new Float:x, Float:y, Float:z;
		GetPlayerPos(playerid, x, y, z);
		new string[64];
		format(string, sizeof(string), "%f, meters from start", (DISTANCE( x, y, z, 1958.3783, 1343.1572, 15.3746)));
		SendClientMessage( playerid, 0xFF3333FF, string);
	}
	return 1;
}
This is just a rough example thrown together in 2 Minutes, but it works.

When i wrote this i forgot a key bit of info:
Quote:

Substitution macros can take up to 10 parameters.

from the pawn language guide


Re: Macro Example VIA Distance Formula - Daren_Jacobson - 12.07.2011

Why not just do

C = A + B;

No function, No DEFINE.


Re: Macro Example VIA Distance Formula - dowster - 13.07.2011

I know, it was there to help understanding, ultimately when your code is compiled it does that because the macro functions are executed in the pawn pre processor. It just helps if you are doing things like distance formula because it speeds it up a bit by you not having to type all the code, and the server not having to call a function.


Re: Macro Example VIA Distance Formula - jameskmonger - 15.07.2011

Quote:
Originally Posted by dowster
Посмотреть сообщение
Код:
stock SUM( A, B)
{
      new c = A + B;
      return C;
}
Which takes up 5 lines VS. 1.
pawn Код:
stock SUM(A, B) return A + B;
Just saying.


Re: Macro Example VIA Distance Formula - dowster - 15.07.2011

Yeah but doing that, the code has to call the function which would take time, not a lot of time but it would still take some time. If you use a macro it is processed when it is compiled, so your compiled code for the distance function actually turns into this when its compiled.;
Код:
YCMD:distancefromstart(playerid, params[], help)
{
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Gets the distance between you and the only spawn point in this script");
    }
	else
	{
		new Float:x, Float:y, Float:z;
		GetPlayerPos(playerid, x, y, z);
		new string[64];
		format(string, sizeof(string), "%f, meters from start", (floatsqroot((x-1958.3783)*(x-1958.3783) + (y-1343.1572)*(y-1343.1572) + (z-15.3746)*(z-15.3746))));
		SendClientMessage( playerid, 0xFF3333FF, string);
	}
	return 1;
}
There are sometimes when its useful to create a macro, or just type it into the code. Your using the example where it'd just be easier to type it into the code. Not saying there's a right and a wrong way, both are valid, but I think many people would benefit from using a macro instead of a simple function.


Re: Macro Example VIA Distance Formula - Ricop522 - 15.07.2011

Quote:
Originally Posted by jameskmonger
Посмотреть сообщение
pawn Код:
stock SUM(A, B) return A + B;
Just saying.
Is this right?
#define SUM(%0, %1) ? (%0 + %1);

@topic


Cool tutorial, I like =p


Re: Macro Example VIA Distance Formula - RyDeR` - 15.07.2011

Quote:
Originally Posted by Ricop522
Посмотреть сообщение
Is this right?
#define SUM(%0, %1) ? (%0 + %1);

@topic


Cool tutorial, I like =p
pawn Код:
SUM(%0,%1) ((%0) + (%1))



Re: Macro Example VIA Distance Formula - dowster - 15.07.2011

Quote:
Originally Posted by Ricop522
Посмотреть сообщение
Is this right?
#define SUM(%0, %1) ? (%0 + %1);

@topic


Cool tutorial, I like =p
Thanks, and like RyDeR reposted, you cannot have the space between the comma after %0 and %1, because like a define that space will seperate the search term with the data it should replace the search term with. ? should just not be there. That would be the same as writing the following define.
Код:
#define MAX HOUSES ? 500;
it just wouldn't work out, and thanks, i saw ******' topic on the pre processor and thought that macros should get their "thread".