[SOLVED] How to set /unset an interface via Lua

  • #1, by PanSFriday, 22. October 2021, 17:07 2 years ago
    Is there a way to set and unset a characters interface in lua?

    For Example, I have an maininterface with all icons and actions. But I want to have some movable elements (buttons, items...). Parts of an interface are not movable as far as I know. So I have another interface (only a button) which is also a maininterface. Works so far when set the interface from beginning. But this button should be optional.
    Yes there some ways to solve it (deactivate the button / move this interface out of the screens) but they have other disadvantages. The simples way would be to unset the interface via lua.

    Adding/Removing the table entries dont work. (okey, it works to add or remove the entry, but the interfaces are not visible after adding etc.)

    table.insert(game.CurrentCharacter.Interfaces, interface)
      for i=0, #Characters.char_onk.Interfaces do
        if Characters.char_onk.Interfaces[i] == interface then
    
          table.remove(game.CurrentCharacter.Interfaces, interface)
        end
      end
    

    Additional I would like to duplicate interfaces (items) and delete them (unset) after a short time.

    Newbie

    73 Posts


  • #2, by PanSMonday, 25. October 2021, 10:59 2 years ago
    Okay, there is apparently no way to manually unset an interface(if there are more of the same class). But is there a way to change the sprite path of a single animation frame? I can print the frame tables, but it seems no having access to the name, pausetime, path etc...
        for i=0, #animItem.Sprites do
          print(animItem.Sprites[i])
        end
    

    If its not possible changing single frame paths I explain what I want to do.

    If the player has a saved object (item) choosen to combine it, want to unselect it and pressing a key/mouse button to unselect it there should be an animation: The item moves from the current cursor position to the screen corner with the inventory item. That should be part of the main interface, BUT I cant move an interface button (but I can change the sprite path) NOR I cant change a frame path (but I can move the Animation)...

    At the moment I have a second main interface with only a button with no functions. When unselecting the current item, I change the buttons sprite path to the items path. Then this whole interface moves to the inventory corner. That works but has some other disadvantage.

    Maybe Im stupid and there is a simplier way to do that but I cant find one. grin

    Newbie

    73 Posts

  • #3, by SimonSMonday, 25. October 2021, 11:09 2 years ago
    Lua has no real fields so all you're doing when calling
    table.insert(game.CurrentCharacter.Interfaces, interface)

    is creating a temporary table and modifying it. That doesn't do anything. You need to this:

    local t = game.CurrentCharacter.Interfaces
    table.insert(t, interface)
    game.CurrentCharacter.Interfaces = t

    Alike for removing.

    The same happens for sprites. This is an array of tables.
        for i=0, #animItem.Sprites do
          print(animItem.Sprites[i].path)
          print(animItem.Sprites[i].position)
          print(animItem.Sprites[i].pause)
        end

    Thread Captain

    1580 Posts

  • #4, by PanSMonday, 25. October 2021, 15:43 2 years ago
    Thx Simon. I hope this code is not problematic (error-prone ...) but it works how I wanted:

    For the pocket item animation:
    function pocketItem(item)
      --print(item)
      if not item:isEmpty() then
        local curPos = getCursorPos()
        local itemGfxPath = item.Sprite.Sprite:getPath()
        
        local tmpOff = 22
      
        showItemInt = true
    
        local sprTable = {}
    
        for i=0, 1 do
          if animItem.Sprites[i] then
            sprTable[i] = animItem.Sprites[i]
          end
        end
    
        if sprTable[1] then
          sprTable[1].path = "" ..itemGfxPath.. ""
          sprTable[1].pause = 100
      
          animItem.Pause = timePocket
          animItem.Sprites = sprTable
        end
    
        startAnimation(animItem)
    
        ActiveAnimations.anim_default_item:to(0, { AnimationCurrentPosition = {x = curPos.x - tmpOff, y = curPos.y - tmpOff}}, easeLinearInOut)
      
        local movPos = { x=0, y=0 }
      
        movPos.x = posInvIcon.x - math.floor(pxItemGfx/2)
        movPos.y = posInvIcon.y - math.floor(pxItemGfx/2)
      
        setDelay(10, function() 
          ActiveAnimations.anim_default_item:to(timePocket, { AnimationCurrentPosition = {x = movPos.x, y = movPos.y}}, easeBackIn)
        end)
      
        setDelay(10+timePocket, function() animInvBttn() end)
      end
    end
    

    And set/unset hotspot button:
    function activateHotBttn()
      local gameHotBttn = Scenes.new_menu_main.Conditions.cond_hotspot_button.ConditionValue
      local invHotSpots = Interfaces.int_hotspots
      local tblInterface = {}
    
      tblInterface = game.CurrentCharacter.Interfaces 
    
      local iC = 0 --check if an interface is already set
    
      if gameHotBttn == true then
        --print("activate hot button")
        for i=0, #tblInterface do
          if tblInterface[i] == invHotSpots then iC = iC + 1 end
        end
    
        if iC == 0 then
          table.insert(tblInterface, invHotSpots)
        end
      else
        --print("deactivate hot button")
        for i=0, #tblInterface do
          --print(tblInterface[i])
          if tblInterface[i] == invHotSpots then
            --print(tblInterface[i], "is part of the table")
            table.remove(tblInterface)
          end
        end  
      end
    
      game.CurrentCharacter.Interfaces = tblInterface
    end

    Newbie

    73 Posts