View Single Post
MaNaReaver
Member
Join Date: Apr 2020
Location: India
Old 08-23-2020 , 14:11   Re: Help on register_forward
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
It triggers when whatever you are hooking occurs.

I would recommend using register_touch() from the engine module if you are looking to detect when a player touches something, since it allows you to specify both classnames, whereas using FM_Touch, you need to do further logic/conditions to react on only specific entities.
I was working on rewriting this plugin, and I was unsure as to how I should use register_touch to work for a specific player. The code's attached below. Do you think this would be inefficient, and needs editing?

Code:
#include <amxmodx>
#include <fakemeta>
#include <fakemeta_util>

#pragma semicolon 1 

new g_bStopwatchStarted[33]; 

new g_iLocId[2][33]; 
new Float:g_fLoc[2][33][3];

new g_iMaxPlayers; 
new g_iSayText;

public plugin_init( ) 
{
    register_plugin( "Stopwatch", "1.1b", "SchlumPF*" );
    register_clcmd( "say /stopwatch", "menuDisplay" );
    register_menucmd( register_menuid( "\rStopwatch - SchlumPF^n^n" ), 1023, "menuAction" );
    register_forward( FM_Touch, "fwdTouch" );
    
    g_iSayText = get_user_msgid( "SayText" );
    g_iMaxPlayers = get_maxplayers( );
}

public menuDisplay( plr ) 
{
    static menu[2048];
    new keys = ( 1<<0 | 1<<1 | 1<<2 | 1<<9 );
    
    new len = format( menu, sizeof menu - 1, "\rStopwatch - SchlumPF^n^n" );
    
    len += format( menu[len], sizeof menu - len, "\r01. \wStart Loc^n" );
    len += format( menu[len], sizeof menu - len, "\r02. \wFinish Loc^n" );
    len += format( menu[len], sizeof menu - len, "\r03. \wReset Locs^n" );
    len += format( menu[len], sizeof menu - len, "\r04. \wReset Timer^n^n" );
    len += format( menu[len], sizeof menu - len, "\r00. \wExit" );
    
    show_menu( plr, keys, menu, -1 );
    
    return PLUGIN_HANDLED;
}

public menuAction( plr, key ) 
{
    switch( key )
    {
        case 0:
        {
            fnCreateLoc( plr, "start" );
            menuDisplay( plr );
        }
        case 1: 
        {
            fnCreateLoc( plr, "stop" );
            menuDisplay( plr );
        }
        case 2: 
        {
            fnResetLocs( plr );
            menuDisplay( plr );
        }
        case 3:
        {
            if( g_bStopwatchStarted[plr] )
            {
                g_bStopwatchStarted[plr] = false;
            }
            fnGreenChat( plr, "[Stopwatch] ^x01Your timer has been resetted!" );
            menuDisplay( plr );
        }
        case 9: show_menu( plr, 0, "" );
    }
    
    return PLUGIN_HANDLED;
}

public fnCreateLoc( plr, type[] ) 
{
    if( equali( type, "start" ) )
    {    
        if( g_iLocId[0][plr] )
        {    
            fnGreenChat( plr, "[Stopwatch] ^x01You have to delete the loc first" );
            return 0;
        }
    }
    else if( equali( type, "stop" ) )
    {    
        if( g_iLocId[1][plr] )
        {
            fnGreenChat( plr, "[Stopwatch] ^x01You have to delete the loc first" );
            return 0;
        }
    }
    
    new Float:origin[3];
    pev( plr, pev_origin, origin );
    
    new ent = engfunc( EngFunc_CreateNamedEntity, engfunc( EngFunc_AllocString, "info_target" ) );
    
    set_pev( ent, pev_solid, SOLID_TRIGGER );
    set_pev( ent, pev_movetype, MOVETYPE_NONE );
    engfunc( EngFunc_SetModel, ent, "models/w_c4.mdl" );
    engfunc( EngFunc_SetSize, ent, Float:{ -16.0, -16.0, -16.0 }, Float:{ 16.0, 16.0, 16.0 } );
    engfunc( EngFunc_SetOrigin, ent, origin );
    
    if( equali( type, "start" ) )
    {
        set_pev( ent, pev_classname, "start" );
        fm_set_rendering( ent, kRenderFxGlowShell, 0, 255, 0, kRenderNormal, 16 );
        
        g_iLocId[0][plr] = ent;
        
        g_fLoc[0][plr][0] = origin[0];
        g_fLoc[0][plr][1] = origin[1];
        g_fLoc[0][plr][2] = origin[2];
    }
    else if( equali( type, "stop" ) )
    {
        set_pev( ent, pev_classname, "stop" );
        fm_set_rendering( ent, kRenderFxGlowShell, 255, 0, 0, kRenderNormal, 16 );
        
        g_iLocId[1][plr] = ent;
        
        g_fLoc[1][plr][0] = origin[0];
        g_fLoc[1][plr][1] = origin[1];
        g_fLoc[1][plr][2] = origin[2];
    }
    
    return 1;
}

public fnResetLocs( plr ) 
{
    if( g_iLocId[0][plr] )
    {
        engfunc( EngFunc_RemoveEntity, g_iLocId[0][plr] );
        g_iLocId[0][plr] = 0;
    }
    
    if( g_iLocId[1][plr] )
    {
        engfunc( EngFunc_RemoveEntity, g_iLocId[1][plr] );
        g_iLocId[1][plr] = 0;
    }
}

public fwdTouch( touched, plr ) 
{
    if( is_user_alive( plr ) )
    {
        static Float:kztime[33];
        
        if( touched == g_iLocId[0][plr] && touched )
        {
            kztime[plr] = get_gametime( );
            g_bStopwatchStarted[plr] = true;
            
            client_print( plr, print_center, "stopwatch started" );
        }
        else if( touched == g_iLocId[1][plr] && touched && g_bStopwatchStarted[plr] )
        {
            g_bStopwatchStarted[plr] = false;
            
            client_print( plr, print_center, "stopwatch stopped" );
            fnGreenChat( plr, "[Stopwatch] ^x01You have wasted %f seconds of time ", get_gametime( ) - kztime[plr] );
        }
    }
}

public client_connect( plr ) 
    g_bStopwatchStarted[plr] = false;
    
public client_disconnect( plr )
    fnResetLocs( plr );

// by fatalis 
fnGreenChat( plr, const message[], {Float,Sql,Result,_}:... )
{
    static msg[192];
    msg[0] = 0x04;
    
    vformat( msg[1], sizeof msg - 2, message, 3 );
    
    if( plr > 0 && plr <= g_iMaxPlayers )
    {
        message_begin( MSG_ONE, g_iSayText, { 0, 0, 0 }, plr );
        write_byte( plr );
        write_string( msg );
        message_end( );
    }
    else if( plr == 0 )
    {
        for( new i = 1; i <= g_iMaxPlayers; i++ )
        {
            if( !is_user_connected( i ) )
                continue;
                
            message_begin( MSG_ONE, g_iSayText, { 0, 0, 0 }, i );
            write_byte( i );
            write_string( msg );
            message_end( );
        }
    }
    
    return 1;
}
Best regards,
MaNaReaver.
__________________
MaNaReaver is offline