How to save table entries into savegame

  • #1, by PanSWednesday, 13. March 2019, 10:09 6 years 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
    

    Newbie

    73 Posts


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

    Thread Captain

    1590 Posts

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

    Newbie

    73 Posts

  • #4, by SimonSThursday, 14. March 2019, 00:57 6 years 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

    1590 Posts

  • #5, by PanSThursday, 14. March 2019, 10:15 6 years 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

    73 Posts

  • #6, by PanSThursday, 14. March 2019, 15:06 6 years 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
    

    Newbie

    73 Posts