This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Detecting a button press (not a key press)


Post New Thread Reply   
 
Thread Tools Display Modes
MALON
Member
Join Date: Jan 2008
Old 09-08-2008 , 15:59   Re: Detecting a button press (not a key press)
Reply With Quote #11

WOW so many helpful posts! Thank you guys so much thus far!

I'm going to try danielkza's method and see how that works, because that seems to be what I mostly only need to do.

I'll let you guys know how it goes.

--MALON
MALON is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 09-08-2008 , 16:49   Re: Detecting a button press (not a key press)
Reply With Quote #12

I think my method uses less cpu but use the one you want
If you don't need to catch buttons in multiple plugins you can use this light version :

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

enum BUTTONS {
    
START,
    
STOP
}

new 
g_iButtons[BUTTONS]

new 
g_iSpawnForwardId
new g_iMaxPlayers

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_iMaxPlayers get_maxplayers()
}

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

    if( 
iEnt == g_iButtons[START] )
    {
        
// player (id) has pushed the start button
    
}
    else if( 
iEnt == g_iButtons[STOP] )
    {
        
// player (id) has pushed the stop button
    
}
    return 
HAM_IGNORED

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 09-08-2008 , 17:34   Re: Detecting a button press (not a key press)
Reply With Quote #13

Maybe the best is to use a mix of both methods. Instead of looking up a string table,just use general searchs like a did. Also,instead of detecting a single button, use a CellArray to allow unlimited ones. We never know what KZ mappers will do next.
danielkza is offline
MALON
Member
Join Date: Jan 2008
Old 09-08-2008 , 19:54   Re: Detecting a button press (not a key press)
Reply With Quote #14

Wow, there's so many choices. I'm leaning towards going with the least CPU intensive at this point though, because my plugin is over 5,000 lines and uses an extremely horrible method of implementing alternate player information (what you see on your HUD when you aim at them) that creates a new set_task for every player that enters the server and never removes any of those created (until map change of course). I have about when 10 players are on, I have at least 15 set_tasks running probably more if someone dropped and someone else joing, thus creating a new one.

I don't see an impact in game play, so I keep forgetting to change it and fix it up.

Anyway, I stray from the point. I am going to try conorr's method because it does seem the least resource intensive, my goal at this point.

Thanks guys (if anyone has anyhting else they want to add, I would love to read it)

--MALON
MALON is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 09-09-2008 , 13:11   Re: Detecting a button press (not a key press)
Reply With Quote #15

Quote:
Originally Posted by MALON View Post
Wow, there's so many choices. I'm leaning towards going with the least CPU intensive at this point though, because my plugin is over 5,000 lines and uses an extremely horrible method of implementing alternate player information (what you see on your HUD when you aim at them) that creates a new set_task for every player that enters the server and never removes any of those created (until map change of course). I have about when 10 players are on, I have at least 15 set_tasks running probably more if someone dropped and someone else joing, thus creating a new one.

I don't see an impact in game play, so I keep forgetting to change it and fix it up.

Anyway, I stray from the point. I am going to try conorr's method because it does seem the least resource intensive, my goal at this point.

Thanks guys (if anyone has anyhting else they want to add, I would love to read it)

--MALON
Just a suggestion, hooking PreThink,or at least using a single task for all players will be a lot less CPU-intensive than your current method. And the CPU difference between both methods is lower than negligible, if not inexistant. The use forward will only be called for the func_button,and this doesn't happen a lot. Also,the simple string comparisons are pretty fast. Hooking the spawn event,which is called much more times during map start will use a lot of CPU time during the map start. But it's up to you.
danielkza is offline
caffeine
Member
Join Date: Apr 2008
Old 10-25-2008 , 03:11   a simple ham_use button!
Reply With Quote #16

thx all! there is a simple ham_use button below:

hamButton.sma
Code:
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
public plugin_init() {
 RegisterHam(Ham_Use, "func_button", "hamUse")
}
public hamUse(ent,id) {
 new szTarget[32]
 pev(ent, pev_target, szTarget, 31)
 if ( equal(szTarget, "counter_start") || equal(szTarget, "clockstartbutton") || equal(szTarget, "firsttimerelay") || equal(szTarget, "gogogo")) {
  client_print(id, print_chat, "Start button!")
 }
 if ( equal(szTarget, "counter_off") || equal(szTarget, "clockstopbutton") || equal(szTarget, "clockstop") || equal(szTarget, "stop_counter")) {
  client_print(id, print_chat, "Stop button!")
 }
}
Attached Files
File Type: sma Get Plugin or Get Source (hamButton.sma - 397 views - 633 Bytes)

Last edited by caffeine; 10-25-2008 at 03:25.
caffeine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 10-25-2008 , 05:41   Re: Detecting a button press (not a key press)
Reply With Quote #17

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    0.5

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

new 
g_iMaxPlayers

new g_iButtonForwardId
new g_iReturn

find_buttons
()
{
    new 
iTemp
        
iEnt engfunc(EngFunc_FindEntityByString0"classname""func_button"),
        
szTarget[32],
        
i

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

    while( 
iEnt && (!g_iButtons[START] || !g_iButtons[STOP]))
    {
        
iTemp engfunc(EngFunc_FindEntityByStringiEnt"classname""func_button")

        
pev(iEntpev_targetszTarget31)
        if(!
g_iButtons[START])
        {
            for( 
i=i<sizeof(counter_start) ; i++ )
            {
                if( !
g_iButtons[START] && equal(szTargetcounter_start[i] ) )
                {
                    
g_iButtons[START] = iEnt
                
}
            }
        }
        if(!
g_iButtons[STOP])
        {
            for( 
i=i<sizeof(counter_stop) ; i++ )
            {
                if( !
g_iButtons[STOP] && equal(szTargetcounter_stop[i] ) )
                {
                    
g_iButtons[STOP] = iEnt
                
}
            }
        }

        
iEnt iTemp
    
}
    if( 
g_iButtons[START] && g_iButtons[STOP] )
    {
        return 
1
    
}

    
iEnt engfunc(EngFunc_FindEntityByString0"classname""func_button")
    while( 
iEnt && (!g_iButtons[START] || !g_iButtons[STOP]))
    {
        
iTemp engfunc(EngFunc_FindEntityByStringiEnt"classname""func_button")

        
pev(iEntpev_targetnameszTarget31)
        if(!
g_iButtons[START])
        {
            for( 
i=i<sizeof(counter_start) ; i++ )
            {
                if( !
g_iButtons[START] && equal(szTargetcounter_start[i] ) )
                {
                    
g_iButtons[START] = iEnt
                
}
            }
        }
        if(!
g_iButtons[STOP])
        {
            for( 
i=i<sizeof(counter_stop) ; i++ )
            {
                if( !
g_iButtons[STOP] && equal(szTargetcounter_stop[i] ) )
                {
                    
g_iButtons[STOP] = iEnt
                
}
            }
        }

        
iEnt iTemp
    
}
    if( 
g_iButtons[START] && g_iButtons[STOP] )
    {
        return 
2
    
}

    return 
0
}

public 
plugin_init()
{
    switch( 
find_buttons() )
    {
        case 
1:
        {
            
register_pluginPLUGINVERSIONAUTHOR )
            
RegisterHam(Ham_Use"func_button""Ham_Use_button")
        }
        case 
2:
        {
            
register_pluginPLUGINVERSIONAUTHOR )
            
RegisterHam(Ham_Touch"func_button""Ham_Touch_button")
        }
        default:
        {
            
register_pluginPLUGIN"No Buttons Founded"AUTHOR )
            
pause("ad")
            return
        }
    }
    
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
}

public 
Ham_Touch_button(iEntid)
{
    if(!(
<= id <= g_iMaxPlayers))
        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

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:35.


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