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=190170)

GhostMan 07-15-2012 14:02

Countdown
 
PHP Code:

new time
time 
30
set_task
1.0"something"TASK_SOMETHING__"a"time )

public 
something()
{
    
time--
    
    if(
time >= 0)
    {
        
set_hudmessage(02550, -1.00.200.01.00.10.14)
        
show_hudmessage(0"Seconds left: %i"time)
    }
    
    else
    {
        
set_hudmessage(02550, -1.00.200.05.00.10.14)
        
show_hudmessage(0"TIME IS OUT!")
        
        if(
task_exists(TASK_SOMETHING))
            
remove_task(TASK_SOMETHING)
    }


After
PHP Code:

time >= 

is false is variable "time" still getting lower and lower values?

Liverwiz 07-15-2012 14:17

Re: Countdown
 
no. remove_task will cut out that task and delete its hook from execution. From what i see....it should do what you wish it to.

GhostMan 07-15-2012 14:41

Re: Countdown
 
No no, this part works perfectly, i'm just curious about "time" values.

I'm willing to stop the damage when one of tasks like this is active using
PHP Code:

RegisterHam(Ham_TakeDamage"player""hook_TakeDamage"

Now it looks something like that

PHP Code:

public hook_TakeDamage(victimattackeruselessFloat:damagedamagebits) {
 
    switch(
something)
    {

        case 
1:
        {
            if(
task_exists(TASK_1))
                return 
HAM_SUPERCEDE
        
}
        
        case 
2:
        {
            if(
task_exists(TASK_2))
                return 
HAM_SUPERCEDE
        
}

        case 
3:
        {
            if(
task_exists(TASK_3))
                return 
HAM_SUPERCEDE
        
}
        
    
etc...
    
    return 
HAM_IGNORED


But i thought to get a little bit more optimised i could use code like that

PHP Code:

public hook_TakeDamage(victimattackeruselessFloat:damagedamagebits) {
 
    if(
time 0) {
        return 
HAM_SUPERCEDE
    
}
    return 
HAM_IGNORED


then i would save a lot of lines

As far as you said variable TIME stops @ 0 or -1 ( not sure ) and stays with it for good.

Liverwiz 07-15-2012 15:53

Re: Countdown
 
The number of lines of code is -usually- irrelevant to the run time of the code. Its HOW the code is run and written. Just FYI

As for your two pieces of code i'd say the second one is better because you don't need to do more than one check (flag, and task_exists) and it checks to make sure you have time left, rather than calling the native to see if the task is alive. Either way....return HAM_SUPERCEED will not stop damage from being delt (i don't think) you should use SetHamParamFloat(3, 0.0) to deal 0 damage.

Bugsy 07-15-2012 20:22

Re: Countdown
 
Could do something like this
PHP Code:

enum Tasks
{
    
TASK_1,
    
TASK_2,
    
TASK_3,
    
TASK_4,
    
TASK_5,
    
TASK_6
}
new const 
g_TaskIDTasks ] = 
{
    
500,
    
550,
    
600,
    
650,
    
700,
    
750
}

public 
YourFunction() 
{
    
set_task1.0 "TheFunc" g_TaskIDTASK_1 ] );
    
set_task1.0 "TheFunc" g_TaskIDTASK_2 ] );
    
set_task1.0 "TheFunc" g_TaskIDTASK_3 ] );
}

public 
hook_TakeDamage(victimattackeruselessFloat:damagedamagebits
{
    return 
task_existsg_TaskIDTasks:something ] ) ? HAM_SUPERCEDE HAM_IGNORED;



Bugsy 07-15-2012 20:47

Re: Countdown
 
Quote:

Originally Posted by Liverwiz (Post 1751285)
You don't really need the TaskID array if you already have the enum of tasks....an enum already contains a unique int identifier. That's what makes it an enum....
But if you're feeling the need you can set the enum's increment

Code:

enum Tasks (+= 50)
{
    TASK_1 = 500,
    TASK_2,
etc etc etc


Ok genius, then show me how to do this, with using only an enum.
PHP Code:

public hook_TakeDamage(victimattackeruselessFloat:damagedamagebits
{
    return 
task_existsg_TaskIDTasks:something ] ) ? HAM_SUPERCEDE HAM_IGNORED;



Liverwiz 07-15-2012 21:04

Re: Countdown
 
Code:

public hook_TakeDamage(victim, attacker, useless, Float:damage, damagebits) 
    return (time > 0) ? HAM_SUPERCEDE : HAM_IGNORED

The task will be removed after its completion anyway, no need to see if it exists.

Bugsy 07-15-2012 21:16

Re: Countdown
 
Ok, but you didn't respond to my statement above.

And I was just giving him one option, it seemed as if he had the time method already figured out.

Liverwiz 07-15-2012 21:31

Re: Countdown
 
If i'm FORCED to use that method...there is no way. I was just saying that the task identifier would be built into the enum.

Bugsy 07-15-2012 21:48

Re: Countdown
 
Quote:

Originally Posted by Liverwiz (Post 1751301)
If i'm FORCED to use that method...there is no way.

Whether or not you're forced to use this method has nothing to do with it, I want you to backup what you say.

Quote:

You don't really need the TaskID array if you already have the enum of tasks....an enum already contains a unique int identifier. That's what makes it an enum....
But if you're feeling the need you can set the enum's increment
PHP Code:

return task_existsg_TaskIDTasks:something ] ) ? HAM_SUPERCEDE HAM_IGNORED

Quote:

Originally Posted by Liverwiz (Post 1751301)
I was just saying that the task identifier would be built into the enum.

I know what an enum is and how they work. The reason why I used an array is because the method I provided him uses an index that I was passing to the TaskID array to make the code in TakeDamage a simple 1 line return, you can't do this with using only an enum; see method 2 in this post. I was basically taking that method and reducing the code.


All times are GMT -4. The time now is 15:22.

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