Raised This Month: $12 Target: $400
 3% 

[TF2] Help with timer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ShadowMarioBR
Member
Join Date: Feb 2018
Old 06-24-2018 , 13:36   [TF2] Help with timer
Reply With Quote #1

This timer should give +1 primary ammo every 1.5 seconds, but apparently it's not working correctly

Quote:
public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon)
{
!IsValidClient(client) || !IsPlayerAlive(client))
{
return Plugin_Continue;
}

if(medigunEquipped[client])
{
if (buttons & IN_ATTACK == IN_ATTACK || buttons & IN_ATTACK2 == IN_ATTACK2)
{
g_hAmmoTimer = CreateTimer(1.5, Timer_GiveAmmo, GetClientUserId(client), TIMER_REPEAT);
}
}

return Plugin_Continue;
}

public Action Timer_GiveAmmo(Handle timer, int client)
{
PrintToChatAll("Timer working");
if(IsValidClient(client) && IsPlayerAlive(client))
{
new primary = GetPlayerWeaponSlot(client, TFWeaponSlot_Primary);
new ammotype = GetEntProp(primary, Prop_Send, "m_iPrimaryAmmoType");
GivePlayerAmmo(client, 1, ammotype, true);
g_hAmmoTimer = INVALID_HANDLE;
}

if (g_hAmmoTimer != INVALID_HANDLE)
{
CloseHandle(g_hAmmoTimer);
g_hAmmoTimer = INVALID_HANDLE;
}

return Plugin_Handled;
}

Last edited by ShadowMarioBR; 06-24-2018 at 13:37. Reason: Accidentally pressed Enter before finishing the thread
ShadowMarioBR is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-24-2018 , 21:09   Re: [TF2] Help with timer
Reply With Quote #2

Quote:
Originally Posted by ShadowMarioBR View Post
This timer should give +1 primary ammo every 1.5 seconds, but apparently it's not working correctly
Let's see. OnPlayerRunCmd is called every 1 / 100 seconds

You create a repetitive timer 100 times in a second.

The player will start getting 6000 ammo every 1.5 seconds after a minute.

After an hour, the player will get 21600000 ammo every 1.5 seconds.

Just do this:

[code]
#include <sourcemod>

new hTimerAmmo = INVALID_HANDLE;

public OnPluginStart()
{
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
}

public OnMapStart()
{
hTimerAmmo = INVALID_HANDLE;
}

public Event_RoundStart(Handle:hEvent, const String:Name[], bool:dontBroadcast)
{
if(hTimerAmmo != INVALID_HANDLE)
{
CloseHandle(hTimerAmmo);
hTimerAmmo = INVALID_HANDLE;
}
hTimerAmmo = CreateTimer(1.5, GiveAllAmmo, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public Action:GiveAllAmmo(Handle:hTimer)
{
// Give ammo stuff here, will be executed every 1.5 seconds.
return Plugin_Continue;
}
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 06-24-2018 at 21:17. Reason: fo
eyal282 is offline
ShadowMarioBR
Member
Join Date: Feb 2018
Old 06-25-2018 , 13:25   Re: [TF2] Help with timer
Reply With Quote #3

Quote:
Originally Posted by eyal282 View Post
Just do this:

[code]
#include <sourcemod>

new hTimerAmmo = INVALID_HANDLE;

public OnPluginStart()
{
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
}

public OnMapStart()
{
hTimerAmmo = INVALID_HANDLE;
}

public Event_RoundStart(Handle:hEvent, const String:Name[], bool:dontBroadcast)
{
if(hTimerAmmo != INVALID_HANDLE)
{
CloseHandle(hTimerAmmo);
hTimerAmmo = INVALID_HANDLE;
}
hTimerAmmo = CreateTimer(1.5, GiveAllAmmo, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public Action:GiveAllAmmo(Handle:hTimer)
{
// Give ammo stuff here, will be executed every 1.5 seconds.
return Plugin_Continue;
}
I can't do this, it needs to detect if i'm pressing a specific button, and i don't want to it be executed when round start.
ShadowMarioBR is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 06-26-2018 , 05:48   Re: [TF2] Help with timer
Reply With Quote #4

You could just use AddCommandListener, also it didn't work with GivePlayerAmmo for some reason :/
Here's a sample plugin:

PHP Code:
#include <sdktools>

#pragma semicolon 1

public void OnMapStart()
{
    
AddCommandListener(Cmd_Callback"+attack"); /* Mouse1 */
    
AddCommandListener(Cmd_Callback"+attack2"); /* Mouse2 I think, not sure */
}

public 
Action Cmd_Callback(int iClient, const char[] sCommandint iArgs)
{
    
CreateTimer(1.5Timer_Ammo_TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
}

public 
Action Timer_Ammo(Handle hTimer)
{    
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            if (
IsPlayerAlive(i))
            {
                
int iAmmo GetEntProp(iProp_Data"m_iAmmo"); /* Get ammo count */
                
                
SetEntProp(iProp_Sned"m_iClip1"0); /* Remove ammo */
                
                
SetEntProp(iProp_Send"m_iClip1"iAmmo 1); /* Give the player it's original ammo, but + 1 */
            
}
        }
    }
    
    return 
Plugin_Continue;


Last edited by mug1wara; 06-26-2018 at 05:51.
mug1wara is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 06-26-2018 , 11:50   Re: [TF2] Help with timer
Reply With Quote #5

PHP Code:

public void OnPluginStart()
{
    for(
int i 1<= MaxClientsi++)
    {
        if(
IsClientInGame(i) && IsPlayerAlive(i))
        {
            if(...) 
// check if the client needs ammo and maybe check if that client is not in +attack with GetClientButtons()
            
{
                
GiveAmmo(i); // your function for ammo
            
}
        }
    }

use this logic.
Ilusion9 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 06-26-2018 , 12:33   Re: [TF2] Help with timer
Reply With Quote #6

Quote:
Originally Posted by mug1wara View Post
You could just use AddCommandListener, also it didn't work with GivePlayerAmmo for some reason :/
Here's a sample plugin:

PHP Code:
#include <sdktools>

#pragma semicolon 1

public void OnMapStart()
{
    
AddCommandListener(Cmd_Callback"+attack"); /* Mouse1 */
    
AddCommandListener(Cmd_Callback"+attack2"); /* Mouse2 I think, not sure */
}

public 
Action Cmd_Callback(int iClient, const char[] sCommandint iArgs)
{
    
CreateTimer(1.5Timer_Ammo_TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
}

public 
Action Timer_Ammo(Handle hTimer)
{    
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            if (
IsPlayerAlive(i))
            {
                
int iAmmo GetEntProp(iProp_Data"m_iAmmo"); /* Get ammo count */
                
                
SetEntProp(iProp_Sned"m_iClip1"0); /* Remove ammo */
                
                
SetEntProp(iProp_Send"m_iClip1"iAmmo 1); /* Give the player it's original ammo, but + 1 */
            
}
        }
    }
    
    return 
Plugin_Continue;

+attack and similar aren't commands, they are "buttons" and cannot be intercepted via command listeners.

Please do not post false or misleading information.

Quote:
Originally Posted by Ilusion9 View Post
PHP Code:

public void OnPluginStart()
{
    for(
int i 1<= MaxClientsi++)
    {
        if(
IsClientInGame(i) && IsPlayerAlive(i))
        {
            if(...) 
// check if the client needs ammo and maybe check if that client is not in +attack with GetClientButtons()
            
{
                
GiveAmmo(i); // your function for ammo
            
}
        }
    }

use this logic.
That would only happen when the plugin starts, which is usually before there are even any players in the server. This code does nothing.
__________________

Last edited by ddhoward; 06-26-2018 at 12:34.
ddhoward is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 06-26-2018 , 16:55   Re: [TF2] Help with timer
Reply With Quote #7

+attack + AddCommandListener make's it to an "event", it sure registers it but not the actual key pressed, thanks for pointing it out. Think this should do it slightly better:

PHP Code:
#include <sdktools>

#pragma semicolon 1

public void OnMapStart()
{
    
CreateTimer(1.5Timer_Recheck_TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
}

public 
Action Timer_Recheck(Handle hTimer)
{
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            if (
IsPlayerAlive(i))
            {
                if (
GetClientButtons(i) == IN_USE)
                {
                    
GivePlayerAmmo(i);
                }
            }
        }
    }
    
    return 
Plugin_Continue;

mug1wara is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 06-28-2018 , 09:43   Re: [TF2] Help with timer
Reply With Quote #8

Quote:
Originally Posted by ddhoward View Post
That would only happen when the plugin starts, which is usually before there are even any players in the server. This code does nothing.
my bad, i forgot to create the timer in plugin start and call that function in timer.
i think he got the idea.
Ilusion9 is offline
Reply


Thread Tools
Display Modes

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 16:33.


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