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

  • #1, by PanSFriday, 22. October, 17:07 A month 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)
    1
      for i=0, #Characters.char_onk.Interfaces do
        if Characters.char_onk.Interfaces[i] == interface then

          table.remove(game.CurrentCharacter.Interfaces, interface)
        end
      end
    1
    2
    3
    4
    5
    6
    7

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

    Newbie

    73 Posts


  • #2, by PanSMonday, 25. October, 10:59 A month 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
    1
    2
    3
    4

    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, 11:09 A month ago
    Lua has no real fields so all you're doing when calling
    table.insert(game.CurrentCharacter.Interfaces, interface)
    1

    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
    1
    2
    3

    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
    1
    2
    3
    4
    5

    Thread Captain

    1477 Posts

  • #4, by PanSMonday, 25. October, 15:43 A month 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
    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43

    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
    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

    Newbie

    73 Posts

Write post