Skip to content

Instantly share code, notes, and snippets.

@Zbizu
Zbizu / tableClone.lua
Last active January 19, 2022 19:01
[Lua] Clone a table without references
-- creates a full copy of a table
-- changing the value of copied table doesn't touch the original anymore
-- warning: using methods on "copied" userdata will still affect original object
local a = {
["abc"] = {1, 2},
[1] = {4, {2}}
}
function cloneTable(t)
@Zbizu
Zbizu / dumptable.lua
Last active June 13, 2022 11:17
dump lua table contents to string
-- usage: dumpTable(t)
-- to dump to file:
--[[
local str = dumpTable(t)
local f = io.open("dumped_table.txt", "w+")
f:write(str)
f:close()
]]
local function getTabulation(depth)
@Zbizu
Zbizu / ClassExample.lua
Created June 22, 2021 19:00
lua "class" example
local Class = setmetatable ({ },
{
__call = function (class, ...)
return class.new (...)
end
}
)
Class.__index = Class
Class.__call = function (self)
@Zbizu
Zbizu / ClassWithAutoMethods.lua
Created June 22, 2021 19:46
automated getter/setter generation example for lua "classes"
local defaults = {
name = "",
words = "",
mana = 0,
manaPercent = 0,
soul = 0,
spellId = 0,
range = -1,
level = 0,
magLevel = 0,
@Zbizu
Zbizu / matrix.lua
Created June 23, 2021 17:48
matrix operations in lua
local matrixFields = {
area = {}
}
local Matrix = setmetatable(matrixFields,
{
__call = function(matrix, ...)
return matrix.new(...)
end
}
@Zbizu
Zbizu / animatedTextTesting.lua
Created June 24, 2021 19:04
animated text for TFS 1.3 and HP bars bonus
-- who is showing changes and who is watching
spectated = Player("player")
spectator = Player("Administrator")
-- animated text, otclient only (or old clients)
--------------------------------
msg = NetworkMessage()
msg:addByte(0x84)
@Zbizu
Zbizu / lagLessSave.lua
Created June 26, 2021 21:24
less laggy player saving (tfs 1.3 or a fork that supports revscriptsys)
local noLagSave = GlobalEvent("noLagSave")
local timeBetweenSaves = 1 * 60 * 60 * 1000 -- in milliseconds
local delayBetweenSaves = 100
function delayedSave(cid)
local p = Player(cid)
if p then
p:save()
end
end
@Zbizu
Zbizu / getOS.lua
Last active April 2, 2024 07:55
operating system (OS) detection in Lua
function getOS()
-- ask LuaJIT first
if jit then
return jit.os
end
-- Unix, Linux variants
local fh,err = assert(io.popen("uname -o 2>/dev/null","r"))
if fh then
osname = fh:read()
@Zbizu
Zbizu / loopEvent.lua
Created July 2, 2021 17:18
reload safe addEvent loop example
-- configuration
LOOP_EVENT_DELAY = 5 * 1000 -- (in milliseconds)
-- register list of affected creatures and protect them from reload
if not LOOP_EVENT_REGISTERED_CREATURES then
LOOP_EVENT_REGISTERED_CREATURES = {}
end
-- store session id to reload the loop without duplicating it
if LOOP_EVENT_SESSION_ID then
@Zbizu
Zbizu / getVariables.lua
Last active December 24, 2023 13:38
get all Lua functions (and globals)
-- usage: just call it in a Lua environment and look for a file called out.txt
-- lists all global variables in a file
function writeAllGlobals()
local file = io.open("out.txt", "w+")
local seen={}
local function dump(t,i)
seen[t]=true
local s={}