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

For each index & value loop. #6562

Draft
wants to merge 13 commits into
base: dev/feature
Choose a base branch
from

Conversation

Moderocky
Copy link
Member

Description

Having talked with Ayham about my concerns, I've opened this as an alternative proposal to Ayham's loop PR #6053.

This will be in draft until we reach a consensus about which approach to adopt.

Ayham's syntax was:

loop %objects% with index as %-~object%
loop %objects% [with index] as %-~object%( and|,) [with value] %-~object%
loop %objects% [[with value] as %-~object%]

I'm not very happy about changing or adding to the existing loop ... section syntax, because it's such an old and established thing, and I worried that the syntax was becoming a bit long and difficult to interpret.
I also thought this feature might be a good opportunity to mimic for-each loops from other languages like Java and Python.

My alternative for-each is specifically designed for dealing with key<->value maps,
by storing the index and value in reference variables.

It also extends the existing SecLoop so all loop behaviour is available as normal.

The syntax is (note ~object specifically accepts a variable):

for [each] [value] %~object% in %objects%
for [each] (key|index) %~object% in %objects%
for [each] [key|index] %~object% (=|and) [value] %~object% in %objects%

Note: while I wanted to do for A, B in ... (like Python) I was concerned that Skript might mistake this for for <list of A, B> in ... rather than for <A>, <B> in ....

set {_list::*} to 1, 5, and 10

# loop-by-value like a regular loop
for each {_value} in {_list::*}:
    broadcast {_value}

for value {_value} in {_list::*}:
    broadcast {_value}

# loop-by-index
for index {_index} in {_list::*}:
    broadcast {_index}

# loop by index = value
for index {_index} and value {_value} in {_list::*}:
    broadcast "%{_index}% = %{_value}%"

for {_index} and {_value} in {_list::*}:
    broadcast "%{_index}% = %{_value}%"

for {_index} = {_value} in {_list::*}:
    broadcast "%{_index}% = %{_value}%"

Note: this is registered as an experimental feature, toggled with using for each loops.


Target Minecraft Versions: any
Requirements: #6551, #6552
Related Issues: #6052, #6053

@Moderocky Moderocky changed the title For each index & value loop For each index & value loop. Apr 13, 2024
@AyhamAl-Ali AyhamAl-Ali added the feature Pull request adding a new feature. label Apr 13, 2024
@Pikachu920
Copy link
Member

I'm still not sure about this feature. I think the existing system works well, and I think there's a benefit in just having a single simple way to loop things.

@Moderocky
Copy link
Member Author

I'm still not sure about this feature. I think the existing system works well, and I think there's a benefit in just having a single simple way to loop things.

I'm happy with that too, but if people decide we need something I am in favour of this being it.

@sovdeeth
Copy link
Member

Pretty much every loop I've written recently has looked like this, when more than one nested loop is involved.

loop 10 times:
    set {_counter} to loop-value
    loop {_something::*}:
        set {_value} to loop-value-2

It's just so much easier to keep track of what's what, helps avoid off-by-one errors in the loop-value-x, and being able to have descriptive names makes the code way more legible. That said, this code is perfectly fine to keep, but I feel like this addition would be valuable in that it would remove the dead weight of those set lines.

@Moderocky
Copy link
Member Author

helps avoid off-by-one errors in the loop-value-x

I have a vague memory that there are some cases where you can't use the loop-<something>-x because it isn't available within some kinds of subsections (or something like that) and you have to copy it to a variable too.

@sovdeeth
Copy link
Member

helps avoid off-by-one errors in the loop-value-x

I have a vague memory that there are some cases where you can't use the loop-<something>-x because it isn't available within some kinds of subsections (or something like that) and you have to copy it to a variable too.

Yes, any section that resets the events (EffSecSpawn, for example) means you have to put the loop-value into a var in order to access it in the section, since when it runs isn't guaranteed.

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.

None yet

5 participants