Skip to content

fix: g_currentMission.mouseEvent monkey-patch bypasses eventUsed contract #38

@TheCodingDad-TisonK

Description

@TheCodingDad-TisonK

Problem

FarmTabletUI.lua patches g_currentMission.mouseEvent directly instead of using addModEventListener. This approach bypasses the eventUsed contract entirely and can interfere with other mods.

Current code (src/FarmTabletUI.lua ~line 117):

self._oldMouseEvent = g_currentMission.mouseEvent
g_currentMission.mouseEvent = function(mission, px, py, isDown, isUp, btn)
    -- custom handler, then:
    self._oldMouseEvent(mission, px, py, isDown, isUp, btn)
end

Issues:

  1. Bypasses eventUsed — the injected function never receives or returns the eventUsed flag, so other listeners can't signal consumed events
  2. Fragile chaining — if another mod also monkey-patches g_currentMission.mouseEvent, the chain breaks depending on load order
  3. Cleanup risk — if the mod unloads while another mod's patch is in the chain, the restore can leave a dangling closure

Recommended fix

Replace the monkey-patch with addModEventListener, which is the FS25-supported pattern for raw mouse input in mods:

local farmTabletMouseHandler = {}
function farmTabletMouseHandler:mouseEvent(posX, posY, isDown, isUp, button, eventUsed)
    if not eventUsed and self.ui then
        eventUsed = self.ui:onMouseEvent(posX, posY, isDown, isUp, button, eventUsed) or eventUsed
    end
    return eventUsed
end
addModEventListener(farmTabletMouseHandler)

On cleanup, use removeModEventListener(farmTabletMouseHandler) — no manual restore needed.

Reference

addModEventListener + eventUsed pattern fixed across multiple mods in FS25_SoilFertilizer commit d5b8f31 (issue TheCodingDad-TisonK/FS25_SoilFertilizer#130). The same pattern is used in FS25_IncomeMod, FS25_TaxMod, and others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinguser-repliedUser is awaiting response

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions