Quantcast
Passing parameter to a callback function - ESOUI
Thread Tools Display Modes
06/14/20, 11:40 AM   #1
QuantumPie
AddOn Author - Click to view addons
Join Date: Sep 2019
Posts: 21
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.
  Reply With Quote
06/14/20, 12:06 PM   #2
Shinni
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 150
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?
  Reply With Quote
06/14/20, 12:14 PM   #3
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,428
Originally Posted by Shinni View Post
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.
__________________
https://www.imperialtradingcompany.eu/ - My Addons - The Vault (Early updates and experimental projects) - My patreon - My blog
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
  Reply With Quote
06/14/20, 01:07 PM   #4
QuantumPie
AddOn Author - Click to view addons
Join Date: Sep 2019
Posts: 21
Thanks! That did the trick.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Passing parameter to a callback function

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off