Advertisement
Guest User

SendFormattedMessage example

a guest
Mar 31st, 2010
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.73 KB | None | 0 0
  1. // This is a comment
  2. // uncomment the line below if you want to write a filterscript
  3. #define FILTERSCRIPT
  4.  
  5. #include <a_samp>
  6.  
  7. public OnFilterScriptInit()
  8. {
  9.     new msg[30];
  10.    
  11.     format(msg, sizeof(msg), "Hello, %s!", "World");
  12.     print(msg);
  13.     return 1;
  14. }
  15.  
  16. public OnFilterScriptExit()
  17. {
  18.     return 1;
  19. }
  20.  
  21. public OnPlayerCommandText(playerid, cmdtext[])
  22. {
  23.     if (strcmp("/pos", cmdtext, true, 10) == 0)
  24.     {
  25.         new Float:x;
  26.         new Float:y;
  27.         new Float:z;
  28.        
  29.         GetPlayerPos(playerid, x, y, z);
  30.         SendFormattedMessage(playerid, 0x00FFFFFF, "Your position is: %f, %f, %f", x, y, z);
  31.         return 1;
  32.     }
  33.     return 0;
  34. }
  35.  
  36. stock SendFormattedMessage(playerid, color, fstring[], {Float, _}:...)
  37. {
  38.     // This is the number of parameters which are not variable that are passed
  39.     // to this function (i.e. the number of named parameters).
  40.     static const
  41.         STATIC_ARGS = 3,
  42.         BYTES_PER_CELL = cellbits / 8;
  43.     // Get the number of variable arguments.
  44.     new
  45.         n = (numargs() - STATIC_ARGS) * BYTES_PER_CELL;
  46.     if (n)
  47.     {
  48.         new
  49.             message[128],
  50.             arg_start,
  51.             arg_end;
  52.  
  53.         // Load the real address of the last static parameter.  Do this by
  54.         // loading the address of the last known static parameter and then
  55.         // adding the value of [FRM].
  56.         #emit CONST.alt                fstring
  57.         #emit LCTRL                    5
  58.         #emit ADD
  59.         #emit STOR.S.pri               arg_start
  60.  
  61.         // Load the address of the last variable parameter.  Do this by adding
  62.         // the number of variable parameters on the value just loaded.
  63.         #emit LOAD.S.alt               n
  64.         #emit ADD
  65.         #emit STOR.S.pri               arg_end
  66.  
  67.         // Push the variable arguments.  This is done by loading the value of
  68.         // each one in reverse order and pushing them.  I'd love to be able to
  69.         // rewrite this to use the values of pri and alt for comparison,
  70.         // instead of having to constantly load and reload two variables.
  71.         do
  72.         {
  73.             #emit LOAD.I
  74.             #emit PUSH.pri
  75.             arg_end -= BYTES_PER_CELL;
  76.             #emit LOAD.S.pri           arg_end
  77.         }
  78.         while (arg_end > arg_start);
  79.  
  80.         // Push the static format parameters.
  81.         #emit PUSH.S                   fstring
  82.         #emit PUSH.C                   128
  83.         #emit PUSH.ADR                 message
  84.  
  85.         // Now push the number of arguments passed to format, including both
  86.         // static and variable ones and call the function.
  87.         n += BYTES_PER_CELL * 3;
  88.         #emit PUSH.S                   n
  89.         #emit SYSREQ.C                 format
  90.  
  91.         // Remove all data, including the return value, from the stack.
  92.         n += BYTES_PER_CELL;
  93.         #emit LCTRL                    4
  94.         #emit LOAD.S.alt               n
  95.         #emit ADD
  96.         #emit SCTRL                    4
  97.  
  98.         return SendClientMessage(playerid, color, message);
  99.     }
  100.     else
  101.     {
  102.         return SendClientMessage(playerid, color, fstring);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement