Page 1 of 1

Using the Datastorage Mod

PostPosted: Tue Oct 28, 2014 14:38
by LoseTheGame
Hi guys,

I can't figure out how to use the datastorage mod based on the README:

https://github.com/minetest-technic/datastorage

All I need to know for now is how to save a value so that I can access it after quitting and restarting the game.

Thanks :)

Re: Using the Datastorage Mod

PostPosted: Wed Oct 29, 2014 02:05
by addi
i would recomend you using my datastorage mod instead, its more easier
viewtopic.php?f=9&t=9276

Re: Using the Datastorage Mod

PostPosted: Wed Oct 29, 2014 11:31
by LoseTheGame
Thanks Addi, I actually found your mod yesterday but I'll try it out later today :)

Re: Using the Datastorage Mod

PostPosted: Wed Oct 29, 2014 11:47
by rubenwardy
You don't have to use a mod, you can do it yourself like this:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local data = {}
local data_filename = minetest.get_worldpath().."/filename.txt"

local function load_data()
   local file = io.open(data_filename, "r")
   if file then
      local table = minetest.deserialize(file:read("*all"))
      file:close()
      if type(table) == "table" then
         data = table
         return
      end
   end   
end

local function save_data()
   local file = io.open(data_filename, "w")
   if file then
      file:write(minetest.serialize(data))
      file:close()
   end
end

load_data()

Re: Using the Datastorage Mod

PostPosted: Wed Oct 29, 2014 16:50
by LoseTheGame
Thanks Ruben, that would be ideal if I could do it without using a mod. Could you give me a few pointers for how to get this to work? I have an integer variable called "gold" that stores how many points the character has. I've pasted in your code and made it so when the amount of gold changes it runs save_data(). Nothing seems to be being saved though and in the text file (I've called it datastorage.txt) all I can see is:

return { }

Re: Using the Datastorage Mod

PostPosted: Wed Oct 29, 2014 17:39
by rubenwardy
Try this:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local function load_data(filename)
   local file = io.open(filename, "r")
   if file then
      local table = minetest.deserialize(file:read("*all"))
      file:close()
      if type(table) == "table" then
         return table
      end
   end   
end

local function save_data(filename, data)
   local file = io.open(filename, "w")
   if file then
      file:write(minetest.serialize(data))
      file:close()
   end
end

local mydata = load_data(minetest.get_worldpath().."/filename.txt") or {} -- loads file if it exists, or makes empty table
mydata.foo = "bar"
save_data(minetest.get_worldpath().."/filename.txt", mydata)

Re: Using the Datastorage Mod

PostPosted: Wed Oct 29, 2014 22:03
by LoseTheGame
That's working, thanks a lot! I'm making a mod for my students to learn about adding fractions so they'll be very happy :)