ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   Combat Text shows when reloading UI or starting the game (https://www.esoui.com/forums/showthread.php?t=10399)

wido1234 01/04/23 06:34 AM

Combat Text shows when reloading UI or starting the game
 
Hey. i yesterday tried to, create my first addon, with the esoui tutorial. But somehow my "fighting" text always shows when i reloadui or log into the game. when i then fight it works like intended. what did i wrong.
i named my addon "Avatar", but the code is copied from the tut. also i changed it to be account wide. with the NewAccountWide thing






Here my lua code:

-- First, we create a namespace for our addon by declaring a top-level table that will hold everything else.
Avatar = {}

-- This isn't strictly necessary, but we'll use this string later when registering events.
-- Better to define it in a single place rather than retyping the same string.
Avatar.name = "Avatar"

-- Next we create a function that will initialize our addon
function Avatar.Initialize()
Avatar.inCombat = IsUnitInCombat("player")

EVENT_MANAGER:RegisterForEvent(Avatar.name, EVENT_PLAYER_COMBAT_STATE, Avatar.OnPlayerCombatState)

Avatar.savedVariables = ZO_SavedVars:NewAccountWide("AvatarSavedVariables", 1, nil, {})
end
-- ...but we don't have anything to initialize yet. We'll come back to this.
function Avatar.OnPlayerCombatState(event, inCombat)
-- The ~= operator is "not equal to" in Lua.
if inCombat ~= Avatar.inCombat then
-- The player's state has changed. Update the stored state...
Avatar.inCombat = inCombat

-- ...and then update the control.
AvatarIndicator:SetHidden(not inCombat)
end
end

function Avatar.OnIndicatorMoveStop()
Avatar.savedVariables.left = AvatarIndicator:GetLeft()
Avatar.savedVariables.top = AvatarIndicator:GetTop()
end

function Avatar.RestorePosition()
local left = Avatar.savedVariables.left
local top = Avatar.savedVariables.top

AvatarIndicator:ClearAnchors()
AvatarIndicator:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
end

function Avatar.Initialize()
Avatar.inCombat = IsUnitInCombat("player")

EVENT_MANAGER:RegisterForEvent(Avatar.name, EVENT_PLAYER_COMBAT_STATE, Avatar.OnPlayerCombatState)

Avatar.savedVariables = ZO_SavedVars:NewCharacterIdSettings("AvatarSavedVariables", 1, nil, {})

Avatar.RestorePosition()
end

-- Then we create an event handler function which will be called when the "addon loaded" event
-- occurs. We'll use this to initialize our addon after all of its resources are fully loaded.
function Avatar.OnAddOnLoaded(event, addonName)
-- The event fires each time *any* addon loads - but we only care about when our own addon loads.
if addonName == Avatar.name then
Avatar.Initialize()
--unregister the event again as our addon was loaded now and we do not need it anymore to be run for each other addon that will load
EVENT_MANAGER:UnregisterForEvent(Avatar.name, EVENT_ADD_ON_LOADED)
end
end

-- Finally, we'll register our event handler function to be called when the proper event occurs.
-->This event EVENT_ADD_ON_LOADED will be called for EACH of the addns/libraries enabled, this is why there needs to be a check against the addon name
-->within your callback function! Else the very first addon loaded would run your code + all following addons too.
EVENT_MANAGER:RegisterForEvent(Avatar.name, EVENT_ADD_ON_LOADED, Avatar.OnAddOnLoaded)

Baertram 01/04/23 06:47 AM

If it shows upon loading into the game you have forgotten to set your fighting text control invisible at the start (OnAddOnLoaded)

AvatarIndicator:SetHidden(true)

You currently do this only in your InCombat callback function which is not fired until you first get into combat.
AvatarIndicator:SetHidden(not inCombat)


e.g.
Lua Code:
  1. function Avatar.Initialize()
  2.     AvatarIndicator:SetHidden(true) --add this
  3.  
  4.     Avatar.inCombat = IsUnitInCombat("player")
  5.  
  6.     EVENT_MANAGER:RegisterForEvent(Avatar.name, EVENT_PLAYER_COMBAT_STATE, Avatar.OnPlayerCombatState)
  7.  
  8.     Avatar.savedVariables = ZO_SavedVars:NewAccountWide("AvatarSavedVariables", 1, nil, {})
  9. end

wido1234 01/10/23 03:03 AM

Quote:

Originally Posted by Baertram (Post 46919)
If it shows upon loading into the game you have forgotten to set your fighting text control invisible at the start (OnAddOnLoaded)

AvatarIndicator:SetHidden(true)

You currently do this only in your InCombat callback function which is not fired until you first get into combat.
AvatarIndicator:SetHidden(not inCombat)


e.g.
Lua Code:
  1. function Avatar.Initialize()
  2.     AvatarIndicator:SetHidden(true) --add this
  3.  
  4.     Avatar.inCombat = IsUnitInCombat("player")
  5.  
  6.     EVENT_MANAGER:RegisterForEvent(Avatar.name, EVENT_PLAYER_COMBAT_STATE, Avatar.OnPlayerCombatState)
  7.  
  8.     Avatar.savedVariables = ZO_SavedVars:NewAccountWide("AvatarSavedVariables", 1, nil, {})
  9. end

Thank you :)


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

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