Results 1 to 8 of 8

Thread: Resuming a saved game...

  1. #1

    Resuming a saved game...

    Resuming a saved game with active scripting... is this even possible? Am I missing something obvious? (Wouldn't surprise me if I was )

    I have a script with more global variables than I like and a dozen or so large'ish global table arrays that monitor the status of various objects and game states. Last night the thought hit me that every time I run the game, I'm starting from scratch and reinitializing everything. I know, late in the process to finally think about this but what can I say...

    The savegame '.json' file records the position and status of all the objects on the table, but it doesn't save the state of the scripting.

    I must admit, when this finally hit me... it was an 'oh shite' moment.

    Thoughts?

  2. #2
    Join Date
    Feb 2016
    Location
    Sweden
    Posts
    77
    You'd have to save the data in an object that you then mark persistent or load in next time you load the game.
    I do this for my blackjack saves in [URL="http://steamcommunity.com/sharedfiles/filedetails/?id=629958456"]Scripted Anime Blackjack[/URL] (Not sure if the game works atm, unsure if I uploaded a broken version or not, it's kinda on hold)

  3. #3
    Whoa! That's a non-trivial amount of data in my case. And what exactly do you mean by 'save data in an object'? Do you mean a literal Lua object, or a TTS object that gets saved to the json?

    Can't wrap my head around saving a two dimensional array (table) of string (GUID) or numeric (coordinates), or combinations of both into an object. I have 7 of those.

    Don't know what I'd do without the advise of people in this forum (like you).

  4. #4
    Join Date
    Jul 2015
    Posts
    159
    Unfortunately we don't have a good way of serializing the state of running Lua scripts at the moment so saving the state of objects with scripts and rewinding the game is not supported.

  5. #5
    LoL
    That's the epiphany I had last night. Knil had asked me for a saved game to investigate an issue I'm seeing. It suddenly hit me that I had to basically give him the image and model folder contents and the workshop (savegame) file. Then I had include instructions on how to play the first couple of rounds until the game reached the point of being able to recreate the issue.

    Slamakan has me thinking though, maybe I can save the running data as a pseudo-script in an otherwise unobtrusive existing object. The best I can do is pack the data into the object's fake lua script at the end of each turn so it'll be saved in the json.

    Ha! May need to ask for a new API function (onSaveGame) to be called immediately before TTS generates a save file.

  6. #6
    Join Date
    Feb 2016
    Location
    Sweden
    Posts
    77
    I was talking about a TTS object.

    What I did was I had a button that created a new object and used setLuaScript() to fill it with the data and an onload() that loads it into the global script.


    Code:
    function save(GO)
        local close = getClosestPlayer(GO.getPosition())
        if close == host_color or close == "Black" then
            updateAccounts()
    
            local p = {}
            p.type = 'Custom_Model'
            p.position = {10, 30, 0}
            save_slot = spawnObject(p)
            a = {}
            a.mesh = 'http://pastebin.com/raw/LPcLCTMu'
            a.diffuse = 'http://puu.sh/np3N2/1aabce9fcb.png'
            save_slot.setCustomObject(a)
    
            local s = "data = {"
            local ad_counter = 0
    
            for name,t in pairs(account_data) do
                s = s .. name .. " = {"
                ad_counter = ad_counter + 1
                t_counter = 0
                for k,v in pairs(t) do
                    t_counter = t_counter + 1
    
                    if t_counter == #t then
                        s = s .. k .. "=" .. v
                    else
                        s = s .. k .. "=" .. v .. ", "
                    end
                end
            end
    
            s = s .. "}"
            s = s .. "\nfunction onload() for k, v in pairs(data) do Global.getVar('account_data')[k] = v end end"
    
            save_slot.setLuaScript(s)
        end
    end
    
    --Removed some parts to make it a bit less confusing, might've removed the wrong parts.
    --The idea is that you write the variables into a variable that will be initialized as the object is spawned, then have it set a variable in the global script on load. I used Global.getVar() because Global.setVar() had issues when I made this.



    Example output:

    Code:
    data = {NameOfOtherPlayer=0, Slamakans=340000000}
    function onload() for k, v in pairs(data) do getGlobalScriptVar('account_data')[k] = v end end



    I wrote a kind of template or something that might work for you

    Code:
    -- 3d array containing 7 2d arrays
    a = {{{'b732da'}, {3, 0, 2}}, {{'d7a47a'}, {'b732da'}}, {{5, -10, 2}, {5, 3, 5}}, {{'da88aa'}, {'bjx8a8'}}, {{'b77a7a'}, {7, 5, 1}}, {{'has53js'}, {'nose321'}}, {{8, 3, 4}, {0, 0, 0}}}
    
    loadedArray = {} -- Will be set in the save TTS object that you spawn
    
    function save()
    	-- create object in save_slot variable
    	local save_slot = spawnObject() etc.
    
    	local i = 0
    	local data = "data = {"
    
    	-- z here will be {{'b732da'}, {3, 0, 2}} at first
    	for _,z in ipairs(a) do
    		i = i + 1
    		data = data .. "{"
    
    		-- x here will be {'b732da'}  first (decided to input single values as arrays for simplicity)
    		-- then it will be {3, 0, 2}
    		local i2 = 0
    		for _,x in ipairs(z) do
    			i2 = i2 + 1
    			data = data .. "{"
    
    			local i3 = 0
    			for _,val in ipairs(x) do
    				i3 = i3 + 1
    				if type(val) == "string" then
    					if i3 == #x then
    						data = data .. "'" .. val .. "'"
    					else
    						data = data .. "'" .. val .. "', "
    					end
    				else
    					if i3 == #x then
    						data = data .. val
    					else
    						data = data .. val .. ", "
    					end
    				end
    			end
    
    			if i2 == #z then
    				data = data .. "}"
    			else
    				data = data .. "}, "
    			end
    		end
    
    		if i == #a then
    			data = data .. "}"
    		else
    			data = data .. "}, "
    		end
    	end
    
    	data = data .. "\nfunction onload() Global.setVar('loadedArray', data) end"
    	-- Haven't tested so might not work
    
    	save_slot.setLuaScript(data)
    end
    
    function onload()
    	loadData()
    end
    
    function loadData()
    	-- Load the data into the proper variables
    end



    Sorry for all of this being so messy, I haven't really spent time trying to make it cleaner.

  7. #7
    @slamakans Using setLuaScript to save the game state is very clever, indeed. I will make note of it.

  8. #8
    Agreed - very sweet.

Posting Permissions

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