ROBLOX LUA(u) 检查函数的参数
ROBLOX LUA(u) check arguments of a function
我有这个功能。
如您所见,我将 fn
定义为 运行 提供参数的函数,如何检查 fn
函数接收的参数数量是否是函数所需的数量 v
?即,如果用户提供了 2 个参数但需要 3 个,则抛出错误。
模块脚本:
-- Variables
local dss = game:GetService("DataStoreService")
local db = dss:GetDataStore("greenwich")
-- Tables
local greenwich = {}
local dbFunctions = {}
-- Functions
function greenwich:GetDB(name)
local new = {}
new.name = name
coroutine.resume(coroutine.create(function()
for k, v in pairs(dbFunctions) do
local fn = function(...)
local args = {...}
return v(unpack(new), unpack(args))
end
new[k] = fn
new[string.lower(k)] = fn
end
end))
return new
end
function dbFunctions:Set(store, key, value)
store = store.name
db:SetAsync(store .. key, value)
return value
end
function dbFunctions:Get(store, key)
store = store.name
return db:GetAsync(store .. key)
end
function dbFunctions:Delete(store, key)
store = store.name
local success, val = pcall(function()
return db:RemoveAsync(store .. key)
end)
if val and success then
return true
else
return false
end
end
function dbFunctions:Has(store, key)
store = store.name
return not not db:GetAsync(store .. key)
end
-- Returning everything.
return greenwich
在Lua5.3.5的标准库中,可以使用debug.getInfo()函数查看函数。返回的 table 包含一个名为 nparams
的字段,它将告诉您该函数需要多少个参数。
local example = {}
function example.func(a, b, c)
print(a, b, c)
end
local info = debug.getinfo(example.func)
print(info.nparams) -- 3
在Roblox Lua中,这是一个基于Lua 5.1的自定义版本,调试库被大量修改,您需要使用debug.info()功能。当你传入一个函数和参数“a”时,它 returns 函数的参数。
local example = {}
function example.funcA(a, b, c)
print(a, b, c)
end
function example:funcB(a, b, c)
print(a, b, c)
end
function example:funcC(a, b, c, ...)
print(a, b, c)
end
-- print out the number of args and whether there's a vararg
print(debug.info(example.funcA, "a")) -- 3 false
print(debug.info(example.funcB, "a")) -- 4 false
print(debug.info(example.funcC, "a")) -- 4 true
我有这个功能。
如您所见,我将 fn
定义为 运行 提供参数的函数,如何检查 fn
函数接收的参数数量是否是函数所需的数量 v
?即,如果用户提供了 2 个参数但需要 3 个,则抛出错误。
模块脚本:
-- Variables
local dss = game:GetService("DataStoreService")
local db = dss:GetDataStore("greenwich")
-- Tables
local greenwich = {}
local dbFunctions = {}
-- Functions
function greenwich:GetDB(name)
local new = {}
new.name = name
coroutine.resume(coroutine.create(function()
for k, v in pairs(dbFunctions) do
local fn = function(...)
local args = {...}
return v(unpack(new), unpack(args))
end
new[k] = fn
new[string.lower(k)] = fn
end
end))
return new
end
function dbFunctions:Set(store, key, value)
store = store.name
db:SetAsync(store .. key, value)
return value
end
function dbFunctions:Get(store, key)
store = store.name
return db:GetAsync(store .. key)
end
function dbFunctions:Delete(store, key)
store = store.name
local success, val = pcall(function()
return db:RemoveAsync(store .. key)
end)
if val and success then
return true
else
return false
end
end
function dbFunctions:Has(store, key)
store = store.name
return not not db:GetAsync(store .. key)
end
-- Returning everything.
return greenwich
在Lua5.3.5的标准库中,可以使用debug.getInfo()函数查看函数。返回的 table 包含一个名为 nparams
的字段,它将告诉您该函数需要多少个参数。
local example = {}
function example.func(a, b, c)
print(a, b, c)
end
local info = debug.getinfo(example.func)
print(info.nparams) -- 3
在Roblox Lua中,这是一个基于Lua 5.1的自定义版本,调试库被大量修改,您需要使用debug.info()功能。当你传入一个函数和参数“a”时,它 returns 函数的参数。
local example = {}
function example.funcA(a, b, c)
print(a, b, c)
end
function example:funcB(a, b, c)
print(a, b, c)
end
function example:funcC(a, b, c, ...)
print(a, b, c)
end
-- print out the number of args and whether there's a vararg
print(debug.info(example.funcA, "a")) -- 3 false
print(debug.info(example.funcB, "a")) -- 4 false
print(debug.info(example.funcC, "a")) -- 4 true