1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Countdown

Discussion in 'Development' started by LucGamesDE, Jun 4, 2017.

  1. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I have a Repeatingtask with a count down that runs every second. How can I now run the countdown only every 2 seconds if the count is smaller than 10?
     
  2. Daltontastic

    Daltontastic Spider Jockey

    Messages:
    28
    Sharing a code snippet will help people help you.
     
  3. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    In my Repeatingtask:
    PHP:

    if($count >= && $count <= 20) {
    $server->broadcastMessage($count);
    $count--;
    } else {
    $this->count 20;
    }

    //But now I want to slow down the countdown when it reaches 10
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    A way would be to decrement the valueb 0.5. But this isn't flexible at all. This will only work with numbers between 0 and 1 that are multiples of 2 or 5. For example, using $speedAfter10 as 1/3 would change the value of $count from 10 to 9.7, 9.4, 9.1, 8.8 etc. Here, the code below would skip 9 totally because $count was never 9.0.

    The strpos is a hack btw, I thought of using if(is_float($count)) but that didn't work.
    PHP:
    $count 20;
    $speedAfter10 1/2;

    if(
    $count >= && $count <= 20){
        if(
    $count >= 10) {
            
    $server->broadcastMessage($count);
            
    $count--;
        }else{
            if(
    strpos($count".") === false){
                
    $server->broadcastMessage($count);
            }
            
    $count -= $speedAfter10;
        }
    }else{
        
    $count 20;
    }
     
    Last edited: Jun 5, 2017
    Daltontastic likes this.
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Or use a dual counter:
    PHP:
    class DualCounterTask extends PluginTask{
    private 
    $primaryCounter;
    private 
    $secondaryCounter;

    public function 
    __construct(Plugin $owner){
        
    parent::__construct($owner);
        
    $this->primaryCounter 30;
        
    $this->secondaryCounter $this->getTicksPerPrimaryCount();
    }

    public function 
    onRun($ticks){
        --
    $this->secondaryCounter;
        if(
    $this->secondaryCounter <= 0){
            --
    $this->primaryCounter;
            
    $this->secondaryCounter $this->getTicksPerPrimaryCount();
            if(
    $this->primaryCounter <= 0){
                
    // end of the counter, do something?
            
    }
        }
    }

    private function 
    getTicksPerPrimaryCount() : int{
        if(
    $this->primaryCounter 20) return 25;
        if(
    $this->primaryCounter 10) return 20;
        return 
    10;
    }
    }
    In the above snippet, there are two counters, namely "primary counter" and "secondary counter". They work like the seconds and minutes needles of a clock. When the secondary counter finishes a cycle (decreases from its highest value to 0), the primary counter decreases by one, and the secondary counter rewinds back to the highest value. The task should be scheduled at a repeating rate of 1 tick.
    getTicksPerPrimaryCount() evaluates the highest value of the secondary counter. In the above code logic, where the primary counter starts at 30:
    When the primary counter is greater than 20 (i.e. from 21 to 30), the primary counter decreases by one every 25 ticks;
    Otherwise, when it is greater than 10 (i.e. from 11 to 20), it decreases by one every 20 ticks;
    Otherwise (i.e. from 0 to 10), the primary counter deceases by one every 10 ticks.

    The secondary counter decreases by one every tick. Since the secondary counter's reset value varies with the primary count, the primary count decrements at an inconsistent rate depending on the secondary counter, so it decreases at 1/1.25 Hz from 30 to 20, at 1/1 Hz from 20 to 10, and at 1/0.5 Hz from 10 to 0.
     
    Daltontastic and corytortoise like this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    strpos($count, ".") is a very hacky check. It'd be most favourable if your answers on the Development forum provide answers to similar cases rather than answers only to this specific case.
    Please also elaborate your answer. This forum is not a code writing service.
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yeah I was super confused before using that. I couldn't find a better substitute (maybe round(float) === float would work, yet kinda hacky). I thought is_float() would work, but the code converts the integer to float permanently.
     
  8. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    It's a shame you can't use instanceof on scalar types... that would simplify life.
     
    Muqsit likes this.
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    How is it relevant here? 1.5 - 0.5 is still a float.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.