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:
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:
|
What is the actual error you are getting?
|
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:
|
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. |
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! |
You can go even simpler. On a side note, I added local to the first line.
Lua Code:
|
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? |
Quote:
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. |
Quote:
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 :) |
All times are GMT -6. The time now is 01:01 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI