Jump to content

Save game feature: where do I put it in?


Kenneth Nyström
 Share

Recommended Posts

So I have been putting this step off for some time.
"the save function."
...
I have this basic idea how and where it should be implemented:
But the Leadwerks "index" for the save function is to say the least lacking regardng the "SaveData" and "LoadData"
so I am actully stuck in my endevours to proceded with my game´s save/load function.

Especially the part with how to "save" the map: one is currently on within the game, so when the load funtion is activated.

the "correct" map is loaded,

I strongly suspect:

"SetCurrent" & "GetCurrent" (world) is the way to go.
but how should it be implimented?

Having the hardest time to figure out what the save part should look like to include the GetCurrent (world)
................................................................................................

 

function Script:SaveData()
    count1 = (count1 + 1)
  
    prefab = self.entity:GetKeyValue("prefab")
   
    pos = self.entity:GetPosition()
    rot = self.entity:GetRotation()
    world =????????           --is it? self.entity:GetCurrent (world)


    local entityTable ={}
    
    entityTable.posx = pos.x
    entityTable.posy = pos.y
    entityTable.posz = pos.z
    entityTable.rotx = rot.x
    entityTable.roty = rot.y
    entityTable.rotz = rot.z
    entityTable.prefab = prefab
   --is it also? entityTable.world =world

    
    return entityTable
end

.................................................................................................
Let me see if I have gotten the basic idea of what the load script should say:
...................................................................................................

function Script:LoadData(data)

    r1 = data.rotx
    r2    = data.roty
    r3    = data.rotz
    p1 = data.posx
    p2    = data.posy
    p3    = data.posz
   world = data.mapname     --is this correct?

    self.entity:SetCurrent (world)    
    self.entity:SetRotation(r1,r2,r3)
    self.entity:SetPosition(p1,p2,p3)
    System:Print('Entity Loaded')
end

....................
Any pointers much appreciated as I am getting back into headspinning mode (again) but something tells me I might be getting closer to what I want to achive with my save/load scripts.

Link to comment
Share on other sites

You should save to disk only what is needed to recreate the state you where in.

For example say you want to remember player position and the current map:

{ x = 0, y = 0, z = 0, currentmap= "level2" }

Save this to disk.

Load function at start of game.Set player position and load the level from the disk data.

Etc

Start small think what is needed to be saved. And do some small tests.

 

Ways to save to disk:

This lib can help by serialize lua tables:

https://github.com/pkulchenko/serpent

 

Other option is to use api provided by leadwerks:

https://www.leadwerks.com/learn?page=API-Reference_Object_Stream

 

Another solution is to use sqlite.

 

I made this with Leadwerks/UAK:

Structura Stacky Desktop Edition

Website:

Binary Station

Link to comment
Share on other sites

7 hours ago, aiaf said:

You should save to disk only what is needed to recreate the state you where in.

For example say you want to remember player position and the current map:

{ x = 0, y = 0, z = 0, currentmap= "level2" }

Save this to disk.

Load function at start of game.Set player position and load the level from the disk data.

Etc

Start small think what is needed to be saved. And do some small tests.

 

Ways to save to disk:

This lib can help by serialize lua tables:

https://github.com/pkulchenko/serpent

 

Other option is to use api provided by leadwerks:

https://www.leadwerks.com/learn?page=API-Reference_Object_Stream

 

Another solution is to use sqlite.

 

Yeah I am thinking as small as possible; I only really need 
1: what map was the player on: 
2: optional------------(the position) (more then enough with just the normal: map-spawn- in-point) 
3: two table values;  thats cruicial to the "where in the game progress the player is" 
4: save to disk
5 load when game starts.

But I am absolutly CLUELESS where to begin. = I cant get anything to even begin make sense without a starting point where the save scripts is to be put.
(there are no tutorials to be found)
I can (in my mind) see how I only need like 5 or 6 lines of code to make this to be solved

----------------
Something like this:
..................

function Script:SaveData()                      --This is to get the function goin.
    count1 = (count1 + 1)                          --This I hope is to name the save files diffrently each time a save is done

  {currentmap= "self.mapname" }    --This line of code should possibly incl, the two table values of "lets call them "mylevel" & "mybuildlevel"

    local entityTable ={}
    {mylevel= "self.mylevel" mybuildlevel ="self.mybuildlevel" }
    return entityTable
end

.................................................................................................
Let me see if I have gotten the basic idea of what the load script should say:
...................................................................................................

function Script:LoadData(data) --This is to have the load scrip going. (Game comes with a first "save" that has the starting values going                                                    --the first time it is started up; before the game saves its own save file for the first time)

                                              --here I suspect a line of code needs to be put in to identify the latest save; so game knows what file to                                                    --load

currentmap = ""                     --the saved value above in save file
mylevel =  ""                          --the saved value above in save file
mybuildlevel = ""                     --the saved value above in save file

 end

....................

Any help to move me forward will be very much appreciated.



 

Link to comment
Share on other sites

Save serpent.lua i linked earlier and Serializer.lua below.

Serializer = {serpent = {}}

function Serializer:new (o)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   local lserpent = require("serpent")
   self.serpent = lserpent
   return o
end

function Serializer:save(data , filename)
    local stream = FileSystem:WriteFile(filename)
	stream:WriteLine(self.serpent.dump(data))
	stream:Release() 
end

function Serializer:getSerpent()
    return self.serpent
end

 

First decide when you want to save your data.

And since you dont get it yet, do a test use just a save file (call it savefile) to save the current map name.

 

When you load your map or when you change the map save the name in a variable (mapname)

--this table is the data you want to save
gamedata = {"currentMapName" = mapname}

I assume you have some kind of ui , make a button save , and put your save code in the on click handler.Or whatever when you click S key.

gser = Serializer:new()
gser:Save(gamedata, "savefile")

If you look on disk you will have the savefile with your data.

 

And this is the load function you need to call at start game

function load()
	local stream = FileSystem:ReadFile("savefile")
	local data = ""
	if (stream) then
		data = stream:ReadLine(); 
		stream:Release() 
	end
	local fun, err = loadstring(data)
	if err then error(err) end
	return fun()
end

Load data:

gdata = load()

so you say mapname = gdata ["currentMapName"]

then load the saved map.

I made this with Leadwerks/UAK:

Structura Stacky Desktop Edition

Website:

Binary Station

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...