AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   CountDown... (https://forums.alliedmods.net/showthread.php?t=184393)

kramesa 05-04-2012 17:41

CountDown...
 
Hi all, I make this CountDown:

Code:
#include <amxmodx> #include <amxmisc> new Time = 15; new DefaultValue; public plugin_init()     register_clcmd("say /count", "CountDown"); public CountDown() {     if(Time == 0) {         Time = DefaultValue;         DefaultValue = 0;         return;     }         Time--;     DefaultValue++;     client_print(0, print_chat, "CountDown: %d", Time);     set_task(1.0, "CountDown"); }

It works perfectly, but, have other way to make this? More optimized?

Backstabnoob 05-04-2012 18:27

Re: CountDown...
 
PHP Code:

#include <amxmodx>
#include <amxmisc>

new Time 15;
new 
DefaultValue;

public 
plugin_init()
    
register_clcmd("say /count""CountDown");

public 
CountDown() 
{
    
    
Time 15
    set_task
1.0"_t_Countdown"999__"a"15 )
}

public 
_t_Countdowntaskid )
{
    
DefaultValue++
    
Time--
    
    if( 
Time == 
    {
        
Time DefaultValue
        DefaultValue 
0
        
        
return
    }
    
    
client_print(0print_chat"CountDown: %d"Time)


A little more optimized.
No clue what DefaultValue does do though.

Bilal Pro 05-04-2012 19:21

Re: CountDown...
 
I hope this helps you.

PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>

#define PLUGIN "Count down timer"
#define VERSION "1.0"
#define AUTHOR "Bilal"

#define TASKID 1996

new Timer

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("say /cd""CmdMenu")
}

public 
CmdMenu(id)
{
    new 
menu menu_create("\rCountdown Menu!""MainMenuHandler"0)
    
    
menu_additem(menu"Timer \d#5""1")
    
menu_additem(menu"Timer \d#10""2")
    
menu_additem(menu"Timer \d#15""3")
    
menu_additem(menu"Timer \d#60""4")
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL)
    
menu_display(idmenu0)
}

public 
MainMenuHandler(idmenuitem)
{
    if (
item == MENU_EXIT)
    {
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED
    
}
    
    new 
data[6], name[64], accescallback
    menu_item_getinfo
(menuitemaccesdatacharsmax(data), namecharsmax(name), callback)
    new 
key str_to_num(data)
    
    switch (
key)
    {
        case 
1Timer 5
            
case 2Timer 10
            
case 3Timer 15
            
case 4Timer 60
        
}
    
Countdown(id)
    return 
PLUGIN_HANDLED
}

public 
Countdown(id)
{
    if(
task_exists(TASKID))
        
remove_task(TASKID)

    
set_task(1.0"Countdown2"TASKID__"a"Timer)
}

public 
Countdown2(id
{
    if (
Timer == 1
    {
        
set_hudmessage(02550, -1.00.4001.01.00.11.02)
        
show_hudmessage(0"Time is up!"
    }
    else 
    {
        
set_hudmessage(02550, -1.00.3001.01.00.11.01)
        
show_hudmessage(0"Timer: %i"Timer--)
    }
    return 
PLUGIN_CONTINUE



ConnorMcLeod 05-04-2012 19:46

Re: CountDown...
 
Usage example for a 15 seconds countdown :

MakeCountDown(15.0)

PHP Code:

#include <engine>
#include <fakemeta>

new g_entCountDownFloat:g_flFreqFloat:g_flTimeleft

MakeCountDown
Float;flTimeleft Float:flFrequency 1.0 )
{
    if( !
g_entCountDown )
    {
        
g_entCountDown create_entity("info_target")
        new const 
szClass[] = "countdown"
        
register_think(szClass"CountDown")
        
set_pev(g_entCountDownpev_classnameszClass)
    }
    
g_flTimeLeft flTimeleft
    g_flFreq 
flFrequency
    set_pev
(g_entCountDownpev_nextthinkget_gametime())
    
call_think(g_entCountDown)
}

public 
CountDowniEntity )
{
    if( 
iEntity != g_entCountDown )
    {
        return
    }

    
// Do stuff here

    // client_print(0, print_center, "Time Left Before Awesome Thing is %.0f sec", g_flTimeleft)
    

    // end of stuff, update timers
    
set_pev(g_entCountDownpev_nextthinkget_gametime() - g_flFreq)
    
g_flTimeleft -= g_flFreq



kramesa 05-04-2012 21:57

Re: CountDown...
 
Quote:

Originally Posted by ConnorMcLeod (Post 1702292)
Usage example for a 15 seconds countdown :

MakeCountDown(15.0)

PHP Code:

#include <engine>
#include <fakemeta>

new g_entCountDownFloat:g_flFreqFloat:g_flTimeleft

MakeCountDown
Float;flTimeleft Float:flFrequency 1.0 )
{
    if( !
g_entCountDown )
    {
        
g_entCountDown create_entity("info_target")
        new const 
szClass[] = "countdown"
        
register_think(szClass"CountDown")
        
set_pev(g_entCountDownpev_classnameszClass)
    }
    
g_flTimeLeft flTimeleft
    g_flFreq 
flFrequency
    set_pev
(g_entCountDownpev_nextthinkget_gametime())
    
call_think(g_entCountDown)
}

public 
CountDowniEntity )
{
    if( 
iEntity != g_entCountDown )
    {
        return
    }

    
// Do stuff here

    // client_print(0, print_center, "Time Left Before Awesome Thing is %.0f sec", g_flTimeleft)
    

    // end of stuff, update timers
    
set_pev(g_entCountDownpev_nextthinkget_gametime() - g_flFreq)
    
g_flTimeleft -= g_flFreq



Ok, thank you.

Backstabnoob 05-05-2012 04:47

Re: CountDown...
 
Shouldn't you remove the entity when the task is done or create one and use it all the time?

ConnorMcLeod 05-05-2012 09:50

Re: CountDown...
 
That is what is done :

Use the same entity all the time, it just creates it at the first time it is needed.

Backstabnoob 05-05-2012 10:56

Re: CountDown...
 
Oh I see, missed the check for g_entCountDown. My bad :X


All times are GMT -4. The time now is 00:23.

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