List variables losing their indexes when used in functions, why?

  • 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
Why do list variables lose their indexes when used in functions? Is it a known bug, is there any workaround?

code_language.skript:
function sortList(list: objects):
    set {_sortedList::*} to {_list::*}
    loop {_list::*}:
        add 1 to {_count}
        set {_indexes::%loop-value%} to loop-index
        if mod({_count}, 20) = 0:
            wait 1 tick
    loop {_sortedList::*}:
        set {_current} to {_indexes::%loop-value%}
        broadcast "%{_current}% - %loop-value%"

code_language.skript:
command /lis:
   trigger:     
      set {_test::bronko} to 100
       set {_test::alvin} to 5000
       set {_test::marviina} to 22
       sortList({_test::*})
 
This is intended behavior, sorta. Skript is strictly pass-by-value, not pass-by-reference (like java), so every time you pass around a list to a function, Skript creates a brand new list and copies over the values. This is why Skript discards indexes; it simply reconstructs the list in order.

Workaround: use global lists. Pass the variable's name as text and simply access the list like any other global variable.

code_language.skript:
function accessList(var: text):
    loop {%{_var}%::*}:
        broadcast "%loop-index%"
 
Status
Not open for further replies.