PDA

View Full Version : How can add a cooldown without an end function?


Isaacd3d
10-18-2011, 09:26
How can i add a cooldown without an end function

I try a code but when using the clcmd in many times the cooldown will not work.

Hunter-Digital
10-18-2011, 18:04
With a global variable that holds the gametime when the thing was last used and in your command you add the cooldown delay to that and check if it's smaller than the current gametime.

Isaacd3d
10-18-2011, 22:20
With a global variable that holds the gametime when the thing was last used and in your command you add the cooldown delay to that and check if it's smaller than the current gametime.

Do you mean like this:

if (get_gametime() - g_cooldown[id] < get_pcvar_float(cvar_cooldown)
{
return PLUGIN_HANDLED
}


But dint work on me. when i use the command still no cooldown effects
:(

FoxueR
10-18-2011, 22:33
Show your code.

Isaacd3d
10-19-2011, 06:46
Show your code.

For now i'm not creating my next plugin. because i need a cooldown
without an end function for it :(

help me please!

DarkGod
10-19-2011, 06:49
Show what you tried...

Isaacd3d
10-19-2011, 09:55
Show what you tried...


#include <amxmodx>
#include <amxmisc>

new Float:g_cooldown[33]

public plugin_init() {
register_plugin("clcmd", "1.0", "Test")
register_clcmd("text", "clcmd_text")
}

public clcmd_text(id)
{
client_cmd(id, "say print")

if (get_gametime() - g_cooldown[id] > 15.0)
{
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}

Hunter-Digital
10-19-2011, 13:35
And you're watching if the cmd prints "Unknown command" or not ? Because that print there is called regardless of your if() statement because it's before it... return just ends the function, it doesn't cancel everything before it.

You're also not setting the g_cooldown when you're using it.

EDIT: also, I recommend this method because it allows you to use the command at map start, other functions actually start the cooldown when the map starts and you need to wait how much cooldown since the map starts.
Also, if you don't need that variable anywhere else, you can declare it as static in that function only.

Both as examples:
new const Float:g_fCoolDown = 60.0 // expecially if you have large cooldowns like this.

// your func:

static Float:fLastUsed[33]
new Float:fGameTime = get_gametime()

if(fLastUsed[id] < fGameTime)
{
fLastUsed[id] = fGameTime + g_fCoolDown

client_print(id, print_chat, "You did something ! Next use in %.1f seconds", g_fCoolDown)

// use whatever you need to use
}
else
{
client_print(id, print_chat, "Cooldown has %.1f seconds left...", (fLastUsed[id] - fGameTime))
}

Isaacd3d
10-19-2011, 22:20
And you're watching if the cmd prints "Unknown command" or not ? Because that print there is called regardless of your if() statement because it's before it... return just ends the function, it doesn't cancel everything before it.

You're also not setting the g_cooldown when you're using it.

EDIT: also, I recommend this method because it allows you to use the command at map start, other functions actually start the cooldown when the map starts and you need to wait how much cooldown since the map starts.
Also, if you don't need that variable anywhere else, you can declare it as static in that function only.

Both as examples:
new const Float:g_fCoolDown = 60.0 // expecially if you have large cooldowns like this.

// your func:

static Float:fLastUsed[33]
new Float:fGameTime = get_gametime()

if(fLastUsed[id] < fGameTime)
{
fLastUsed[id] = fGameTime + g_fCoolDown

client_print(id, print_chat, "You did something ! Next use in %.1f seconds", g_fCoolDown)

// use whatever you need to use
}
else
{
client_print(id, print_chat, "Cooldown has %.1f seconds left...", (fLastUsed[id] - fGameTime))
}

I dint get the both example:

// your func:

static Float:fLastUsed[33]
new Float:fGameTime = get_gametime()

if(fLastUsed[id] < fGameTime)
{
fLastUsed[id] = fGameTime + g_fCoolDown

client_print(id, print_chat, "You did something ! Next use in %.1f seconds", g_fCoolDown)

// use whatever you need to use
}
else
{
client_print(id, print_chat, "Cooldown has %.1f seconds left...", (fLastUsed[id] - fGameTime))
}

In the quote, should i put it in the clcmd function?

Snaker beatter
10-24-2011, 10:48
I dint get the both example:

In the quote, should i put it in the clcmd function?

Man you dont get it...

or try this


new Float:g_cooldown[33]

public plugin_int()
{
register_plugin("new", "1.0", "test")
register_clcmd("test", "clcmd_test")
}

public clcmd_test(id)
{
if (get_gametime() - g_cooldown[id] < 15.0)
{
return PLUGIN_HANDLED
}

client_cmd(id, "hahah")
g_cooldown[id] = get_gametime()

return PLUGIN_CONTINUE
}

Isaacd3d
10-24-2011, 11:25
Man you dont get it...

or try this


new Float:g_cooldown[33]

public plugin_int()
{
register_plugin("new", "1.0", "test")
register_clcmd("test", "clcmd_test")
}

public clcmd_test(id)
{
if (get_gametime() - g_cooldown[id] < 15.0)
{
return PLUGIN_HANDLED
}

client_cmd(id, "hahah")
g_cooldown[id] = get_gametime()

return PLUGIN_CONTINUE
}


THanks man it works :up: