Skript icon indicating copy to clipboard operation
Skript copied to clipboard

🚀 Add `loop-counter` & Improve loops

Open AyhamAl-Ali opened this issue 2 years ago • 10 comments

Description

This PR adds a new feature to loops

  • Add loop-(counter|iteration)[-X] for both normal and while loop
  • Improve performance for ExprLoopValue (regex pattern)
  • Add Loop interface (see use in code)
  • Improve examples
  • Improve related classes that uses SecLoop and SecWhile checks such as EffExit and EffReturn

Usage example:

while player is online:
    give player 1 stone
    wait 5 ticks
    if loop-counter > 30:
        stop loop

loop {top-balances::*}:
    if loop-iteration <= 10:
        broadcast "##%loop-iteration% %loop-index% has $%loop-value%"

Showcase image image

Thanks to TPGamesNL for helping me with this


Target Minecraft Versions: Any Requirements: None Related Issues:

  • #4126
  • #1549
  • #862

AyhamAl-Ali avatar Feb 12 '22 04:02 AyhamAl-Ali

Converting to Draft as this is still in progress to support outer loops and look into a while loop bug that is mentioned in the second commit message

AyhamAl-Ali avatar Feb 12 '22 04:02 AyhamAl-Ali

honestly, I'm not sure if I like the syntax or concept . I think @FranKusmiruk's idea of iteration count is a better fit

Pikachu920 avatar Feb 12 '22 05:02 Pikachu920

That syntax is up-for-debate, the original idea was to shorten this to 1 line instead of using something like this

while 1 = 1:
    # code
    if iteration count > 30:
        exit loop

AyhamAl-Ali avatar Feb 12 '22 05:02 AyhamAl-Ali

while 1 = 1:
    # code
    if iteration count > 30:
        exit loop

That is a better alternative, but remember it would need to support multiple loops like loop-value-n does.

Moderocky avatar Feb 13 '22 09:02 Moderocky

while 1 = 1:
    # code
    if iteration count > 30:
        exit loop

That is a better alternative, but remember it would need to support multiple loops like loop-value-n does.

Yes, that's why it's in draft right now 👌

AyhamAl-Ali avatar Feb 13 '22 09:02 AyhamAl-Ali

Coming back to this, I think the iteration count syntax should be while-number to go with loop-number. Nested while loops could use while-number-X. This ought to make it simpler for users.

Moderocky avatar Feb 26 '22 17:02 Moderocky

Coming back to this, I think the iteration count syntax should be while-number to go with loop-number. Nested while loops could use while-number-X. This ought to make it simpler for users.

This can be an alternative syntax, because the purpose has become a bit bigger, the iteration count should also work for loops like

loop {top-players::*}:
    iteration count <= 10
    # code

AyhamAl-Ali avatar Feb 26 '22 17:02 AyhamAl-Ali

@TPGamesNL

Although I'd like to be able to see loop-iteration be usable in the loop expression / while condition directly (e.g. while loop-iteration <= 10:), this isn't something the current API allows.

I don't know how to achieve something like yet, if it's a simple thing that you can put me on the right path to it I might be able to do it otherwise I am not into adding that in this PR, maybe in another PR once I figure out how to do it

AyhamAl-Ali avatar Jul 01 '22 15:07 AyhamAl-Ali

I don't know how to achieve something like yet, if it's a simple thing that you can put me on the right path to it I might be able to do it otherwise I am not into adding that in this PR, maybe in another PR once I figure out how to do it

This isn't something the current internal API supports, is what I meant. This is better left for the future

TPGamesNL avatar Jul 01 '22 15:07 TPGamesNL

Another related issue https://github.com/SkriptLang/Skript/issues/1549

TheLimeGlass avatar Jul 19 '22 21:07 TheLimeGlass