View Single Post
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 09-03-2022 , 17:06   Re: Translation in a timer action possible?
Reply With Quote #8

Quote:
Originally Posted by JLmelenchon View Post
The whole difficulty now is that i have to get the name of a particular client that was in an array and trigger on player disconnect ...
If you want to show a message with the name of a client. Won't work if the client has disconnected during the timer interval.
PHP Code:
void SomeFunction(int client)
{
    
CreateTimer(1.0Timer_TESTGetClientUserId(client));
}


Action Timer_TEST(Handle timerany data)
{
    
int client GetClientOfUserId(data);
    if (!
client// check if client disconnected during that 1s delay
    
{
        return 
Plugin_Stop;
    }
    
CPrintToChatAll("%t""translation_message"client); // CPrintToChatAll loops CPrintToChat, it is a bit special as it allows %t instead of %T

    
return Plugin_Stop;

example translation file (@Cruze 's example)
PHP Code:
"Phrases"
{
    
"translation_message"
    
{
        
"#format"        "{1:N}" // 1=client index
        
"en"            "This is translated message for {1}."
    
}



Still want the message to show even if the player disconnected? A bit more annoying, you'll have to use a DataPack.
Here's the code for it:

-- If you want to show a message with the name of a client. WILL work if the client has disconnected during the timer interval. --
-- You can also call SomeFunction() on OnClientDisconnect(), because OnClientDisconnect() is a pre-hook, so the client is still counted as in the game when it's called. --
PHP Code:
void SomeFunction(int client)
{
    
// Get client name
    
char sName[MAX_NAME_LENGTH 1];
    
GetClientName(clientsNamesizeof(sName));

    
// Store the client name in a datapack
    
DataPack hData = new DataPack();
    
hData.WriteCell(GetClientOfUserId(client)); // Pass the client userid in the datapack, optional, but could be useful
    
hData.WriteString(buffer); // Pass the client name in the datapack
    
CreateTimer(1.0Timer_TESThData);
}


Action Timer_TEST(Handle timerany data)
{
    
// ALWAYS DO 'delete data;' BEFORE EXITING THE TIMER!

    
data.Reset(); // Resets the cursor/position in the datapack
    
int client GetClientOfUserId(data.ReadCell());
    if (!
client// check if client disconnected during that 1s delay
    
{
        
delete data// Prevents a memory leak
        
return Plugin_Stop;
    }

    
char sName[MAX_NAME_LENGTH 1];
    
data.ReadString(sNamesizeof(sName));
    
delete data// Prevents a memory leak
    
CPrintToChatAll("%t""translation_message"sName); // CPrintToChatAll loops CPrintToChat, it is a bit special as it allows %t instead of %T

    
return Plugin_Stop;

example translation file
PHP Code:
"Phrases"
{
    
"translation_message"
    
{
        
"#format"        "{1:s}" // 1=client name
        
"en"            "This is translated message for {1}."
    
}

You could also use CreateDataTimer() but I never use it personally.
__________________
GitHub | Discord: @azalty | Steam

Last edited by azalty; 09-03-2022 at 17:13.
azalty is offline