Cycle Inventory Items - Help for modifying the script

  • #1, by sebastianFriday, 11. September 2015, 13:42 5 years ago
    Hello community,

    I thought of some other ways to navigate through the inventory instead of using "prev page" / "next page" buttons which appear when there are more items than page 1 can hold.

    So my idea was some kind of scrolling which was also used in the newer version from Zak2 (i think it uses this script from the wiki: Cycle Inventory Items (CMS) http://wiki.visionaire-tracker.net/wiki/Cycle_Inventory_Items_(CMS))

    SCRIPT:
    --[[
    Cycle inventory items by ascending or descending order [v2] (12/02/2014)
    Written by AFRLme [Lee Clarke]
    -- + --
    alternatingfrequencies@hotmail.com | skype @ AFRLme
    -- + --
    This script is donation optional. In game credit is non-negotiable.
    You are free to: ¹ use it in your game(s). ² modify the script.
    Do not remove - or edit - this comment block.
    --]]

    -- * local variables * --
    local char, items, item, amt -- empty variables
    local slots = 0 -- define inventory slot amount here
    local cycle = false -- false = only cycle inventory when item amount equals same as or more than inventory slots. true = always cycle

    -- * function for changing cycle mode * --
    function toggleCycleMode()
    if cycle then cycle = false else cycle = true end
    end

    -- * function for cycling through the inventory items * --
    function cycleItems(asc)
    char = game:getLink(VGameCurrentCharacter) -- store current character
    items = char:getLinks(VCharacterItems) -- store inventory items into a table
    amt = table.maxn(items) -- get index number of last item
    if cycle or amt >= slots then
    if asc then item = items[amt]; table.remove(items, amt); table.insert(items, 1, item) else item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item) end
    char:setValue(VCharacterItems, items) -- update the inventory
    end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32


    While this is a great idea i want to tweak it a bit because it still is a bit weird to use.
    Instead of scrolling left or right to infinity one item at a time i'd like to scroll a specific amount of items (lets say 4) in one direction, but only so much until i reach A) the first items or B) the last items in the list. So it should stop at page 1 when scrolling -1 and stops at page n when scrolling to page n+1 (n amount of current pages available).

    After this works i want to add a scrollbar in the inventory which indicates the current position (page) in the inventory relative to the max amount of pages available.

    I think the last part i can solve by my own, but editing the script from Lee / AFRLme to act like i described above is a bit hard to solve for me...

    Hope someone can help me here smile

    kind regards

    Sebastian


    Thread Captain

    2337 Posts


  • #2, by afrlmeFriday, 11. September 2015, 13:51 5 years ago
    To scroll by 4 items, you would need to have it loop 4 times or just grab the last 4 items & then move them to the back or first position. So you could call the function 4 times... I forget what the function is named without looking at the script, so the name below is just made up...
    scrollItems(false); scrollItems(false); scrollItems(false); scrollItems(false)
    1
    2

    ... calls the scroll item function left 4 times. Or you could modify the script slightly to include 4 variables or temp tables or even add a new function variable for declaring loop amount & then include a for loop with the new loop value. There are multiple ways you could edit my script. wink

    I recommend looking at what I did & having a go at scripting it yourself. You'll never learn if you don't try!

    Imperator

    6842 Posts

  • #3, by sebastianFriday, 11. September 2015, 13:56 5 years ago
    yeah. calling the script 4x or (as i did it now) repeating the actions inside the lua script worked:
    if asc then 
    item = items[amt]; table.remove(items, amt); table.insert(items, 1, item)
    item = items[amt]; table.remove(items, amt); table.insert(items, 1, item)
    item = items[amt]; table.remove(items, amt); table.insert(items, 1, item)
    item = items[amt]; table.remove(items, amt); table.insert(items, 1, item)

    else
    item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item)
    item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item)
    item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item)
    item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item)
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13


    Now how do i get the items stop cycling when the first item in the list was reached and the last one is reached? So that it doesn't scroll to infinity...

    I thought of defining 2 variables: page and pages
    page = actual page (initial value 1)
    pages = amount of available pages (initial value 1, depending on max items in inventory changing to 2, 3, ...)



    But how do i tell the script that it should scroll anymore when the start/end is reached?
    Maybe is there a way to call the "Normal" page scroll prev/next via luascript? By that i dont cycle items but just going to the next page

    Thread Captain

    2337 Posts

  • #4, by afrlmeFriday, 11. September 2015, 14:13 5 years ago
    Hmm... I thought I added an alternative or countermeasure option to enable or prevent infinite cycling? It's a little complicated though as you are effectively changing the index id of the items each time you move them.

    P.S: instead of duplicating the lines as you have done in your example, you could have used the for loop instead. wink

    if asc then
    for a = 1, 4 do
    item = items[amt]; table.remove(items, amt); table.insert(items, 1, item)
    end
    else
    for b = 1, 4 do
    item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item)
    end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    ... less code is always more desirable. Easier to manage & not 100% sure, but it probably uses less RAM, as there's much less code to read.

    edit: on second thought... it might be possible to use the scrolling system if you were to sort the items out by object id or alphabetically. There needs to be something the engine can use to determine the initial item & the last item.

    Imperator

    6842 Posts

  • #5, by sebastianFriday, 11. September 2015, 15:19 5 years ago
    Hmm yeah. there should be something in lua that only turns the page (is it saved in the datastructure which page is shown?)

    I found "CharacterItemsScrollPosition" as a data structure field. could a use this to set the actual page? My tries to read out that field ended up in
    15:16:24: Warning: Tried to access invalid field. Field CharacterItemsScrollPosition (id: 518) does not exist in table Character (id: 148).
    15:16:24: Warning: TDataGroup::GetValue: Cannot find field in object: CharacterItemsScrollPosition
    15:16:24: value for TLink is NULL
    15:16:24: ---Empty---
    1
    2
    3
    4
    5

    errors...

    Thread Captain

    2337 Posts

  • #6, by afrlmeFriday, 11. September 2015, 15:46 5 years ago
    According to the data structure that field is not scriptable.
    print(Characters["Tom"].ItemsScrollPosition)
    1
    2

    It works based on the index id, but the cycle script updates the index id instead by swapping item position instead of scrolling.

    Imperator

    6842 Posts

  • #7, by sebastianFriday, 11. September 2015, 15:50 5 years ago
    So i cant use it via Lua to swap the page?
     Error: Unknown data-field "ItemsScrollPosition".
    15:47:42: nil
    1
    2
    3

    Your code gives also an error

    Thread Captain

    2337 Posts

  • #8, by afrlmeFriday, 11. September 2015, 16:14 5 years ago
    Sorry, No idea. It could be a redundant data structure field.

    Imperator

    6842 Posts

Write post