AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved [Help] Simple Countdown error (https://forums.alliedmods.net/showthread.php?t=304053)

Hey 01-02-2018 08:47

[Help] Simple Countdown error
 
Hi Happy new year everyone,

I need some help with this code which is a simple countdown from 10 to 0 the problem is when i set the countdown variable to 10 at round start it doesn't get set to 10 i suppose because the countdown throws some negative numbers like if the variable number was set to 0 or -1, but when i call the countdown by a clcmd it counts without errors the code is down below, i don't have any idea why so i came here for help here is the code:

PHP Code:

#include <amxmodx>
#include <amxmisc>

new g_count;

new const 
countdown_sounds[][] = 
{
    
"BR/one.wav",
    
"BR/two.wav",
    
"BR/three.wav",
    
"BR/four.wav",
    
"BR/five.wav",
    
"BR/six.wav",
    
"BR/seven.wav",
    
"BR/eight.wav",
    
"BR/nine.wav",
    
"BR/ten.wav"
}
public 
plugin_init()
{
    
register_logevent("logevent_start_rnd"2"1=Round_Start");
    
register_clcmd("say /call""count");
}
public 
plugin_precache()
{
    for(new 
0sizeof(countdown_sounds); i++)
    {
        
precache_sound(countdown_sounds[i]);
    }
}
public 
count(id)
{
    
g_count 10;
    
countdown_start(id);
}
public 
logevent_rnd_start()
{
    new 
players[32], idpnum;
    
get_players(playerspnum);
    
    for(new 
0pnumi++)
    {
        
id players[i];
        if(!
is_user_alive(id))
            return 
PLUGIN_HANDLED;
        
        
g_count 10;
        
//strip_user_weapons(id);
        
set_task(5.0"countdown_start"id);
        
//set_task(1.0, "show_info", TASK_SHOW_HUD + id);
    
}
    return 
PLUGIN_HANDLED;
}
public 
countdown_start(id)
{
    
g_count -= 1;
    
client_print(0print_center"%s Battle Starts in %i"TAGg_count);
    
client_cmd(0"spk ^"%s^""countdown_sounds[g_count]);
    
    if(
g_count 0)
    {
        
set_task(1.0"countdown_start"id);
    }
    else
    {
        
set_task(2.0"Battle_start"id);
    }


BTW: there is one error that is index out of bound which happens because of the count returning a negative number and the countdown_sounds variable has no negative number so it throws the error but that does happen in the clcmd /call.

Thanks in advance.

zmd94 01-02-2018 11:40

Re: [Help] Simple Countdown error
 
Review below code:
HTML Code:

#include <amxmodx>
#include <amxmisc>

#define TASK_COUNT 2018

new g_iCount

new const CountdownSounds[][] =
{
        "BR/one.wav",
        "BR/two.wav",
        "BR/three.wav",
        "BR/four.wav",
        "BR/five.wav",
        "BR/six.wav",
        "BR/seven.wav",
        "BR/eight.wav",
        "BR/nine.wav",
        "BR/ten.wav"
}

public plugin_init()
{
        // Use below event instead register_logevent("Logevent_Start", 2, "1=Round_Start");
        register_event("HLTV", "Event_NewRound", "a", "1=0", "2=0")
}

public plugin_precache()
{
        for(new i = 0; i < sizeof(CountdownSounds); i++)
        {
                precache_sound(CountdownSounds[i]);
        }
}

public Event_NewRound()
{
        // Set hardcode countdown global variable
        g_iCount = 10
       
        // Call countdown function every 1s
        set_task(1.0, "CountdownFunc", TASK_COUNT, _, _, "b")
}

public CountdownFunc()
{
        // If g_iCount equal to 0
        if(g_iCount >
0)
        {
                client_print(0, print_center, "Battle starts in %d", g_iCount)
                client_cmd(0, "spk ^"%s^"", CountdownSounds[g_iCount]);
        }
        else
        {
                // Remove CountdownFunc task
                remove_task(TASK_COUNT)
               
                // Start other function below
        }
       
        // Deduct countdown variable
        g_iCount--
}


Hey 01-02-2018 15:29

Re: [Help] Simple Countdown error
 
thanks for the reply tho but now the count just prints 10 and plays the ten.wav file then stops.

i think the problem is that the countdown function gets called one time which is from round start so it runs only that one time and prints the 10 and plays the ten.wav file then stops because it doesn't get called again to reduce the g_iCount value... .

now the biggest Mystery is that the my code works when i run the clcmd /call :/, i just need to know why g_count doesn't get the value 10 when rounds start and instead it returns a negative number, i tried in player spawn with Hamsandwich and i tried to do it in round end too but same issue.

OciXCrom 01-02-2018 16:30

Re: [Help] Simple Countdown error
 
You forgot to set the "b" flag so the task will repeat. Also, g_count should be reduced in the end of the function, not the beginning.

Quote:

// Use %d instead of %i
Why???

Hey 01-02-2018 17:41

Re: [Help] Simple Countdown error
 
same result prints random number negative and positive and just keeps repeating the task cuz the if statement doesn't stop the task ugh.... .

anyone got any idea why the value doesn't get set to the variable at round start/end & and player spawn too but it does only when i set it by that clcmd.

so i tried to make a solution by executing server_cmd("say /call"); it worked by calling that function in the all the rounds but except the first round it did print random negative and positive numbers :| i tried to do sv_restart but every first round keeps happening, such a stupid error :>.

zmd94 01-04-2018 09:46

Re: [Help] Simple Countdown error
 
Fixed:

https://forums.alliedmods.net/showpo...78&postcount=2


All times are GMT -4. The time now is 14:54.

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