ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Problem with OnPlayerCombat State (https://www.esoui.com/forums/showthread.php?t=7391)

elly22 10/02/17 07:51 PM

Problem with OnPlayerCombat State
 
Hello. I try to follow this tutorial: http://wiki.esoui.com/Writing_your_first_addon
And I create a function like that:

Lua Code:
  1. function testplugin.OnPlayerCombatState(event, inCombat)
  2.   -- The ~= operator is "not equal to" in Lua.
  3.   if inCombat ~= testplugin.inCombat then
  4.     -- The player's state has changed. Update the stored state...
  5.     testplugin.inCombat = inCombat
  6.  
  7.     -- ...and then update the control.
  8.    
  9.    testpluginIndicator:SetHidden(not inCombat)
  10.  
  11.  
  12.   end
  13. end


But something went wrong, because when I reload UI, it already show me "Fighting!" sign. But I'm not in combat state! After killing the enemy, the sign disappears. But why It exists without fight mode just after reloadui? Why inCombat variable return 1?

For example,

Lua Code:
  1. if inCombat then
  2.       d("Entering combat.")
  3.     else
  4.       d("Exiting combat.")
  5.     end

works perfectly - it shows me Entering/Exiting when it really occurs.

Kyoma 10/03/17 03:19 AM

Are you also hiding the indicator on startup?

elly22 10/03/17 03:55 AM

Quote:

Are you also hiding the indicator on startup?
No. I need to do that?

I have no experience in Lua, so I only following the tutorial and here is nothing about hiding it on startup.
But why sign exists on startup?
CombatState is 1 by default? Or default value is sth like "CombatState = nil" and it must be set to zero manually?

If you can help me and show how to hide it on startup, i'll be thankful to you.

Full listing:

Warning: Spoiler

elly22 10/03/17 04:40 AM

Used pChat plugin to see what happens with inCombat veriable and saw inCombat = nil as I expected.
Okay, tried to set it to 0:
Lua Code:
  1. function testplugin:Initialize()
  2.   self.inCombat = IsUnitInCombat("player")
  3.   EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
  4.  
  5.   inCombat=false
  6. end
And inCombat is 0 after that (I check it in testplugin.OnAddOnLoaded), but sign still exists after reloadui.

elly22 10/03/17 05:18 AM



https://youtu.be/dfee9c6T918

Letho 10/03/17 07:02 AM

Can you post the indicator code pls?

Dolgubon 10/03/17 08:19 AM

Ok so most likely, if you're following the guide on the wiki, you have this in your XML:

<TopLevelControl name="TestPluginIndicator"> <Dimensions x="200" y="25" /> <Anchor point="BOTTOM" relativeTo="GuiRoot" relativePoint="CENTER" offsetY="-20" />


The UI will automatically be visible from the start, because that's what makes sense. If you draw something, default behaviour should be that you see it. However, you can make it hidden from the start. Change <TopLevelControl name="TestPluginIndicator"> to <TopLevelControl name="TestPluginIndicator" hidden="true">. This probably isn't the best way to handle it though. If you do this, and the player reloadsui while in combat, the indicator will be hidden. Sure, it's a rare bug but it's still a bug. Here's two ways you can fix that.


1. Check if the player is in combat in the Initialized() function, and set it visible/hidden as needed.
2. Use the <OnInitialized> field:
<OnInitialized>
-- LUA code here. This is where you'd check to see if the control needs to be hidden or not.
</OnInitialized>
Oninitialized does what you might expect: Runs the code within when the control is created.Edit: Actually, I don't think it does that. I think it runs when the addon is initialized, but I'm not sure.


I'd suggest checking here: http://wiki.esoui.com/UI_XML to see what other attributes are available. A TopLevelControl will have the fields under TopLevelControl. Since it says Inherits: Control there, it also has all the fields which are listed under Control.

Quote:

Originally Posted by elly22 (Post 32811)
No. I need to do that?

But why sign exists on startup?
CombatState is 1 by default? Or default value is sth like "CombatState = nil" and it must be set to zero manually?


The value of CombatState in general has nothing to do with the visibility of the indicator. It only affects the visibility because of the code. So changing the value of combatState will do nothing, because there's no event for a variable being changed. combatState is just a variable within your code, and it has no connection at all to the indicator. Please let me know if you're still confused about this.

Letho 10/03/17 06:09 PM

Once the addon is initialized, the according function should check the combat state and set the indicator to hidden, if the player is out of combat. This behaviour is definetely strange.

Dolgubon 10/04/17 01:01 AM

Yes the according function should check it, but it is never called after a reloading, until the player enters or edits combat.

ArtOfShred 10/05/17 05:05 AM

I'd set up your UI element for the indicator (testpluginIndicator) to be hidden by default on EVENT_ADD_ON_LOADED and then only ever change the display of it with your event handler for EVENT_PLAYER_COMBAT_STATE.

Only toggle the display of the element in your handler in EVENT_PLAYER_COMBAT_STATE.

There's one additional issue you might run into - if you reload UI during combat, the combat state event does not fire upon reloading UI, so you need to check for combat state anytime the player is activated. You can cover this by registering an additional event handler to EVENT_PLAYER_ACTIVATED and calling your function there. That should check combat state upon activation of the player after the loading screen.

Letho 10/05/17 12:35 PM

Quote:

Originally Posted by Dolgubon (Post 32825)
Yes the according function should check it, but it is never called after a reloading, until the player enters or edits combat.

I think that's not true, because:

Code:

function testplugin:Initialize()
self.inCombat = IsUnitInCombat("player")
EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
inCombat=false
end

After reloadui the addon will be initialized, retrieve the combat state via IsUnitInCombat("player") and set the addon's display accordingly.

Quote:

Originally Posted by ArtOfShred (Post 32830)
(...)
There's one additional issue you might run into - if you reload UI during combat, the combat state event does not fire upon reloading UI (...)

IsUnitInCombat() doesn't need a combat state event to fire as it will always return the native code's combat state information. Just verified it.

Dolgubon 10/05/17 12:48 PM

Quote:

Originally Posted by Letho (Post 32831)
I think that's not true, because:

Code:

function testplugin:Initialize()
self.inCombat = IsUnitInCombat("player")
EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
inCombat=false
end

After reloadui the addon will be initialized, retrieve the combat state via IsUnitInCombat("player") and set the addon's display accordingly.

The thing is though, the addon's display is never set accordingly. IsUnitInCombat is a base game function. It's not going to change the visibility of any UI elements from an addon. Do you see a call of testpluginIndicator:SetHidden in that initialized code? Without that the visibility won't be changed properly.

Letho 10/05/17 01:00 PM

Quote:

Originally Posted by Dolgubon (Post 32832)
The thing is though, the addon's display is never set accordingly. IsUnitInCombat is a base game function. It's not going to change the visibility of any UI elements from an addon. Do you see a call of testpluginIndicator:SetHidden in that initialized code? Without that the visibility won't be changed properly.

Oh right, now I get what he is doing. The easiest way is indeed setting the xml element to hidden and only display it when combat state changes to incombat.


All times are GMT -6. The time now is 03:03 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI