AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Ham_Touch spam issue (https://forums.alliedmods.net/showthread.php?t=292444)

heroicpower7613 01-06-2017 14:20

Ham_Touch spam issue
 
im new to scripting
i have a simple code that prints a message when you're touching player.
problem is its spamming the message like crazy when touching.
how can i get it to print only one message and not lag the server?

code:
PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <cstrike>
#include <hamsandwich>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

public plugin_init() {
    
    
register_plugin(PLUGINVERSIONAUTHOR)
    
RegisterHam(Ham_Touch"player""PlayerTouch");
    
// Add your code here...
}

public 
PlayerTouch(idplayer) {
    
    if( 
is_user_aliveid ) && is_user_aliveplayer ) && cs_get_user_teamid ) ==CS_TEAM_T && cs_get_user_teamplayer ) == CS_TEAM_CT ) { //if you are CT and touch T
        
        
new touch_target[32]
        
get_user_name(idtouch_target31);
        
client_printplayerprint_chat"touching: %s [terrorist]"touch_target );
    }
    return 
PLUGIN_CONTINUE;



OciXCrom 01-06-2017 14:44

Re: Ham_Touch spam issue
 
Of course it will be spaming. You are touching the player all the time. Add a simple timer and send the message only if X seconds have passed since the last message.

heroicpower7613 01-06-2017 15:14

Re: Ham_Touch spam issue
 
Quote:

Originally Posted by OciXCrom (Post 2484130)
Add a simple timer and send the message only if X seconds have passed since the last message.

idk how to do it

OciXCrom 01-06-2017 16:41

Re: Ham_Touch spam issue
 
Then search. There are approximately 644628 examples of how to do it out there.

Craxor 01-06-2017 16:57

Re: Ham_Touch spam issue
 
like that:
PHP Code:

// Here we insert the current times + the necesary time to wait
new g_Time[33];


public 
touchid )
{
    
// here we get the exact current seconds passed from map/server start.
    
new iCurrentTime floatroundget_gametime() );

    
// We check if 'now' is bigger or equal with the g_Time, wich means if he past 10 seconds.
    
if( iCurrentTIme >= g_Timeid ] )
    {
        
// Times passed, you can use now!
        // your code here ...


        // Also we update the time:
        // +10 , how many seconds he need to wait until he can use again the command.
        
g_Timeid ] = iCurrentTime 10;
    }

    else
    {
        
client_printid print_chat"wait %i seconds before use again this command!"g_Timeid ] - iCurrentTIme );    
    }




heroicpower7613 01-06-2017 17:21

Re: Ham_Touch spam issue
 
Quote:

Originally Posted by Craxor (Post 2484176)
like that:

thanks my man, it works

PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <cstrike>
#include <hamsandwich>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

new g_Time[33];

public 
plugin_init() {
    
    
register_plugin(PLUGINVERSIONAUTHOR)
    
RegisterHam(Ham_Touch"player""PlayerTouch");
    
// Add your code here...
}

public 
PlayerTouch(idplayer) {
    
    if( 
is_user_aliveid ) && is_user_aliveplayer ) && cs_get_user_teamid ) ==CS_TEAM_T && cs_get_user_teamplayer ) == CS_TEAM_CT ) { //you are CT and touch T
        
        
new iCurrentTime floatroundget_gametime() );
        if( 
iCurrentTime >= g_Time[player] ) 
        {
        
            new 
touch_target[32]
            
get_user_name(idtouch_target31);
            
client_print(playerprint_chat"touching: %s [t]"touch_target);
            
g_Time[player] = iCurrentTime 10;

        } else {

            return 
PLUGIN_HANDLED;
        }
        
    }
    return 
PLUGIN_HANDLED;



PRoSToTeM@ 01-06-2017 19:37

Re: Ham_Touch spam issue
 
@Craxor what is the purpose of using rounded time? Rounded time in your code gives not 10 secs, but from 9.5 to 10.5 secs.
So:
PHP Code:

#define GetCurrentGameTime get_gametime

new Float:g_nextNotificationTime[MAX_PLAYERS 1];

public 
PlayerTouch(touchedPlayertoucher)
{
    if (
GetCurrentGameTime() >= g_nextNotificationTime[touchedPlayer])
    {
        
// Notify touched player
        
        
const delaySeconds 10;
        
g_nextNotificationTime[touchedPlayer] = GetCurrentGameTime() + delaySeconds;
    }
    
    
// if you need to count remain seconds you can use this:
    // floatround(g_nextNotificationTime[touchedPlayer] - GetCurrentGameTime(), floatround_ceil)



Bugsy 01-06-2017 23:14

Re: Ham_Touch spam issue
 
Instead of putting a timer on the touch, you can track the last player that was touched and not register a touch again until a different player is touched.

1 touches 2 [touch fires]
1 touches 2 [nothing happens]
1 touches 2 [nothing happens]
1 touches 4 [touch fires]
1 touches 4 [nothing happens]
1 touches 2 [touch fires]

PHP Code:

#include <amxmodx>
#include <hamsandwich>

#define MAX_PLAYERS 32

#define IsPlayer(%1)    (1<=%1<=MAX_PLAYERS)

new g_LastTouchedMAX_PLAYERS ];

public 
plugin_init() 
{
    
RegisterHamHam_Touch "player" "PlayerTouch" );
}

public 
client_connectid 
{
    
g_LastTouchedid ] = 0;
}

public 
PlayerTouchid player 
{
    if ( 
IsPlayerid ) && IsPlayerplayer ) )
    {
        if ( 
g_LastTouchedid ] != player )
        {
            
//Touched player
            
client_printid print_chat "Touched %d" player );
        }
        
        
g_LastTouchedid ] = player;
    }



HamletEagle 01-07-2017 05:22

Re: Ham_Touch spam issue
 
@Bugsy:
1 touch 2
1 stop touching 2
1 touch 2 again

Craxor 01-07-2017 10:20

Re: Ham_Touch spam issue
 
Prostet. i don't understand what are you saying, is working as is supposed to work..


All times are GMT -4. The time now is 01:56.

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