Quantcast UnitInVehicle() not working inside an event handler - WoWInterface
Thread Tools Display Modes
01-04-19, 10:23 AM   #1
FranekW
A Deviate Faerie Dragon
 
FranekW's Avatar
Join Date: Mar 2018
Posts: 20
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
01-04-19, 11:17 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,051
What is the actual error you are getting?
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
01-04-19, 12:12 PM   #3
FranekW
A Deviate Faerie Dragon
 
FranekW's Avatar
Join Date: Mar 2018
Posts: 20
Below is the error I was getting. I saved and copied / pasted its string because the function is working after I have changed the condition to test "unitID".

String with the error:
Lua Code:
  1. 1x HiddenFrames\core.lua:70: Usage: UnitInVehicle("unit")
  2. [C]: in function `UnitInVehicle'
  3. HiddenFrames\core.lua:70: in function `?'
  4. ...ibraries\CallbackHandler-1.0\CallbackHandler-1.0-7.lua:119: in function <...ibraries\CallbackHandler-1.0\CallbackHandler-1.0.lua:119>
  5. [C]: ?
  6. ...ibraries\CallbackHandler-1.0\CallbackHandler-1.0-7.lua:29: in function <...ibraries\CallbackHandler-1.0\CallbackHandler-1.0.lua:25>
  7. ...ibraries\CallbackHandler-1.0\CallbackHandler-1.0-7.lua:64: in function `Fire'
  8. ElvUI\Libraries\AceEvent-3.0\AceEvent-3.0-4.lua:120: in function <ElvUI\Libraries\AceEvent-3.0\AceEvent-3.0.lua:119>
  Reply With Quote
01-04-19, 12:22 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,051
The error is saying the parameter (unit) passed wasn't valid not that the function UnitInVehicle was nil.

Possibly a typo that didn't register when you looked back over the code.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
01-04-19, 12:47 PM   #5
FranekW
A Deviate Faerie Dragon
 
FranekW's Avatar
Join Date: Mar 2018
Posts: 20
I know what I did, now. There is no typo in the block of code (the first one), which I tested again.

UnitInVehicle() inside the event handler UNIT_EXITED_VEHICLE would never return "true" because, when this event is fired, a player is no longer in a vehicle, which I could not understand. Before, I tried to test the whole handler by adding `print(UnitInVehicle("player"))` in front of `if` and this is where I must have made a typo.

I was confused and assumed that perhaps UnitInVehicle() is not available (as I got nil before). It's total newbie thing! I hope someone else can learn from my mistake!
  Reply With Quote
01-05-19, 06:07 AM   #6
myrroddin
A Molten Giant
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 989
You can go even simpler. On a side note, I added local to the first line.
Lua Code:
  1. local HiddenFrames = LibStub("AceAddon-3.0"):NewAddon("HiddenFrames", "AceEvent-3.0")
  2.  
  3. function: HiddenFrames:OnInitialize()
  4.     -- called when the addon is loaded
  5. end
  6.  
  7. function HiddenFrames:OnEnable()
  8.     PlayerFrame:SetAlpha(1)
  9.     TargetFrame:SetAlpha(1)
  10.    
  11.     self:RegisterEvent("UNIT_ENTERED_VEHICLE", "CheckVehicle")
  12.     self:RegisterEvent("UNIT_EXITED_VEHICLE", "CheckVehicle")
  13. end
  14.  
  15. function HiddenFrames:OnDisable()
  16.     PlayerFrame:SetAlpha(1)
  17.     TargetFrame:SetAlpha(1)
  18. end
  19.  
  20. function HiddenFrames:CheckVehicle()
  21.     if UnitInVehicle("player") then
  22.         PlayerFrame:SetAlpha(0)
  23.         TargetFrame:SetAlpha(0)
  24.     else
  25.         PlayerFrame:SetAlpha(1)
  26.         TargetFrame:SetAlpha(1)
  27.     end
  28. end
  Reply With Quote
01-06-19, 04:24 AM   #7
FranekW
A Deviate Faerie Dragon
 
FranekW's Avatar
Join Date: Mar 2018
Posts: 20
Hi myrrodin,

Thanks for the code. Itís very compact. It shows that I need to learn more! My first version used to have UniInVehicle but I had a problem. I can see your code can work perfectly with that function.

Do you always define the main addon object as local?
  Reply With Quote
01-06-19, 07:51 PM   #8
myrroddin
A Molten Giant
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 989
Originally Posted by FranekW View Post
Do you always define the main addon object as local?
I do, yes. Otherwise the object is in the global name space, which is bad more often than getting away with it, and it is never good.

The name HiddenFrames is incredibly generic, and 99.99% likely to be overwritten with code that isn't yours. Even if you did have a totally unique name, making variables local is almost always better.

With Ace3, you can use AceAddon's GetAddon API to return the object if it is in another file within the same addon, meaning if the primary file defines the object as local, you can still reference it in another file.
  Reply With Quote
01-07-19, 07:55 AM   #9
FranekW
A Deviate Faerie Dragon
 
FranekW's Avatar
Join Date: Mar 2018
Posts: 20
Originally Posted by myrroddin View Post
I do, yes. Otherwise the object is in the global name space, which is bad more often than getting away with it, and it is never good.

The name HiddenFrames is incredibly generic, and 99.99% likely to be overwritten with code that isn't yours. Even if you did have a totally unique name, making variables local is almost always better.

With Ace3, you can use AceAddon's GetAddon API to return the object if it is in another file within the same addon, meaning if the primary file defines the object as local, you can still reference it in another file.
I understand. I've changed variable to local. Before I thought this object would need to be in global scope.

I really appreciate your comments because those couple of posts (this one here and others answering my questions in other threads) let me figure out how things work. There's still plenty to learn but I believe I have a pretty good understanding of basic stuff

Last edited by FranekW : 01-07-19 at 07:59 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » UnitInVehicle() not working inside an event handler

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