AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Detect firing for x seconds? (https://forums.alliedmods.net/showthread.php?t=307494)

sekac 05-12-2018 13:37

Detect firing for x seconds?
 
How can I call a function after a user has been firing from a gun for more than 2 seconds? The timer should reset when the player lets go of mouse 1.

fysiks 05-12-2018 15:15

Re: Detect firing for x seconds?
 
Probably need to detect the IN_ATTACK button and calculate how long it is active. If more than 2 seconds, call your function. There are several threads around here that show how to check when IN_ATTACK is pressed and released.

sekac 05-12-2018 15:19

Re: Detect firing for x seconds?
 
Quote:

Originally Posted by fysiks (Post 2591895)
Probably need to detect the IN_ATTACK button and calculate how long it is active. If more than 2 seconds, call your function. There are several threads around here that show how to check when IN_ATTACK is pressed and released.

I think I will set a task when a player starts holding IN_ATTACK and remove the task when he releases it. That way I don't have to calculate how long the user has been holding the attack key.

klippy 05-12-2018 15:23

Re: Detect firing for x seconds?
 
Do you want it to continually call your function after the user has been holding the key for longer than 2 seconds, or only call it once, then repeat the whole process after the user releases the button and starts holding it again?

Natsheh 05-12-2018 15:49

Re: Detect firing for x seconds?
 
Hook cmdstart check if user pressing attack and its old button, then execute the function you wanted depends on the gametime.

sekac 05-12-2018 16:22

Re: Detect firing for x seconds?
 
Quote:

Originally Posted by KliPPy (Post 2591899)
Do you want it to continually call your function after the user has been holding the key for longer than 2 seconds, or only call it once, then repeat the whole process after the user releases the button and starts holding it again?

Just once. Solved.

klippy 05-12-2018 17:43

Re: Detect firing for x seconds?
 
Quote:

Originally Posted by sekac (Post 2591911)
Just once. Solved.

In that case you should post the solution, for the benefit of the community.

sekac 05-12-2018 18:21

Re: Detect firing for x seconds?
 
Quote:

Originally Posted by KliPPy (Post 2591921)
In that case you should post the solution, for the benefit of the community.

Sure!

PHP Code:

new buttons pev(idpev_button);
new 
oldbuttons pev(idpev_oldbuttons);

if(
buttons IN_ATTACK)
{
    
// Executed when you start holding IN_ATTACK
    
set_task(2.0"function"id); // Here you can set the amount of seconds you need to hold IN_ATTACK
}

if( 
oldbuttons IN_ATTACK && !( buttons IN_ATTACK ) )
{
    
// Executed when you release IN_ATTACK
    
remove_task(id);
    
boolean[id] = false;
}

public function(
id) {
    
boolean[id] = true// This boolean will only be true while holding IN_ATTACK for 2 seconds
}

// Then you can use that boolean in your code. 

Here is what I have done with it: Negev from CS:GO

Natsheh 05-12-2018 18:24

Re: Detect firing for x seconds?
 
There is abetter way you can use and what forward you used to check the attack?

sekac 05-12-2018 18:35

Re: Detect firing for x seconds?
 
Quote:

Originally Posted by Natsheh (Post 2591931)
There is abetter way you can use and what forward you used to check the attack?

There probably is but I found this very simple to use. I used FM_PlayerPreThink, probably not the best either.


All times are GMT -4. The time now is 04:37.

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