This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

UserMessage fails


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-13-2009 , 09:14   UserMessage fails
Reply With Quote #1

Hi.
Currently I'm trying to send a chat text to the user (for most of you it should be easy but not for me ).
Here's my code:
Code:
void cModule::chatMsg(int iFilter, int iIndex, const char *sMessage, ...)
{
    char sBuffer[256];

    va_list vArgs;
    va_start(vArgs, sMessage);
    vsprintf(sBuffer, sMessage, vArgs);
    va_end(vArgs);

    sprintf(sBuffer, "\x03[%s] \x04%s\n", this->sPrefix, sBuffer);

    MRecipientFilter mFilter;
    mFilter.AddAllPlayers();

    bf_write *bfUserMsg = gEngine->UserMessageBegin(&mFilter, gUtils.iUserMsgChat);
    bfUserMsg->WriteByte(0);
    bfUserMsg->WriteString(sBuffer);
    bfUserMsg->WriteByte(0);
    gEngine->MessageEnd();
}
It works, the message is send to the user but with an error. You see the sprintf. I use it to add an \n and to add the module prefix to the chat message. If I do that, the output is:
Code:
[PREFIX] [PREFIX] [PREFIX] [PREFIX] [PREFIX] [PREFIX] [PREFIX] [PREFIX]
If I leave out the sprintf, the text I want to send comes and everything works well. Why does the prefix appears so often?

The type of this->sPrefx is const char*.

I hope s.o. can help me

~ Chris
Chrisber is offline
BAILOPAN
Join Date: Jan 2004
Old 04-13-2009 , 10:31   Re: UserMessage fails
Reply With Quote #2

Unrelated aside: do not use sprintf/vsprintf on arbitrary input like that. They do not check the bounds of the buffer. Use snprintf/vsnprintf instead.
__________________
egg
BAILOPAN is offline
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-13-2009 , 11:55   Re: UserMessage fails
Reply With Quote #3

Hi.
Thanks for that. I will recognize it in future

But the problem isn't solved :/

~ Chris
Chrisber is offline
Keeper
Senior Member
Join Date: Nov 2006
Old 04-13-2009 , 12:49   Re: UserMessage fails
Reply With Quote #4

First off, I don't like this line:
Code:
sprintf(sBuffer, "\x03[%s] \x04%s\n", this->sPrefix, sBuffer);
I would put it in another character array to be sure. Maybe test this:

Code:
void cModule::chatMsg(int iFilter, int iIndex, const char *sMessage, ...)
{
    char sBuffer[256];
    char sBuffer2[256];.

    va_list vArgs;
    va_start(vArgs, sMessage);
    vsprintf(sBuffer, sMessage, vArgs);
    va_end(vArgs);

    sprintf(sBuffer2, "\x03[%s] \x04%s\n", this->sPrefix, sBuffer);

    MRecipientFilter mFilter;
    mFilter.AddAllPlayers();

    bf_write *bfUserMsg = gEngine->UserMessageBegin(&mFilter, gUtils.iUserMsgChat);
    bfUserMsg->WriteByte(0);
    bfUserMsg->WriteString(sBuffer2);
    bfUserMsg->WriteByte(0);
    gEngine->MessageEnd();
}
Also, on a side note, if you're wanting the \x03 to work, you'll need to change the first
Code:
 bfUserMsg->WriteByte(0);
to be
Code:
bfUserMsg->WriteByte( engine->IndexOfEdict( pEntity ) )
where the pEntity is the speaker. Otherwise the colors won't work with the team.

Last edited by Keeper; 04-13-2009 at 12:51.
Keeper is offline
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-13-2009 , 15:35   Re: UserMessage fails
Reply With Quote #5

I don't want to use chat with team color, I only want to output a normal message with gold, green and lightgreen :/

~ Chris
Chrisber is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-13-2009 , 17:26   Re: UserMessage fails
Reply With Quote #6

Not being sure if that came through:

The prefix most likely comes so often because the output buffer pointer you pass to sprintf is the same as one of the input parameters. The standard says that behaviour in such case is undefined (analogous to overlapping memory areas passed to memcpy).
__________________
hello, i am pm
PM is offline
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-13-2009 , 19:31   Re: UserMessage fails
Reply With Quote #7

OMFG!!!
Thanks, that was the error, lol.
Thanks to all

EDIT: Why does the console puts out the following text when I'm sending the usermsg?!
Code:
SV_BroadcastMessage:  Recipient Filter for message type 23 (reliable: no, init: no) with bogus client index (X) in list of X clients
:S

~ Chris

Last edited by Chrisber; 04-13-2009 at 19:34.
Chrisber is offline
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-16-2009 , 07:13   Re: UserMessage fails
Reply With Quote #8

Hi.
I found out, that the message apears X times (X = count of players in RecipientFilter).

Does anybody has an idea? :S That message spams my console :/

~ Chris
Chrisber is offline
Keeper
Senior Member
Join Date: Nov 2006
Old 04-16-2009 , 10:41   Re: UserMessage fails
Reply With Quote #9

The chatMsg isn't being called during a loop is it?
Keeper is offline
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-16-2009 , 12:11   Re: UserMessage fails
Reply With Quote #10

No.
It's called once.

~ Chris
Chrisber is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode