Results 1 to 2 of 2

Thread: Is there a way to save a variable's value between a game save and reload ?

  1. #1

    Is there a way to save a variable's value between a game save and reload ?

    I have a variable which holds how many times a deck can be reshuffled and reused before the game ends. During play it reduces towards zero as the deck is reshuffled.

    Is there a way to save the current value of that variable if the user saves their TTS game state and then retrieve it when they reload the saved game at a later date ?
    Last edited by groovyclam; 11-23-2020 at 07:46 AM.

  2. #2
    Found out how to do it:

    The variable I want to save and reload is called deck_refills so make the follow code changes:

    1. Give an argument to your onLoad function definition of saved_data:

    Code:
    function onLoad(saved_data)
    2. Make an onSave function:

    Code:
    function onSave()
    
        local data_to_save = {saved_count = deck_refills}
        saved_data = JSON.encode(data_to_save)
        return saved_data
    
    end
    3. In your main code add the following (note in my game deck_refills depends on the number of players if it isn't loaded from a saved game ):

    Code:
    -- load any saved data from a previous game save
    	
    local loaded_data = JSON.decode(saved_data)
    
    -- check if the load, loaded anthing
    
    if ( loaded_data == nil ) then
    
            -- it didn't, so set deck_refills to -1 temporarily
    	deck_refills = -1
    
    else
    
            -- it did load data, so set deck_refills to the loaded data
    	deck_refills = loaded_data.saved_count
    
    end
    	
    print('Deck Refills loaded as:' )
    print(deck_refills)
    
    -- if I set deck_refills to -1 I have to set it based on the number of players
    	
    if ( deck_refills == -1 ) then
    	
    	if num_players == 3 then
    		deck_refills = 1
    	else
    		if num_players < 3 then
    			deck_refills = 0
    		else
    			deck_refills = 2
    		end
    	end
    		
    print('Deck Refills loaded as blank/-1 so set to:' )
    print(deck_refills)
    
    end
    Last edited by groovyclam; 11-27-2020 at 09:14 AM.

Similar Threads

  1. [SUPPORT] Error: no save found. You must load a save first.
    By Martimusrex in forum Technical Support
    Replies: 3
    Last Post: 09-13-2020, 06:01 PM
  2. Replies: 2
    Last Post: 04-28-2017, 09:48 AM
  3. Save Button in Save/Load not working in VR
    By Klos in forum VR Support
    Replies: 0
    Last Post: 01-30-2017, 08:40 PM
  4. [SOLVED] Game crashes on save-save file not written.
    By tallcaddell in forum Technical Support
    Replies: 2
    Last Post: 11-16-2016, 04:56 PM
  5. Replies: 6
    Last Post: 07-14-2016, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •