user7360436
user7360436

Reputation:

Creating function from available function

I'm trying to do script for game which replaces avalible function with created function. So this is my LUA code:

function ifEmotePlayed(playerName, emoteID)
if emoteID == 0 then
print(playerName.." is dancing!")
end
end
return 
function eventEmotePlayed(playerName, emoteID)
end

Exactly thing i want to do is to replace eventEmotePlayed function with ifEmotePlayed function likely

function ifEmotePlayed(playerName, emoteID)
if emoteID==0 then
print(playerName.." is dancing!")
end
end

Instead of

function eventEmotePlayed(playerName, emoteID)
if emoteID==0 then
print(playerName.." is dancing!")
end
end

Does anyone know how to do it?

Upvotes: 0

Views: 55

Answers (1)

Piglet
Piglet

Reputation: 28940

If you want to rename a function you simply do it like that:

myNewFunctionName = originalFunctionName

Then calling myNewFunctionName() will be identical to calling originalFunctionName() as myNewFunctionName now refers to the same function as originalFunctionName.

In Lua functions are variables.

You could also define a function that calls the original function and pass the parameters like:

function myNewFunction(a,b)
  return originalFunction(a,b)
end

But this is obviously not as efficient as you have to do one additional function call.

If you want to replace a function with your own function so the original function will not be executed but yours instead you simply assign your function to the original functions "name" (actually you make the variable that referred to the original function from now on refer to your own function)

function originalFunction(a,b)
  return a + b
end

function myOwnFunction(a,b)
  return a * b
end

originalFunction = myOwnFunction

Now originalFunction refers to your function and calling originalFunction(a,b) will return the product of a and b instead of the sum. It's the same as in the first example, just the other way around.

Upvotes: 1

Related Questions