View Single Post
01/04/23, 06:34 AM   #1
wido1234
Join Date: Dec 2022
Posts: 2
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)

Last edited by wido1234 : 01/04/23 at 06:36 AM.
  Reply With Quote