[Tutorial] Macro Example VIA Distance Formula
#1

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
Reply


Messages In This Thread
Macro Example VIA Distance Formula - by dowster - 12.07.2011, 21:55
Re: Macro Example VIA Distance Formula - by Daren_Jacobson - 12.07.2011, 23:37
Re: Macro Example VIA Distance Formula - by dowster - 13.07.2011, 03:03
Re: Macro Example VIA Distance Formula - by jameskmonger - 15.07.2011, 11:34
Re: Macro Example VIA Distance Formula - by dowster - 15.07.2011, 17:40
Re: Macro Example VIA Distance Formula - by Ricop522 - 15.07.2011, 19:30
Re: Macro Example VIA Distance Formula - by RyDeR` - 15.07.2011, 20:49
Re: Macro Example VIA Distance Formula - by dowster - 15.07.2011, 20:59

Forum Jump:


Users browsing this thread: 2 Guest(s)