Lua 从可用函数创建函数
我正在尝试为游戏编写脚本,用创建的函数替换可用函数。这是我的LUA代码:Lua 从可用函数创建函数,lua,Lua,我正在尝试为游戏编写脚本,用创建的函数替换可用函数。这是我的LUA代码: function ifEmotePlayed(playerName, emoteID) if emoteID == 0 then print(playerName.." is dancing!") end end return function eventEmotePlayed(playerName, emoteID) end 我想做的就是用ifEmotePlayed函数替换eventEmotePlayed函数 fun
function ifEmotePlayed(playerName, emoteID)
if emoteID == 0 then
print(playerName.." is dancing!")
end
end
return
function eventEmotePlayed(playerName, emoteID)
end
我想做的就是用ifEmotePlayed函数替换eventEmotePlayed函数
function ifEmotePlayed(playerName, emoteID)
if emoteID==0 then
print(playerName.." is dancing!")
end
end
而不是
function eventEmotePlayed(playerName, emoteID)
if emoteID==0 then
print(playerName.." is dancing!")
end
end
有人知道怎么做吗?如果要重命名函数,只需这样做:
myNewFunctionName = originalFunctionName
然后调用myNewFunctionName()
将与调用originalFunctionName()
相同,因为myNewFunctionName
现在引用的函数与originalFunctionName
相同
在Lua中,函数是变量
您还可以定义一个调用原始函数并传递如下参数的函数:
function myNewFunction(a,b)
return originalFunction(a,b)
end
但是,这显然不如您必须执行一个额外的函数调用那么有效
如果您想用自己的函数替换一个函数,这样原始函数将不会被执行,而是您的函数,您只需将您的函数分配给原始函数“name”(实际上,从现在起,您将引用原始函数的变量指定给您自己的函数)
现在
originalFunction
引用您的函数,调用originalFunction(a,b)
将返回a
和b
的乘积,而不是总和。这与第一个示例中的相同,只是相反。我不完全确定您在问什么。你能澄清/解释你的问题吗?嗯,我想用ifEmotePlayed替换函数eventEmotePlayed,我可能会使用与eventEmotePlayed相同的函数。。嗯,很难解释:像本地foo=function()print('first')end foo=function()print('second')end这样的东西?这将用第二个函数定义替换foo。简单赋值应该可以“替换”:eventEmotePlayed=ifEmotePlayed
我永远不会理解为什么每个人都认为它是LUA。它从未正式被称为LUA:/
function originalFunction(a,b)
return a + b
end
function myOwnFunction(a,b)
return a * b
end
originalFunction = myOwnFunction