Jump to content

Save and Load data with LUA


Phodex Games
 Share

Recommended Posts

Hello,

 

Because I made myself some thoughts about a saving mechanic for my current project, I searched the web and especially the Leadwerks site for how to save simple game data, like the players health or position. The only kind of useful thing I found which works for LUA was this: http://www.leadwerks.com/werkspace/topic/2103-save-data-to-lua-syntax-as-textcompressed/page__hl__save+data

 

and this:

http://www.leadwerks.com/werkspace/topic/1853-howto-using-lua-script-as-data-file/

 

I actually was able to create a data file and read it into my code with "dofile". However I didnt find out how to create/save a file with lua yet. I searched the Leadwerks API and found the Stream and FileSystem Class.

 

So I tried the following code:

 

function Script:Start()
local gamedata = FileSystem:WriteFile("Test.txt")
end

 

It didnt work, and I also dont know how it works. Where has the file been created? I searched my PC and the Projectsfolder but couldnt find such a file and I also dont get any kind of error message.

I guess this is completely wrong, but I was just playing around, because I don't have that much experience with Lua and Leadwerks. The file you can download in the first thread I posted ("TStoreData.lua") doesnt work anymore and I cant find where he saves data.

 

That you clearly undersand what I want. I would like to do something like this:

 

function Script:Start()
local gamedata = {}
gamedata.playerHealth = 100
gamedata.playerWeapon = "Sword"
gamedata.playerPos = Vec3(10, 100, 53)
FileSystem:WriteFile("gamedata.lua", gamedata)
end

 

This is just some code that you know what I mean. I know this doesnt work xD. So could you please help me out a little bit smile.png.

 

PS.: I kind of understood what AndyGFX wrote in his post, I just couldnt find anything how to save a file.

Link to comment
Share on other sites

The syntax is wrong for FileSystem:WriteFile(). The API example shows how to properly do this.

 

But for your example, it would be like this:

function Script:Start()

local gamedata = {}

gamedata.playerHealth = 100

gamedata.playerWeapon = "Sword" --Note the quotes

gamedata.playerPos = Vec3(10, 100, 53)

local stream = FileSystem:WriteFile("gamedata.lua")

stream:WriteLine(gamedata.playerHealth)

stream:WriteLine(gamedata.playerWeapon)

stream:WriteLine(gamedata.playerPos:ToString())

end

 

But this method will only write the value and not the key that is associated with that value. Another way to do this (as there are many), is to use System:SetProperty(key, value) which will write the key and value into the game's config file. Then use System:GetProperty(key) to read the value.

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

The syntax is wrong for FileSystem:WriteFile(). The API example shows how to properly do this.

 

But for your example, it would be like this:

function Script:Start()

local gamedata = {}

gamedata.playerHealth = 100

gamedata.playerWeapon = "Sword" --Note the quotes

gamedata.playerPos = Vec3(10, 100, 53)

local stream = FileSystem:WriteFile("gamedata.lua")

stream:WriteLine(gamedata.playerHealth)

stream:WriteLine(gamedata.playerWeapon)

stream:WriteLine(gamedata.playerPos:ToString())

end

 

But this method will only write the value and not the key that is associated with that value. Another way to do this (as there are many), is to use System:SetProperty(key, value) which will write the key and value into the game's config file. Then use System:GetProperty(key) to read the value.

 

I know that my code was wrong, it was just for visualization purpose. However the code you sent me, didnt work, it says stream is a nil value. That was what I wrote before, FileSystem:WriteFile() doesnt seem to work...

 

However System:SetProperty() works fine :) and thats more what I searched for, but I would also like to know how to save new file, because I maybe would like to have various save files in the future...

Link to comment
Share on other sites

That was what I wrote before, FileSystem:WriteFile() doesnt seem to work...

 

No idea that your code was just for visualizing so assumed that the issue was the incorrect syntax. And Genebris is correct, for any of the FileSystem commands that write/delete files/directories (and any lua IO functions), the lua sandbox must be disabled. Just keep in mind that disabling the lua sandbox will not work for any game you make for the Leadwerks Game Launcher.

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

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...