Is it possible to move the images of individual inventory items one by one? (or scroll the inventory through scripting?)

  • #1, z lemonheadMonday, 22. July 2013, 16:15 hodinky 12 years ago
    Hi, I'm trying to see if I can change the positions of individual items in the inventory so I can animate their sliding up/down in turn, instead of moving the entire inventory as a whole. As far as I can see, the only way to change an image position through scripting is by changing the position of an active animation, as x/y co-ords of other things like sprites are not scriptable according to Data_Structure_3.x. The closest I got to achieving this was to move the animation of an inventory item, and in theory this should work to control the positions of all items, but somehow no matter which item or how many items I specify it moves only 1 item (same one each time).

    This is the problematic code:

    function prepOpenInv()
        local kitems = getObject("Game.GameCurrentCharacter"):getLinks(VCharacterItems)
        local VileItem, VileItem2
        for i=1,table.maxn(kitems) do
          VileItem = kitems[i]:getLink(VObjectAnimation)
          print(kitems[i]:getName())
          print(VileItem:getName())
          VileItem2 = getObject("ActiveAnimations[" .. VileItem:getName() .. "]")
          VileItem2:setValue(VAnimationCurrentPosition, {x=0,y=100})
        end
    end
    


    For each item, I print its name and the name of the animation. As the message log shows below (from the 2 print() lines above), it does manage to iterate through the items alright, but somehow the animations connected to the items are all identical. And yet each animation plays properly.

    21:49:57: 1_Weapon
    21:49:57: Unbenannt
    21:49:57: 2_sharp_rock
    21:49:57: Unbenannt
    21:49:57: 3_Scanner
    21:49:57: Unbenannt

    I've included a project file that should work with the demo game so you can see what I mean. Just click anywhere (except the item squares and arrows) on the top inventory and you should see only one image disappear. If the code worked as intended, it would make all the images disappear. Instead, only the second one disappears.

    Does anyone know why this happens, or can you suggest a better way to achieve this? Thanks!

    P.S. Is it possible to handle inventory scrolling through scripting, so players can scroll through the inventory by hovering on a button instead of clicking? I was thinking of using CharacterItemsScrollPosition for this, but again it seems unscriptable according to Data_Structure_3.x.

    Bažant

    6 Posts


  • #2, z afrlmeMonday, 22. July 2013, 16:43 hodinky 12 years ago
    A'llo Lemonhead smile

    Ok so, you need to increment the index number inside of the square brackets.
    I recommend creating another local variable inside either called "i" or "index" & then just before the "end" of the for handler add:

    i = i + 1
    -- or alternatively whatever variable name you decide to use
    


    also I recommend when creating variables inside of functions that you store them inside of a temporary table so that the data doesn't get created multiple times & stored into the save game data...

    tblName = {}
    tblName["_temporary_"] = ""
    tblName[integer value] = -- something...
    tblName["name"] = -- alternative method
    


    alternatively create the initial local variables outside of the function & update them inside of the function.

    P.S: just because the data structure says something is unscriptable doesn't necessarily mean that it is...
    & also some of the data structure could be slightly out of date or whatever it does says from v3.x after all.

    Imperátor

    7285 Posts

  • #3, z lemonheadMonday, 22. July 2013, 16:58 hodinky 12 years ago
    Wow that's fast! Okay, I'll try it and let you know how it goes (: Thank you!

    Bažant

    6 Posts

  • #4, z lemonheadMonday, 22. July 2013, 17:24 hodinky 12 years ago
    Hmm it doesn't seem to work. The incrementing part is working alright (that's why we get 1_Weapon, 2_sharp_rock and 3_Scanner in the log) but the problem is that it seems they all link to the same animation. Perhaps this has something to do with how items only get 1 animation and we can't name the animation?

    I'm really confused about the local variable thing though. I thought local variables get destroyed after the block finishes executing. Or even if they don't get destroyed, wouldn't creating them in a loop make it just replace the same one over and over again since they have the same name? That way it won't bloat up the save file. And if I create the local variables outside of the function, won't they be on the main level / outside of all blocks? I'm a beginner though, so I really don't know. Anyway, do you recommend that tblName should be used as a global table?

    Bažant

    6 Posts

  • #5, z afrlmeMonday, 22. July 2013, 17:24 hodinky 12 years ago
    yeah I don't know about that - the log file is chucking a load of different error messages at me.

    for cycling items automatically you could create on mouse over/out actions for the arrow tags & wrap them inside of an if query with a condition or value that you set as true on mouse over & false on mouse out & inside of this if query you could create a pause function to control the speed in which the items should scroll & then at the end add a jump back to x action part to create a loop which will break/end if the condition or value is not true...

    P.S: if you really wanted to, I believe you could even create a custom inventory system without using the one that comes with VS editor by using a combination of tables, table sort & for handlers. (an idea)

    Imperátor

    7285 Posts

  • #6, z afrlmeMonday, 22. July 2013, 17:32 hodinky 12 years ago
    Hmm it doesn't seem to work. The incrementing part is working alright (that's why we get 1_Weapon, 2_sharp_rock and 3_Scanner in the log) but the problem is that it seems they all link to the same animation. Perhaps this has something to do with how items only get 1 animation and we can't name the animation?

    I'm really confused about the local variable thing though. I thought local variables get destroyed after the block finishes executing. Or even if they don't get destroyed, wouldn't creating them in a loop make it just replace the same one over and over again since they have the same name? That way it won't bloat up the save file. And if I create the local variables outside of the function, won't they be on the main level / outside of all blocks? I'm a beginner though, so I really don't know. Anyway, do you recommend that tblName should be used as a global table?


    if the table will only be used inside of the function you add it then it can be local if you prefer.
    in regards to local variables - each time you call one inside of a function, it creates a entry into a table & thus if you were to use them inside of a mainLoop you would end up with a lot of unnecessary data.

    creating temporary tables dumps all the stuff you don't need at the end of the session.

    local is local to the script or the function or if query you add them inside of so it depends on where you use them really...
    global functions & variables can be used throughout any script you create inside of the same project so you need to be careful when naming them.

    also few other things: you are only allowed one eventHandler/Listener type per project, which is why I recommend adding them to a separate script & then linking functions into them rather than creating code inside of them.

    http://wiki.visionaire-tracker.net/wiki/Player_Commands

    Imperátor

    7285 Posts

  • #7, z lemonheadMonday, 22. July 2013, 17:52 hodinky 12 years ago
    How does Visionaire know which tables are temporary and should be dumped?

    Thanks for explaining the cycling! I think the custom inventory system is too much for me right now, but I'll keep it in mind (: Anyway, thank you for all your help! You're really quick!

    Bažant

    6 Posts

  • #8, z afrlmeMonday, 22. July 2013, 18:16 hodinky 12 years ago
    because you created a temporary table like I showed you, in an example of earlier on wink

    no, worries wink
    I told Chris to just ask if he needed any help with any of the Lua stuff, providing it isn't something overly complicated that's going to distract me too long from my own work wink

    Imperátor

    7285 Posts

  • #9, z lemonheadTuesday, 23. July 2013, 16:15 hodinky 12 years ago
    Oh, so it checks the _temporary_ key to see if it's defined? Should've guessed.

    I think I've finally figured out what was wrong with the code in my first post. While the names of the animation for each item are the same, that doesn't mean the objects are identical, as Visionaire doesn't identify the objects by name only. Printing their IDs instead of their names shows that the function is indeed fetching different animations. Silly me. So now my question is, instead of looking up the name in the ActiveAnimations table, is there any way I can retrieve it through the ID (or some other unique property) instead?

    Also, how are buttons linked to inventory items? Instead of getting the animation directly from the item itself (which would be problematic for retrieving things in the correct order), would it be possible to look up each button by name and get the link to the inventory item connected to it at the time of calling the script (since the buttons are not always connected to the same items)?

    Thank you and all the best with your work! (:

    Bažant

    6 Posts

  • #10, z afrlmeTuesday, 23. July 2013, 16:58 hodinky 12 years ago
    I believe you can use names or table id values...
    http://wiki.visionaire2d.net/index.php?title=GetObject

    As for the rest, I can't say off the top of my head without looking into it.
    I assume though that if you were to store all items in linked characters inventory & then print them out by order of their id that they should be in same order as displayed in the placeholders....

    the stuff we can do with items seems to be somewhat limited; coding wise.

    Imperátor

    7285 Posts

  • #11, z lemonheadTuesday, 23. July 2013, 17:35 hodinky 12 years ago
    The thing is, when I print IDs of animations I get something like "table: 100C81F8" instead of (0,3). Basically I've successfully got the animation as an object using something like getObject("Game.GameCurrentCharacter.CharacterItems[sword].ObjectAnimation"), but I'm trying to look this object up in the ActiveAnimations table (because it seems I can only change the position of an ActiveAnimation and not a mere Animation), and since names aren't unique, I can't use that for the lookup. So given that I have the animation as an object, is it possible to find that same animation in the ActiveAnimations table using something other than name?

    Bažant

    6 Posts