View Single Post
01-04-19, 10:23 AM   #1
FranekW
A Cyclonian
 
FranekW's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 44
UnitInVehicle() not working inside an event handler

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:
  1. HiddenFrames = LibStub("AceAddon-3.0"):NewAddon("HiddenFrames", "AceEvent-3.0")
  2.  
  3. function HiddenFrames:OnInitialize()
  4.     -- Called when the addon is loaded
  5.     self.PlayerFrameAlphaHidden = 0
  6.     self.TargetFrameAlphaHidden = 0
  7. end
  8.  
  9. function HiddenFrames:OnEnable()
  10.     self.PlayerFrameAlphaOn = PlayerFrame:GetAlpha()
  11.     self.TargetFrameAlphaOn = TargetFrame:GetAlpha()
  12.     --
  13.     self:RegisterEvent("UNIT_ENTERED_VEHICLE")
  14.     self:RegisterEvent("UNIT_EXITED_VEHICLE")
  15. end
  16.  
  17. -- function HiddenFrames:OnDisable()
  18. --     -- Called when the addon is disabled
  19. -- end
  20.  
  21. function HiddenFrames:UNIT_ENTERED_VEHICLE(eventName, unitId)
  22.     if unitId == "player" then
  23.         PlayerFrame:SetAlpha(self.PlayerFrameAlphaHidden)
  24.         TargetFrame:SetAlpha(self.TargetFrameAlphaHidden)
  25.     end
  26. end
  27.  
  28. function HiddenFrames:UNIT_EXITED_VEHICLE()
  29.     if UnitInVehicle("player") then
  30.         print("Inside if")
  31.         PlayerFrame:SetAlpha(self.PlayerFrameAlphaOn)
  32.         TargetFrame:SetAlpha(self.TargetFrameAlphaOn)
  33.     end
  34. 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:
  1. function HiddenFrames:UNIT_EXITED_VEHICLE(eventName, unitId)
  2.     if unitId == "player" then
  3.         PlayerFrame:SetAlpha(self.PlayerFrameAlphaOn)
  4.         TargetFrame:SetAlpha(self.TargetFrameAlphaOn)
  5.     end
  6. end

Last edited by FranekW : 01-04-19 at 10:50 AM.
  Reply With Quote