removing eventListener in another function

I’m facing a challenge with eventListeners in several of my functions. Here is a simplified example:

local function exitOverlayMode( event )    composer.hideOverlay()    -- Hides the overlay (re-enable underneath content)  if( globalData.backgroundRectangleOverlay ~= nil ) then      globalData.backgroundRectangleOverlay:removeSelf()         globalData.backgroundRectangleOverlay = nil              end    -- Do some more stuff   return true   -- Prevents propagating the event to other objects    end   -- exitOverlayMode

local function upgradeLevel( event )      exitOverlayMode()   -- Exiting the overlay and re-enable some in-game functions   -- Do stuff to upgrade the building end

-- Overlay scene function create( event )   local upgradeBlockArea = display.newRoundedRect(100, 100, 400, 200, 60 )   upgradeBlockArea:addEventListener("tap", upgradeLevel)  -- Starts listening to tap on the upgradeBlockArea end   -- "scene:create()"

In my scene:create() I have a rectangle area acting as a button, which will call the upgradeLevel function when the user taps the area. For that I create the  addEventListener on  upgradeBlockArea.

In the  upgradeLevel , I would like to remove the EventListener on  upgradeBlockArea (because I have read that it is best practice) and the only way to do that  is to change my code and using pseudo global variable. For that I would add the EventListenaer and remove it as follows:

-- In overlay scene globalData.upgradeBlockArea:addEventListener("tap", upgradeLevel)

-- In upgrade function globalData.upgradeBlockArea:removeEventListener("tap", upgradeLevel)

Is that the only/best way to do it? Any recommendation? Is it necessary to always remove event listeners, even in an overlay ?

Where is upgradeLevel? If in the same file as create, you can just store upgradeBlockArea as a local variable in the file, instead of local to the create function. Then upgradeBlockArea is accessible from upgradeLevel.

Good suggestion, in that specific case upgradeLevel and upgradeBLockArea are in the same file so that would work.

For cases where they are in different files, is there a better to do than my suggestion?

Where is upgradeLevel? If in the same file as create, you can just store upgradeBlockArea as a local variable in the file, instead of local to the create function. Then upgradeBlockArea is accessible from upgradeLevel.

Good suggestion, in that specific case upgradeLevel and upgradeBLockArea are in the same file so that would work.

For cases where they are in different files, is there a better to do than my suggestion?