[Question] How to do SendFormattedMessage in PAWN?
#1

Hello. I've unusual problem, because I really don't know how to go about it. Now I explain what I mean - I want to get something like this, without #emit directive, because I don't know it:
pawn Код:
stock SendFormattedMessage(id, colour = -1, string[] = "", message[] = "", {Float, _}:...) {
    new str = sizeof(string[]);
    new data = {Float, _}:...;
    format(string, str, message, data);
    SendClientMessage(id, colour, message);
    printf(message);
}
Code above doesn't work and all what I need is to know (or to learn) how to use the {Float, _} or ... in format. If I'll use data - PAWN will not accept this and will show sort of errors like this:
Код:
error 017: undefined symbol "Float"
error 029: invalid expression, assumed zero
error 029: invalid expression, assumed zero
fatal error 107: too many error messages on one line
If I'll use only
pawn Код:
new data;
instead
pawn Код:
new data = {Float, _}:...;
or even
pawn Код:
new data = Float, _;
or
pawn Код:
new data = _;
it will compile, but it will not work. I thought about getting function arguments and save them into variable, but I don't know how to do it.
Reply
#2

What is the difference between message and string ?

I don't understand why your format is like that :3 (and also why creating a var to stock a sizeof )
Reply
#3

There are several ways - the usual one is with a macro, but there is also y_va that can do this for you.
Reply
#4

Code that I use is very simple and has only two arguments:
pawn Код:
SendFormattedMessage(playerid, .message = "b");
As you can see, I write a string variable in order to format it (and it's saved in memory), then I call the message (.message = "b") that is sent to the player in such a way. I test the code instantly, it works perfectly in that way, but it doesn't work when I want to do something more... advanced.

ColeMiner, I don't know anything about y_va and don't want to use macros, because I want dynamically saved size of variable. In macro it can be only statically and with typical value "128" or (maybe) little more.
Reply
#5

PHP код:
stock SendFormattedMessage(playeridcolour, const fmat[],  va_args<>)
{
    new 
str[145];
    
va_format(strsizeof (str), fmatva_start<3>);
    return (
playerid != -1) ? SendClientMessage(playeridcolourstr) : SendClientMessageToAll(colourstr);

LoL You Do This One With The FUCKING way :P


Any Way If I Helped You REP me
Not Westing Time
Reply
#6

If you don't know anything about a library, you look up the documentation and tutorials on it...
Reply
#7

No Really
When I Saw This Thread
In Reply TextBox I Wrote
I Learned It From A Person That You Know Him
Not All But Just The Last Line
But I Realy Didn't Go Too Tutorials Forum
Reply
#8

I did it. By the way, I've noticed that your code also creates only one static variable (with size 145), whilst nowhere operate on the size of arrays, so even when I'll try to format something like this:
pawn Код:
va_SendClientMessage(playerid, -1, "%s is good", PlayerName(playerid));
I only use 32 cells, and 113 are unused and mess up a memory (I know that nowadays it does not matter, but at any rate it is a waste of memory). So - I've decided to create my version of this and... it won't work (surprised?):
pawn Код:
stock SendFormattedMessage_(playerid, colour = -1, const string_[] = "", str_ = sizeof(string_), fmat[] = "", {Float, _}:...) {
    va_format(string_, str_, fmat, va_start<5>); // Compiler doesn't like "string_" and sends message " argument type mismatch (argument 1)"
    return (playerid != -1)? SendClientMessage(playerid, colour, fmat): SendClientMessageToAll(colour, fmat);
}
Is there really no other way to dynamically allocate size of the array, only the option to assign it a static size?
Reply
#9

That's more of a compile-time allocation than dynamic. Format expects first argument to be non-const, and fmt should be const:
pawn Код:
//Before
stock SendFormattedMessage_(playerid, colour = -1, const string_[] = "", str_ = sizeof(string_), fmat[] = "", {Float, _}:...)
//after
stock SendFormattedMessage_(playerid, colour = -1, string_[] = "", str_ = sizeof(string_), const fmat[] = "", {Float, _}:...)
Reply
#10

Hm... it seems that it works if I do
pawn Код:
stock SendFormattedMessage_(playerid, colour = -1, string_[] = "", str_ = sizeof(string_), const fmat[] = "", {Float, _}:...) {
    va_format(string_, str_, fmat, va_start<5>);
    return (playerid != -1)? SendClientMessage(playerid, colour, fmat): SendClientMessageToAll(colour, fmat);
}
and
pawn Код:
SendFormattedMessage_(playerid, .fmat = "b");
in
pawn Код:
OnPlayerConnect
everything is okay. But if I do
pawn Код:
new str___[128];
SendFormattedMessage_(playerid, -1, str___, sizeof(str___), "%s test", PlayerName(playerid));
in
pawn Код:
OnPlayerConnect
too, it crash my server.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)