AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Replacing a SayText message with arguments (https://forums.alliedmods.net/showthread.php?t=293464)

OciXCrom 02-01-2017 12:54

Replacing a SayText message with arguments
 
PHP Code:

public plugin_init()
    
register_message(get_user_msgid("TextMsg"), "msgTextMsg")

public 
msgTextMsg(iMessageiDestid)

    static 
szMessage[32]
    
get_msg_arg_string(2szMessagecharsmax(szMessage))
    
    
set_hudmessage(bla bla)
    
show_hudmessage(idszMessage)


If the message contains arguments, like this one:

PHP Code:

"Cstrike_TitlesTXT_Game_bomb_drop"            "%s1 dropped the bomb" 

... how do I get the full formated message without the "%s" and send it as a HUD message?

HamletEagle 02-01-2017 13:22

Re: Replacing a SayText message with arguments
 
Code:

byte        DestinationType
string        Message
string        Submsg
string        Submsg
string        Submsg
string        Submsg


OciXCrom 02-01-2017 14:59

Re: Replacing a SayText message with arguments
 
So, how would I format the message acording to the number of substrings? I know that I can find their number with get_msg_args(), but how to add them in the message?

PHP Code:

new szSubString[get_msg_args()][32]
// This is invalid, because it isn't a constant expression

format(szMessagecharsmax(szMessage), "%s"szMessageszSubstring[0], szSubstring[1], ...) // ? 

This has been bugging me for a long time and I still don't have a clue how to do it. I just need the %s parameters to be automatically replaced by the respective substrings.

OciXCrom 02-01-2017 15:30

Re: Replacing a SayText message with arguments
 
I tried using format_args(), but it returns a blank string. Not even sure if this function is used here.

SpannerSpammer 02-02-2017 08:42

Re: Replacing a SayText message with arguments
 
You need to rebuild the message being sent by mapping out what the
final message text is being processed by the client. In your example
here, the bomb drop TextMsg #Game_bomb_drop is sent to the
client, who then looks up the entry in cstrike/titles.txt which is another
localized string: #Cstrike_TitlesTXT_Game_bomb_drop which
is then looked up in resource/cstrike_english.txt .

Simplify this process so that you get the final msg from the TextMsg
by using you own function to map out the messages being sent.
CS doesn't seem to use more than 3 of the 4 optional substrings BTW.

The code below is untested, but you get the picture.

Spoiler

OciXCrom 02-02-2017 09:23

Re: Replacing a SayText message with arguments
 
So basically I'll need to check the number of parameters 1 by 1 and form the messages with all the checks again. I guess I can do that if there's no easier way.

OciXCrom 02-02-2017 09:50

Re: Replacing a SayText message with arguments
 
Oh, I actually came up with an automized way to do this using a simple loop:

PHP Code:

public msgTextMsg(iMessageiDestid)

    static 
szMessage[64]
    
get_msg_arg_string(2szMessagecharsmax(szMessage))
    
    if(
TrieKeyExists(g_eTries[TRIE_MESSAGES], szMessage))
    {
        new 
szNewMessage[128], iType
        TrieGetString
(g_eTries[TRIE_MESSAGES], szMessageszNewMessagecharsmax(szNewMessage))
        
TrieGetCell(g_eTries[TRIE_TYPE], szMessageiType)
        
        new 
iArgs get_msg_args()
        
        if(
iArgs 2)
        {
            for(new 
szSubString[32], 2iArgsi++)
            {
                
get_msg_arg_string(1szSubStringcharsmax(szSubString))
                
replace(szNewMessagecharsmax(szNewMessage), SYM_SUBSTRINGszSubString)
            }
        }
        
        
replace_all(szNewMessagecharsmax(szNewMessage), SYM_SUBSTRING"")

        ... 
send message 

Thanks for pointing me in the right direction. :)

HamletEagle 02-02-2017 09:51

Re: Replacing a SayText message with arguments
 
You can progresively add them to the message. Something like:
PHP Code:

const baseArguments 2

new argsNum get_msg_args()
if(
argsNum baseArguments)
{
    new 
subStringsNum argsNum baseArguments
    
for(new baseArguments 1<= subStringsNumi++)
    {
        
get_msg_arg_string(isubStringcharsmax(subString))
    }


And replace each %s with the subString value.

OciXCrom 02-02-2017 10:20

Re: Replacing a SayText message with arguments
 
1 minute too late. :) Thanks anyways.


All times are GMT -4. The time now is 21:03.

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