AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [TF2] Help with timer (https://forums.alliedmods.net/showthread.php?t=308537)

ShadowMarioBR 06-24-2018 13:36

[TF2] Help with timer
 
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;
}

eyal282 06-24-2018 21:09

Re: [TF2] Help with timer
 
Quote:

Originally Posted by ShadowMarioBR (Post 2599101)
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;
}

ShadowMarioBR 06-25-2018 13:25

Re: [TF2] Help with timer
 
Quote:

Originally Posted by eyal282 (Post 2599158)
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.

mug1wara 06-26-2018 05:48

Re: [TF2] Help with timer
 
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;



Ilusion9 06-26-2018 11:50

Re: [TF2] Help with timer
 
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.

ddhoward 06-26-2018 12:33

Re: [TF2] Help with timer
 
Quote:

Originally Posted by mug1wara (Post 2599320)
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 (Post 2599391)
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.

mug1wara 06-26-2018 16:55

Re: [TF2] Help with timer
 
+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;



Ilusion9 06-28-2018 09:43

Re: [TF2] Help with timer
 
Quote:

Originally Posted by ddhoward (Post 2599396)
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.


All times are GMT -4. The time now is 10:25.

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