I have made a small addon which is supposed to hide my player and target frames when I enter a vehicle and restore their previous states on exit. The addon is working and all events are registered and fired as expected. The only problem I have is one of the function UnitInVehicle() is somehow not recognised in the event handler UNIT_EXITED_VEHICLE. Probably I am missing something obvious.
What happens is that the function either causes error when used directly because it is nil or is always recognised as false when used inside "if". I don't understand it because, when I print UnitInVehicle("player") in console, it correctly returns boolean value.
Lua Code:
HiddenFrames = LibStub("AceAddon-3.0"):NewAddon("HiddenFrames", "AceEvent-3.0")
function HiddenFrames:OnInitialize()
-- Called when the addon is loaded
self.PlayerFrameAlphaHidden = 0
self.TargetFrameAlphaHidden = 0
end
function HiddenFrames:OnEnable()
self.PlayerFrameAlphaOn = PlayerFrame:GetAlpha()
self.TargetFrameAlphaOn = TargetFrame:GetAlpha()
--
self:RegisterEvent("UNIT_ENTERED_VEHICLE")
self:RegisterEvent("UNIT_EXITED_VEHICLE")
end
-- function HiddenFrames:OnDisable()
-- -- Called when the addon is disabled
-- end
function HiddenFrames:UNIT_ENTERED_VEHICLE(eventName, unitId)
if unitId == "player" then
PlayerFrame:SetAlpha(self.PlayerFrameAlphaHidden)
TargetFrame:SetAlpha(self.TargetFrameAlphaHidden)
end
end
function HiddenFrames:UNIT_EXITED_VEHICLE()
if UnitInVehicle("player") then
print("Inside if")
PlayerFrame:SetAlpha(self.PlayerFrameAlphaOn)
TargetFrame:SetAlpha(self.TargetFrameAlphaOn)
end
end
EDIT.
I have changed the handler to check directly the second parameter sent to a function and it is working but I still don't get why I have the error when trying to check if a player is inside a vehicle!
Lua Code:
function HiddenFrames:UNIT_EXITED_VEHICLE(eventName, unitId)
if unitId == "player" then
PlayerFrame:SetAlpha(self.PlayerFrameAlphaOn)
TargetFrame:SetAlpha(self.TargetFrameAlphaOn)
end
end