I wanted to register a command in one of my addons to essentially do a complete reset of all its data, because I was a tard and released the addon before the internal data structures were managed correctly. And I figured why not make it something that other addons can piggyback on, just because it would be a fun exercise? So I did, and I got zx64 to look it over, and we concluded that it was crap.
So then zx64 suggested why not just use an event, and this is what I came up with:
Code: Select all
-- Version 1 by folk, zx64
-- To the extent possible under law, the authors have waived all copyright and related or neighboring rights to lib-reset.lua.
-- http://creativecommons.org/publicdomain/zero/1.0/
-- In laymans terms: "do whatever you want with this file and its content"
-- Credits: folk, zx64
--[[
USAGE DESCRIPTION
Somewhere in the control scope of your addon:
local function reset(event)
if not event or (not event.all and event.addon ~= "myaddon") then return end
reinitializeAllMyStuffFromScratchLikeItsANewGameOrWhatever()
for _, force in pairs(game.forces) do
for name, tech in pairs(force.technologies) do
if tech.valid and name == "mytech" and tech.researched then
enableTurtlesAllTheWayDown()
end
end
end
end
require("lib-reset")(reset)
And then, ingame, players will be able to execute from the console:
To reset your addon specifically: /reset myaddon
To reset all addons registered with the library: /reset
--]]
local MAJOR, MINOR, register = "lib-reset", 1, true
local eventId
if remote.interfaces[MAJOR] then
local newId = remote.call(MAJOR, "getEventId")
if type(newId) ~= "nil" then eventId = newId
else error("Previous version of lib-reset did not pass on the registered event ID.") end
local version = remote.call(MAJOR, "version")
if type(version) == "number" and version <= MINOR then register = false
else
local cmd = remote.call(MAJOR, "registeredCommand")
if commands and type(cmd) == "string" then _G.commands.remove_command(cmd) end
remote.remove_interface(MAJOR)
print("More recent version of lib-reset has been detected.") --stdout print in case someone notices
end
end
if register then
local usedCommand
local notification = "%s ran a /reset addons command."
if type(eventId) ~= "number" then eventId = script.generate_event_name() end
local function runReset(input)
if type(input.parameter) == "string" then input.addon = input.parameter
else input.all = true end
script.raise_event(eventId, input)
if game and game.print then
if type(input) == "table" and input.player_index then game.print({"", notification:format(game.players[input.player_index].name)})
else game.print({"", "Someone ran a /reset addons command."}) end
end
end
remote.add_interface(MAJOR, {version=function() return MINOR end,registeredCommand=function() return usedCommand end, getEventId=function() return eventId end})
if commands then
for _, c in next, {"reset", "resetmod", "resetmods", "resetaddon", "resetaddons", "rstmds", "rstadd"} do
if not commands.commands[c] and not commands.game_commands[c] then
usedCommand = c
commands.add_command(c, "", runReset)
break
end
end
end
end
return function(f) script.on_event(eventId, f) end
hastebin.com syntax highlighted lua: https://hastebin.com/ayuqerehos.lua
The library, as you can see, is in the public domain, so anyone can just drop it in their package and register with it.
If the library is found in more than one addon, all addons that use it get automatically upgraded to the highest version found.
The overhead from using it is insignificant.
At the same time, it doesn't really offer many uses that would be hard without it; I just had fun making it.
So, if you dont want to read the code above, which does contain docs on how to use it, here is how:
1. Save it as lib-reset.lua in your addon
2. In control.lua, create a function that takes one argument that does whatever you want to happen if someone types /reset
3. Do require("lib-reset")(myFunctionReference)
Your function will get one argument (call it 'event') that has two keys that are of interest; .all and .addon
.all is set to boolean=true the user simply types /reset, and is nil if not.
.addon is set to whatever the user types after /reset, so "/reset foobar" becomes .addon == "foobar", and is nil if not.
The actual /-command used by the library depends what is available, but it tries to register for these, in prioritized order:
"reset", "resetmod", "resetmods", "resetaddon", "resetaddons", "rstmds", "rstadd"
I don't claim this is useful, but it was fun to write, so hopefully someone can make use if it.
On the heels of this, zx64 lamented that we can't have string'd event names, so I made another library for that; lib-events. Also just for fun.
EDITS:
#1: 02.05.17: Added link to lib-events thread.