Thread Tools Display Modes
10/02/17, 07:51 PM   #1
elly22
Join Date: Oct 2017
Posts: 4
Question 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.
  Reply With Quote
10/03/17, 03:19 AM   #2
Kyoma
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 125
Are you also hiding the indicator on startup?

Last edited by Kyoma : 10/03/17 at 03:27 AM.
  Reply With Quote
10/03/17, 03:55 AM   #3
elly22
Join Date: Oct 2017
Posts: 4
Question

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
  Reply With Quote
10/03/17, 04:40 AM   #4
elly22
Join Date: Oct 2017
Posts: 4
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.
  Reply With Quote
10/03/17, 05:18 AM   #5
elly22
Join Date: Oct 2017
Posts: 4


https://youtu.be/dfee9c6T918
  Reply With Quote
10/03/17, 07:02 AM   #6
Letho
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 238
Can you post the indicator code pls?
  Reply With Quote
10/03/17, 08:19 AM   #7
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
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.

Originally Posted by elly22 View Post
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.

Last edited by Dolgubon : 10/03/17 at 08:48 AM.
  Reply With Quote
10/03/17, 06:09 PM   #8
Letho
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 238
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.
  Reply With Quote
10/04/17, 01:01 AM   #9
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
Yes the according function should check it, but it is never called after a reloading, until the player enters or edits combat.
  Reply With Quote
10/05/17, 05:05 AM   #10
ArtOfShred
 
ArtOfShred's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 103
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.

Last edited by ArtOfShred : 10/05/17 at 05:10 AM.
  Reply With Quote
10/05/17, 12:35 PM   #11
Letho
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 238
Originally Posted by Dolgubon View Post
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.

Originally Posted by ArtOfShred View Post
(...)
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.

Last edited by Letho : 10/05/17 at 12:40 PM.
  Reply With Quote
10/05/17, 12:48 PM   #12
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
Originally Posted by Letho View Post
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.
  Reply With Quote
10/05/17, 01:00 PM   #13
Letho
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 238
Originally Posted by Dolgubon View Post
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.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Problem with OnPlayerCombat State

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