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 :)
Using the Datastorage Mod
-
- Member
- Posts: 33
- Joined: Fri Oct 10, 2014 13:06
- addi
- Member
- Posts: 666
- Joined: Thu Sep 20, 2012 03:16
- GitHub: adrido
- Location: Black-Forest, Germany
Re: Using the Datastorage Mod
i would recomend you using my datastorage mod instead, its more easier
viewtopic.php?f=9&t=9276
viewtopic.php?f=9&t=9276
-
- Member
- Posts: 33
- Joined: Fri Oct 10, 2014 13:06
Re: Using the Datastorage Mod
Thanks Addi, I actually found your mod yesterday but I'll try it out later today :)
- rubenwardy
- Moderator
- Posts: 6978
- Joined: Tue Jun 12, 2012 18:11
- GitHub: rubenwardy
- IRC: rubenwardy
- In-game: rubenwardy
- Location: Bristol, United Kingdom
- Contact:
Re: Using the Datastorage Mod
You don't have to use a mod, you can do it yourself like this:
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()
-
- Member
- Posts: 33
- Joined: Fri Oct 10, 2014 13:06
Re: Using the Datastorage Mod
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 { }
return { }
- rubenwardy
- Moderator
- Posts: 6978
- Joined: Tue Jun 12, 2012 18:11
- GitHub: rubenwardy
- IRC: rubenwardy
- In-game: rubenwardy
- Location: Bristol, United Kingdom
- Contact:
Re: Using the Datastorage Mod
Try this:
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)
-
- Member
- Posts: 33
- Joined: Fri Oct 10, 2014 13:06
Re: Using the Datastorage Mod
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 :)
Who is online
Users browsing this forum: No registered users and 6 guests