0

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?

6
  • I'm not entirely sure what you are asking. Can you clarify/paraphrase your question? Commented Jan 5, 2017 at 20:20
  • Uh, so I want to replace function eventEmotePlayed with ifEmotePlayed, and I will may use it same as eventEmotePlayed.. uh hard to explain :L Commented Jan 5, 2017 at 20:22
  • Something like local foo = function() print('first') end foo = function() print('second') end? That replaces foo with the second function definition. Commented Jan 5, 2017 at 20:25
  • Simple assignment should do the "replacement": eventEmotePlayed = ifEmotePlayed Commented Jan 5, 2017 at 20:38
  • 1
    I will never understand why everybody thinks it's LUA. Nowhere is it ever formally called LUA :/ Commented Jan 6, 2017 at 3:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.