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

Moving text algorithm.

Discussion in 'Development' started by RumDaDuMCPE, Aug 16, 2018.

  1. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    Hi.
    I'm trying to code a Task that 'moves' the text. I.e., From left to right.
    Read the code, you would understand what I mean.
    PHP:
    $popups = [
        
    '&7- &eVote for us using the command /vote and earn rewards! -'
    ];

    $length strlen($popups[0]);
    $maxlength 7;

    if (
    $length $maxlength) {
        for (
    $end $length $maxlength$end >= 1$end--) {
            for (
    $start 0$end >= 1$start++) {
                
    $string substr($popups[0], $start, -$end);
                
    $this->player->sendPopup(TextFormat::colorize($string));
            }
        }
    }
    The above code is executed every 1 second (as a repeatingTask) and it does not work and instead breaks the server.
     
    Last edited: Aug 16, 2018
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Because those for loops are causing you to send far more than 44 popups within an extremely small amount of time, probably hundreds within a second.
     
  3. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    :eek:
    So, Increase the period of the task?
     
  4. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    Looks like the loop doesn't end. Could you help me debug?
     
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    For loops don't stop, it will run until the conditions are met. How often do you intend to move the popup, because the for loop would move faster than PocketMine ticks, which isn't helpful at all.

    I would suggest moving the text only when the task runs, rather than every time the loop runs. I would give a code example, but I don't know what kind of animation you're looking for.
     
    RumDaDuMCPE likes this.
  6. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    A horizontal left to right animation.
    such as:
    first tick:
    this is an exa...
    second tick:
    ...his is an exam...
    and so forth till the sentence ends:
    ...is an example!
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Okay, I'll work up an example and post it here in a while.
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you will need $chop, this indicates how much to chop from start and end
    you will need $text, the text to be shifted
    internally you will need $phase this will be a var that gets incremented counter will never be > $chop +1
    internally we will also need $direction, we will use true for positive, false for negative, this will make sense latter

    before we start, if you plan to copy things here and treat it as code, please dont, i here have a tendency to focus on concept more then code, but everything should be self explanatory here, we will use sudo code php for ease of everyone

    lets say
    $text = 1234567890
    $chop = 2
    expected result
    phase:result
    0 12345678
    1 23456789
    2 34567890
    1 23456789
    0 12345678

    we can start by looking at the pattern, we can see what would be 3 and 4 is same to 1 and 0 so lets name them that this is why we need $direction

    we can see that, phase 0, front cutoff is 0, and backcutoff is 2
    thus
    $frontcut = max($phase - $chop,0)
    $endcut = max($chop - $phase,0)
    and adding
    if($phase >= $chop)$direction = false
    if($phase <= 0)$direction = true
    to indicate reverse
    now just cut front and end to get the desired result, like as seen as you have been using substr

    finally you just need to put it in a task, each time you do this and increment the phase based on direction on run
    now you will get a moving handler
    if you need it to go slower/faster, you can let another task to increment it
    WITHOUT showing the popup please, the popup should be constant rate on another task
    but it's recommended to do it on the same task unless you need that further configuration and complexity
     
    RumDaDuMCPE and RyanShaw like this.
  9. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    Appreciate your help, Thunda. <3
     
  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.