Skript
Skript copied to clipboard
🚀 Add `loop-counter` & Improve loops
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
Thanks to TPGamesNL for helping me with this
Target Minecraft Versions: Any Requirements: None Related Issues:
- #4126
- #1549
- #862
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
honestly, I'm not sure if I like the syntax or concept . I think @FranKusmiruk's idea of iteration count
is a better fit
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
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.
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 👌
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.
Coming back to this, I think the iteration count syntax should be
while-number
to go withloop-number
. Nested while loops could usewhile-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
@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
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
Another related issue https://github.com/SkriptLang/Skript/issues/1549