Reload Lua files on the fly.
![Post Post](./styles/mt_v1/imageset/icon_post_target.gif)
While thinking about how to better test my mapgen, I had the idea that it would be neat if I'd be able to reload parts of it on the fly, without the need to exit the subgame and restart it. There is no direct facility in Minetest that allows it, but we can work around it with a chatcommand and by encapsulating the important code in a function or object. Obviously you can't reload everything, as there are things that just need to be known when the mod starts, but other parts can be reloaded.
Let's have a look at the basics on how to accomplish this, assume this to be in the init.lua:
And that's the basic framework, now you can run the command "reload" from within Minetest and "dynamic.lua" will be reloaded, you can make changes to "dynamic.lua" and simply reload it without having to exit Minetest.
You could also do that with a mapgen system, for example (note that the code assumes my utils mod to shorten it):
Now you can make changes to the system, type "reload" in Minetest and the next piece of the world is generated the new, reloaded system will be used.
Let's have a look at the basics on how to accomplish this, assume this to be in the init.lua:
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 base_path = minetest.get_modpath(minetest.get_current_modname())
minetest.register_chatcommand("reload", {
description = "Reloads all we need.",
params = "",
func = function(name, params)
dofile(base_path .. "/dynamic.lua")
return true, "Done"
end
})
And that's the basic framework, now you can run the command "reload" from within Minetest and "dynamic.lua" will be reloaded, you can make changes to "dynamic.lua" and simply reload it without having to exit Minetest.
You could also do that with a mapgen system, for example (note that the code assumes my utils mod to shorten it):
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 base_path = minetest.get_modpath(minetest.get_current_modname())
dofile(base_path .. "/mapgen.lua")
-- MapGen object is defined in mapgen.lua.
local mapgen = MapGen.create()
minetest.register_chatcommand("reload", {
description = "Reloads all we need.",
params = "",
func = function(name, params)
-- Reload the file.
dofile(base_path .. "/mapgen.lua")
-- Now we have the "new" MapGen object that we can use.
mapgen = MapGen.create()
return true, "Done"
end
})
minetest.register_on_generated(function(minp, maxp, seed)
local manipulator = MapManipulator:new()
mapgen:run(minp, maxp, manipulator)
manipulator:set_data()
end)
Now you can make changes to the system, type "reload" in Minetest and the next piece of the world is generated the new, reloaded system will be used.