Using the Datastorage Mod

LoseTheGame
Member
 
Posts: 33
Joined: Fri Oct 10, 2014 13:06

Using the Datastorage Mod

by LoseTheGame » Tue Oct 28, 2014 14:38

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 :)
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: Using the Datastorage Mod

by addi » Wed Oct 29, 2014 02:05

i would recomend you using my datastorage mod instead, its more easier
viewtopic.php?f=9&t=9276
 

LoseTheGame
Member
 
Posts: 33
Joined: Fri Oct 10, 2014 13:06

Re: Using the Datastorage Mod

by LoseTheGame » Wed Oct 29, 2014 11:31

Thanks Addi, I actually found your mod yesterday but I'll try it out later today :)
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Using the Datastorage Mod

by rubenwardy » Wed Oct 29, 2014 11:47

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()
 

LoseTheGame
Member
 
Posts: 33
Joined: Fri Oct 10, 2014 13:06

Re: Using the Datastorage Mod

by LoseTheGame » Wed Oct 29, 2014 16:50

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 { }
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Using the Datastorage Mod

by rubenwardy » Wed Oct 29, 2014 17:39

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)
 

LoseTheGame
Member
 
Posts: 33
Joined: Fri Oct 10, 2014 13:06

Re: Using the Datastorage Mod

by LoseTheGame » Wed Oct 29, 2014 22:03

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 :)
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 5 guests

cron