Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Add loop-counter & Improve loops #4595

Merged
merged 46 commits into from
Jun 26, 2023

Conversation

AyhamAl-Ali
Copy link
Member

@AyhamAl-Ali AyhamAl-Ali commented Feb 12, 2022

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%"

Test Code:

on command:
	loop 2 times:
		loop 2 times:
			loop 2 times:
				broadcast "&a%loop-counter-1% - &c%loop-counter-2% - &b%loop-counter-3%"

	broadcast "------------------"

	add "a" and "b" to {_1::*} and {_2::*} and {_3::*}
	loop {_1::*}:
		loop {_2::*}:
			loop {_3::*}:
				broadcast "&a%loop-value-1% - &c%loop-value-2% - &b%loop-value-3%"

Showcase
image
image

Thanks to TPGamesNL for helping me with this


Target Minecraft Versions: Any
Requirements: None
Related Issues:

- loop-value support within while loop is yet to be implemented & current iteration expression as well
- This doesn't yet support getting iteration counter of outer loops
- SEEMS like there is a bug with nested while loops, th inner while loop only runs once, haven't tested if this is caused by this PR or before that
@AyhamAl-Ali
Copy link
Member Author

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 AyhamAl-Ali marked this pull request as draft February 12, 2022 04:40
@Pikachu920
Copy link
Member

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

@AyhamAl-Ali
Copy link
Member Author

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

@TPGamesNL TPGamesNL added the feature Pull request adding a new feature. label Feb 12, 2022
@Moderocky
Copy link
Member

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.

@AyhamAl-Ali
Copy link
Member Author

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 👌

@Moderocky
Copy link
Member

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.

@AyhamAl-Ali
Copy link
Member Author

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

- Added Loop interface
- Removed `fails at` syntax
- Added loop-(counter|iteration)-X for both normal and while loop
- Improved performance for ExprLoopValue (regex pattern)
- Improved examples
@AyhamAl-Ali AyhamAl-Ali changed the title 🚀 Add fails at to while loop 🚀 Add loop-counter to loops Mar 30, 2022
@AyhamAl-Ali AyhamAl-Ali marked this pull request as ready for review March 30, 2022 16:48
@AyhamAl-Ali AyhamAl-Ali changed the title 🚀 Add loop-counter to loops 🚀 Add loop-counter & Improve loops Jun 16, 2022
@TheLimeGlass TheLimeGlass self-requested a review October 26, 2022 10:17
Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com>
@TheLimeGlass TheLimeGlass mentioned this pull request Dec 15, 2022
AyhamAl-Ali and others added 3 commits April 19, 2023 17:21
Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com>
@TheLimeGlass TheLimeGlass merged commit 9d06f02 into SkriptLang:master Jun 26, 2023
Moderocky pushed a commit to Moderocky/Skript that referenced this pull request Sep 16, 2023
NotSoDelayed pushed a commit to NotSoDelayed/Skript that referenced this pull request Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Pull request adding a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants