What is that bug with list variables?

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

pepper82

Member
Jan 26, 2017
272
1
18
Somehow it seems skript is ordering elements in a list variable automatically which is bad for my actual script.

Look at this example:

code_language.skript:
command /order:
    trigger:
        add "player1" to {_test::*}
        add "player2" to {_test::*}
        add "player3" to {_test::*}
        
        broadcast "1.) List:"
        loop {_test::*}:
            broadcast "%loop-value%"

        wait 1 second
        broadcast "deleting player1"           
        loop {_test::*}:
            if loop-value is "player1":
                remove loop-value from {_test::*}
            
        broadcast "2.) List:"
        loop {_test::*}:
            broadcast "%loop-value%"

        wait 1 second
        broadcast "adding player1"
        add "player1" to {_test::*}

        broadcast "3.) List:"
        loop {_test::*}:
            broadcast "%loop-value%"

Result:
1.) List:
player1
player2
player3

2.) List:
player2
player3

3.) List:
player1
player2
player3

But List 3.) should be:

player2
player3
player1

because I added player1 after I have added player2 + player3.

Why is this and how can I fix it???
 
This isn't a bug, it's exactly how Skript lists are meant to behave.
If you don't provide an index (by using 'add "some value" to {_list::*}'), Skript will automatically search for the next available integer-based index starting at 1.
Source: https://github.com/bensku/Skript/bl...a/ch/njol/skript/lang/Variable.java#L471-L478

If you want to keep your list ordered, try adding a list "counter" variable, and don't use "add %object% to {_list::*}":

code_language.skript:
set {_list::next} to 1 # Starting a counter for the next index in our list.

set {_list::%{_list::next}%} to "item:1" # Using the counter, the list will always be ordered.
add 1 to {_list::next}                   # The counter must be incremented after an entry is added.

set {_list::%{_list::next}%} to "item:2"
add 1 to {_list::next}

set {_list::%{_list::next}%} to "item:3"
add 1 to {_list::next}

loop {_list::*}:
  
    send "&b[Initial list]&r Index: %loop-index% Value: '%loop-value%'"

delete {_list::1} # Deleting the first entry in our list.

loop {_list::*}:
  
    send "&8[Deleted an entry]&r Index: %loop-index% Value: '%loop-value%'"
 
set {_list::%{_list::next}%} to "item:4" # Adding a new unique entry to our list.
add 1 to {_list::next}

loop {_list::*}:

    send "&3[Added a new entry]&r Index: %loop-index% Value: '%loop-value%'"

0r5nCCZ.png
 
Status
Not open for further replies.