mymonths/functions.lua

59 lines
1.2 KiB
Lua
Raw Normal View History

2015-11-13 07:04:45 -08:00
-- Save table to file
function mymonths.save_table()
2015-11-13 07:04:45 -08:00
local data = mymonths
local f, err = io.open(minetest.get_worldpath() .. "/mymonths_data", "w")
if err then
return err
end
2015-11-13 07:04:45 -08:00
f:write(minetest.serialize(data))
f:close()
end
-- Reads saved file
function mymonths.read_mymonths()
local f, err = io.open(minetest.get_worldpath() .. "/mymonths_data", "r")
2015-11-13 07:04:45 -08:00
local data = minetest.deserialize(f:read("*a"))
2015-11-13 07:04:45 -08:00
f:close()
2015-11-13 07:04:45 -08:00
return data
end
-- Saves the table every 10 seconds
local tmr = 0
2015-11-13 07:04:45 -08:00
minetest.register_globalstep(function(dtime)
tmr = tmr + dtime
2015-11-13 07:04:45 -08:00
if tmr >= 10 then
2015-11-13 07:04:45 -08:00
tmr = 0
2015-11-13 07:04:45 -08:00
mymonths.save_table()
end
end)
-- Check to see if file exists and if not sets default values.
2015-11-13 07:04:45 -08:00
local f, err = io.open(minetest.get_worldpath().."/mymonths_data", "r")
2015-11-13 07:04:45 -08:00
if f == nil then
mymonths.day_counter = 1
mymonths.month_counter = 6
mymonths.month = "June"
mymonths.weather = "clear"
mymonths.day_name = "Monday"
2015-11-13 07:04:45 -08:00
else
mymonths.day_counter = mymonths.read_mymonths().day_counter
mymonths.month_counter = mymonths.read_mymonths().month_counter
mymonths.month = mymonths.read_mymonths().month
mymonths.weather = mymonths.read_mymonths().weather
mymonths.day_name = mymonths.read_mymonths().day_name
2015-11-13 07:04:45 -08:00
end