ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Passing parameter to a callback function (https://www.esoui.com/forums/showthread.php?t=9215)

QuantumPie 06/14/20 11:40 AM

Passing parameter to a callback function
 
When using a callback function with RegisterForEvent, is there a way I can specify a parameter that the callback uses? For example:

Lua Code:
  1. EVENT_MANAGER:RegisterForEvent(BarTracker.name, EVENT_ACTION_LAYER_PUSHED, BarTracker.toggleVisibility)
  2.  
  3. function BarTracker.toggleVisibility(hidden)
  4.     BarTracker.guiWindow:SetHidden(hidden)
  5. end

I can't simply pass BarTracker.toggleVisibility(true) as the 3rd parameter since that immediately invokes the function. In JavaScript, I could use bind or the syntax (() => BarTracker.toggleVisibility(true)) since that won't invoke it, but still passes the parameter when the callback is called. Does Lua have something similar? I don't know exactly what this is called so I couldn't find anything on SO.

Shinni 06/14/20 12:06 PM

EVENT_MANAGER:RegisterForEvent(BarTracker.name, EVENT_ACTION_LAYER_PUSHED, function() BarTracker.toggleVisibility(true) end)

It's basically shorthand for something like below. The only difference is that in the code below, the function has the name TurnOffVisibility, while in the code above, the function is anonymous.

Lua Code:
  1. local function TurnOffVisibility()
  2.     BarTracker.toggleVisibility(true)
  3. end
  4. EVENT_MANAGER:RegisterForEvent(BarTracker.name, EVENT_ACTION_LAYER_PUSHED, TurnOffVisibility)

I suppose "function() BarTracker.toggleVisibility(true) end" might be the same as your JavaScript example? Is "(() => BarTracker.toggleVisibility(true))" a function?

sirinsidiator 06/14/20 12:14 PM

Quote:

Originally Posted by Shinni (Post 41525)
I suppose "function() BarTracker.toggleVisibility(true) end" might be the same as your JavaScript example? Is "(() => BarTracker.toggleVisibility(true))" a function?

There is no equivalent to lambda functions in Lua, so the most direct comparison would simply be an anonymous function in js:
Code:

RegisterSomething(function() {
  DoSomething(true);
});

You may think they do the same, but as with many things in JS there are fine and easily overlooked differences that will make your life as a developer a living hell if you don't know about them. ;)

QuantumPie 06/14/20 01:07 PM

Thanks! That did the trick.


All times are GMT -6. The time now is 01:44 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI