AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Detecting a button press (not a key press) (https://forums.alliedmods.net/showthread.php?t=77224)

MALON 09-07-2008 20:14

Detecting a button press (not a key press)
 
I know there are other threads out there, but I'm getting a bit overwhelmed with them. I'm not sure which to use.

Basically, what's the proper way to detect a button press? Like an ingame button (specifically a kz button).

Currently I am using an EmitSound forward and when the button makes the normal button sound, it triggers my start function.

Is this the best way? Somehow, hooking a sound forward doesn't seem like the best way. I'd think it should be in a client_prethink perhaps.

If you suggest another method other than using sound, I'd appreciate maybe a little snippet.

Thanks guys.

--MALON

anakin_cstrike 09-07-2008 20:23

Re: Detecting a button press (not a key press)
 
pev(id,pev_button) ?:? IN_*

Edit: Oh god no...with this only 'use' key i think...

stupok 09-07-2008 22:07

Re: Detecting a button press (not a key press)
 
Use Ham Sandwich.

Code:

RegisterHam ( Ham:function, const EntityClass[], const Callback[], Post=0 )
Look for the hamsandwich files in your amxx folder, there's a function like ham_use or something like that.

Emp` 09-07-2008 22:23

Re: Detecting a button press (not a key press)
 
you might be able to register the entity's think (if you know the classname) with register_think(const Classname[], const function[]); (engine)

MALON 09-07-2008 23:28

Re: Detecting a button press (not a key press)
 
I'm checking into the hamsandwich method now. I've never used it, so we'll see how i do.

And yes, I do know the entity's classname (it's always one of 5 classnames because it's a convention) so register_think is seeming really awesome. I don't know if button's think, but they must because they trigger something else in the map that's map related and not plugin related. Boy, I hope i know what the funk i'm talking about and not just speaking out of my arse.

Thanks so far guys!

--MALON

stupok 09-07-2008 23:51

Re: Detecting a button press (not a key press)
 
here's an example from another thread: (it's old, I don't think you need "#include hamsandwich" anymore)

http://forums.alliedmods.net/showthr...hlight=ham_use

Code:

#include <amxmodx>
#include <hamsandwich>

new bool:IAmUsed[33]

public plugin_init() {
    RegisterHam(Ham_Use,"hostage_entity","event_hostage_used",1)
}

public event_hostage_used(hostage, id) {
    if(!IAmUsed[hostage]) {
        IAmUsed[hostage] = true
        client_print(id,print_chat,"Used a hostage")
    }
}


MALON 09-08-2008 00:25

Re: Detecting a button press (not a key press)
 
So this is what I tried:

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>

#define PLUGIN    "New Plugin"
#define AUTHOR    "Unknown"
#define VERSION    "1.0"

new bool:IAmUsed[33]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
RegisterHam(Ham_Use,"counter_start","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"clockstartbutton","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"firsttimerelay","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"gogogo","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"multi_start","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"func_button","event_hostage_used",1)//the entire button class
}

public 
event_hostage_used(hostageid) {
    
client_print(idprint_chat"HI")


I tried it with and without the func_button one, but alas, failure. Sorry I'm so not awesome with HamSandwich.



EDIT
--------------------------

D'OH...I didn't try just plain old func_button. I'm stupid. I didn't read classname and I thought entity name. So once I call the function (in this case event_hostage_used), I have to check what entities are within a sphere of the player? Or can I send the entity that called the function TO the function and then extract the entity name from there?

Thanks again!

--MALON

SchlumPF* 09-08-2008 01:20

Re: Detecting a button press (not a key press)
 
RegisterHam( Ham_Use, "func_button", "hamUseButton_Pre", 0 );
and then inside check whether pev_target is equal to
"counter_start", "clockstartbutton", "firsttimerelay", "multi_start"
"counter_off", "clockstop", "clockstopbutton", "multi_stop"


danielkza 09-08-2008 13:05

Re: Detecting a button press (not a key press)
 
Quote:

Originally Posted by MALON (Post 682871)
So this is what I tried:

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>

#define PLUGIN    "New Plugin"
#define AUTHOR    "Unknown"
#define VERSION    "1.0"

new bool:IAmUsed[33]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
RegisterHam(Ham_Use,"counter_start","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"clockstartbutton","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"firsttimerelay","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"gogogo","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"multi_start","event_hostage_used",1)//possible button name
    
RegisterHam(Ham_Use,"func_button","event_hostage_used",1)//the entire button class
}

public 
event_hostage_used(hostageid) {
    
client_print(idprint_chat"HI")


I tried it with and without the func_button one, but alas, failure. Sorry I'm so not awesome with HamSandwich.



EDIT
--------------------------

D'OH...I didn't try just plain old func_button. I'm stupid. I didn't read classname and I thought entity name. So once I call the function (in this case event_hostage_used), I have to check what entities are within a sphere of the player? Or can I send the entity that called the function TO the function and then extract the entity name from there?

Thanks again!

--MALON

As i can see this is for KZ timer.Luckly for you,I have done this already:
PHP Code:

public plugin_init()
{
    
RegisterHam(Ham_Use"func_button""fwButtonUse"1)
}


public 
fwButtonUse(buttonplayertypeFloat:value)
{
    if( !
get_pcvar_num(cvTimerOn) )
        return 
HAM_IGNORED

    
new szTarget[32]
    
pev(buttonpev_targetszTarget31)

    
// Check if this button is the timer's end/start. Look for the words "counter","clock" and "timer"
    // If they are found,check if it's the start or the end timer
    
if( containi(szTarget"counter") != -|| containi(szTarget"clock") != -|| containi(szTarget"timer") != -)
    {
        if( 
containi(szTarget"start") != -|| containi(szTarget"on") != -)
            
TimerStart(player)
        else if( 
containi(szTarget"stop") != -|| containi(szTarget"off") != -)
            
TimerEnd(player)
    }
    else
    {
        new 
szTargetName[32]
        
pev(buttonpev_targetnameszTargetName31)

        if( 
containi(szTargetName"counter") != -|| containi(szTargetName"clock") != -|| containi(szTargetName"timer") != -)
        {
            if( 
containi(szTargetName"start") != -|| containi(szTargetName"on") != -)
                
TimerStart(player)
            else if( 
containi(szTargetName"stop") != -|| containi(szTargetName"off") != -)
                
TimerEnd(player)
        }
    }

    return 
HAM_IGNORED



ConnorMcLeod 09-08-2008 15:41

Re: Detecting a button press (not a key press)
 
Code:

#if defined _kz_buttons_included
        #endinput
#endif
#define _kz_buttons_included

#pragma reqlib kz_buttons

#if !defined AMXMODX_NOAUTOLOAD
        #pragma loadlib kz_buttons
#endif

#define START        0
#define STOP        1
#define BUTTONS        2

forward client_use_button(index, button)

PHP Code:

/*    Copyright © 2008, ConnorMcLeod

    Kz Buttons is free software;
    you can redistribute it and/or modify it under the terms of the
    GNU General Public License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kz Buttons; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#define PLUGIN "Kz Buttons"
#define AUTHOR "ConnorMcLeod"
#define VERSION "0.0.1"

#define MAX_PLAYERS    32

#define START        0
#define STOP        1
#define BUTTONS    2

#define MIN_DELAY    1.0

new g_iButtons[BUTTONS]
new 
Float:g_flLastUse[MAX_PLAYERS+1][BUTTONS]

new 
g_iSpawnForwardId
new g_iMaxPlayers

new g_iButtonForwardId
new g_iReturn

public plugin_precache()
{
    
register_pluginPLUGINVERSIONAUTHOR )
    
g_iSpawnForwardId register_forward(FM_Spawn"Forward_Spawn")
}

public 
Forward_Spawn(iEnt)
{
    if( !
pev_valid(iEnt) )
        return

    new 
counter_start[][] = {"counter_start""clockstartbutton""firsttimerelay""multi_start"}
    new 
counter_stop[][] = {"counter_off""clockstop""clockstopbutton""multi_stop"}

    new 
szTarget[17]
    
pev(iEntpev_targetszTarget16)

    for(new 
ii<4i++)
    {
        if( 
equal(szTargetcounter_start[i]) )
        {
            
g_iButtons[START] = iEnt
        
}
        else if( 
equal(szTargetcounter_stop[i]) )
        {
            
g_iButtons[STOP] = iEnt
        
}
    }
}

public 
plugin_init()
{
    
unregister_forward(FM_Spawng_iSpawnForwardId)
    if(!
g_iButtons[START] || !g_iButtons[STOP])
    {
        
pause("ad")
        return
    }

    
RegisterHam(Ham_Use"func_button""Ham_Use_button")
    
g_iButtonForwardId CreateMultiForward("client_use_button"ET_IGNOREFP_CELLFP_CELL)
    
g_iMaxPlayers get_maxplayers()
}

public 
plugin_natives()
{
    
register_library("kz_buttons")
}

public 
client_putinserver(id)
{
    
g_flLastUse[id][START] = 0.0
    g_flLastUse
[id][STOP] = 0.0
}

public 
Ham_Use_button(iEntididactivatoruse_typeFloat:flValue)
{
    if( !(
<= id <= g_iMaxPlayers) ||
            
use_type != 2        ||
            
flValue != 1.0        )
    {
        return 
HAM_IGNORED
    
}

    static 
iButton

    
if( iEnt == g_iButtons[START] )
    {
        
iButton START
    
}
    else if( 
iEnt == g_iButtons[STOP] )
    {
        
iButton STOP
    
}
    else
    {
        return 
HAM_IGNORED
    
}

    static 
Float:flTime
    global_get
(glb_timeflTime)

    if( 
g_flLastUse[id][iButton] + MIN_DELAY flTime )
    {
        return 
HAM_IGNORED
    
}

    
g_flLastUse[id][iButton] = flTime

    ExecuteForward
(g_iButtonForwardIdg_iReturnidiButton)

    return 
HAM_IGNORED



Then, you can make a plugin like this :
PHP Code:

#include <amxmodx>

#include "kz_buttons.inc"

#define PLUGIN "Kz Buttons Test"
#define AUTHOR "ConnorMcLeod"
#define VERSION "0.0.1"

public plugin_init()
{
    
register_pluginPLUGINVERSIONAUTHOR )
}

public 
client_use_button(idiButton)
{
    
client_print(idprint_chat"You have just pressed the %s button"iButton "stop" "start")


or

PHP Code:

#include <amxmodx>
#include <fakemeta>

#include "kz_buttons.inc"

#define PLUGIN "Kz Buttons Test"
#define AUTHOR "ConnorMcLeod"
#define VERSION "0.0.1"

#define MAX_PLAYERS    32

new g_iMaxPlayers

new Float:g_flStartTime[MAX_PLAYERS+1]

public 
plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_forward(FM_EmitSound"EmitSound")
    
g_iMaxPlayers get_maxplayers()
}

public 
client_putinserver(id)
{
    
g_flStartTime[id] = 0.0
}

public 
client_use_button(idiButton)
{
    if( 
iButton == START )
    {
        
g_flStartTime[id] = get_gametime()
        
client_print(idprint_chat"Timer started !!!")
    }
    else if( 
g_flStartTime[id] )
    {
        new 
szName[32]
        
get_user_name(idszName31)


        new 
iTime floatround(get_gametime() - g_flStartTime[id])
        if( 
iTime 3600 )
        {
            new 
iTime2 iTime%3600
            client_print
(idprint_center"%d:%02d:%02d"iTime/3600iTime2/60iTime2%60)
        }
        else
        {
            
client_print(idprint_center"%d:%02d"iTime/60iTime%60)
        }
        
g_flStartTime[id] = 0.0
    
}
}

public 
EmitSound(idosefszSound[])
{
    if( !(
<= id <= g_iMaxPlayers) )
        return 
FMRES_IGNORED

    
//common/wpn_denyselect.wav
    
if( szSound[0] != 'c' || szSound[7] != 'w' || szSound[11] != 'd')
        return 
FMRES_IGNORED

    
if(g_flStartTime[id])
    {        
        new 
iTime floatround(get_gametime() - g_flStartTime[id])
        if( 
iTime 3600 )
        {
            static 
iTime2
            iTime2 
iTime%3600
            client_print
(idprint_center"%d:%02d:%02d"iTime/3600iTime2/60iTime2%60)
        }
        else
        {
            
client_print(idprint_center"%d:%02d"iTime/60iTime%60)
        }

        return 
FMRES_HANDLED
    
}
    return 
FMRES_IGNORED




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

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