#Function script feedback

1 messages · Page 1 of 1 (latest)

digital raft
#
    set {_totalChance} to 0
    loop {_chances::*}:
        set {_entry::*} to split loop-value at ":"
        set {_chance} to {_entry::2} parsed as number
        add {_chance} to {_totalChance}

    set {_random} to random integer from 1 and {_totalChance}

    set {_lastNumber} to 0

    loop {_chances::*}:
        set {_entry::*} to split loop-value at ":"
        set {_item} to {_entry::1} 
        set {_chance} to {_entry::2} parsed as number  

        set {_rangeStart} to {_lastNumber} + 1
        set {_rangeEnd} to {_lastNumber} + {_chance}
        set {_lastNumber} to {_rangeEnd}

        if {_random} >= {_rangeStart}:
            if {_random} <= {_rangeEnd}:
                return {_item}```

So this function is designed to take a list set out like this: "item:30", "item2:40". Which is an item name and a chance. It rolls it and returns the item it landed on.

This is my first time making a very reusable function like this and would appreciate some feedback as I'm not the best and i probably missed some things.
karmic dawn
#

instead of having it like "item:30" you couldve just passed in 2 lists like (items: items, chances: nums)

set {_var} to 0 is redundant, there are 2 in your code

#

and at the end you couldve done if {_random} is between {_rangeStart} and {_rangeEnd}

digital raft
#

ill try it again though