How to save table entries into savegame

  • #1, by PanSWednesday, 13. March, 10:09 A week ago
    Hi,

    I created a simple function to randomize a shoe counter. Everytime the player steps into a special room a sitting character has differnt shoes. Every pair of shoes is shown once. The function works fine but of course the current table entries wont be saved. Do you have an idear?

    local tShoes = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}

    function randomShoes()

      if Conditions["cond_no_more_shoes"].ConditionValue == false then

        local shoeNum = math.random(1, #(tShoes))

        print('shoe index number '..shoeNum..' is called: '..tShoes[shoeNum])
        print('max numbers of shoes: '..#(tShoes))
       
        if #(tShoes) <;= 1 then
          Conditions["cond_no_more_shoes"].ConditionValue = true
          print('the shoe seller has nothing left in stock')
        end
       
        table.remove(tShoes, shoeNum)
      end
     
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    Newbie

    49 Posts


  • #2, by SimonSWednesday, 13. March, 10:14 A week ago
    2 options:
    - serialize the missing parts into a value / condition (e.g.  value | 2^9)
    - make the table global

    Thread Captain

    1187 Posts

  • #3, by PanSWednesday, 13. March, 22:13 A week ago
    Thx. Making the table global is the easiest way. Is there a disadvantage for this option?

    Newbie

    49 Posts

  • #4, by SimonSThursday, 14. March, 00:57 A week ago
    Hm.. Could be possible that it won't remove the table entries, you would need to check, also changes in the ved will be overwritten by old savegames.

    Thread Captain

    1187 Posts

  • #5, by PanSThursday, 14. March, 10:15 A week ago
    Indeed. This option produces a strange behavior. When I start the game (from Editor) and load the savegame the table has all entries (10). But when I start the game and play aslong the table entries are less then in the savegame (7 entries) it loads the correct number. Seems the game only filled the table with the loaded one from the savegame.

    So maybe I have to clear the table completly before loading a savegame, right?

    Newbie

    49 Posts

  • #6, by PanSThursday, 14. March, 15:06 A week ago
    Thats my full script working perfectly so far.

    Thx for your help!

    --this randomize fabians shoes
    tShoes = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
    local cNoShoes = Conditions["cond_no_more_shoes"]
    local vShoeCounter = Characters["char_fabian"].Values["count_fabians_shoes"]

    function randomShoes()
      if cNoShoes.ConditionValue == false then
        if #(tShoes) >;= 1 then
     
          local shoeNum = math.random(1, #(tShoes))
     
          print('shoe index number '..shoeNum..' is called: '..tShoes[shoeNum])
          print('max numbers of shoes: '..#(tShoes))
         
          vShoeCounter.Int = tonumber(tShoes[shoeNum])
         
          table.remove(tShoes, shoeNum)   
        else
          cNoShoes.ConditionValue = true
          print('the shoe seller has nothing left in stock')
        end 
      end
    end

    --clears shoe table before load a savegame
    function clearShoeTable()
      tShoes = {}
      print('the shoe table has '..#(tShoes)..' entries')
    end

    --play the correct animation when showing scene
    function playShoesAnimation()
      local numShoes = vShoeCounter.Int
      if numShoes <;= 42 and cNoShoes.ConditionValue == false then
        startAnimation("Scenes[room_03_lounge].Objects[set_fabian_legs].Animations[shoes_"..numShoes.."]")
        print('play animation shoes_'..numShoes)
      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

    Newbie

    49 Posts

Write post