View Single Post
03/14/24, 02:15 PM   #5
Saint-Ange
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 10
Originally Posted by Baertram View Post
Not sure why but if I dowload your currently uploaded addon file Windows Defender pops in and says your file is a virus.
Maybe a false positive but are you sure you had uploaded the actual addon and nothing else?


And yes, please, as Dolgubon already said:
Always put a local in front of your function names! Or if you define a table local addon, add the functions to that table

function addon.OnCombatStateChanged(param)
function addon.Initialize(param)
function addon.UpdateGroupFrameVisibility(param)


and call them the same addon.OnCombatStateChanged(...) or addon.Initialize, or addon.UpdateGroupFrameVisibility
-> Keep in mind that local functions and code cannot be accessed from XML files of your addon, so see below how to fix this.

Else you might overwrite other addons/ZOs lua code, or some other addon might do that at your addon.

Everything not explicitly defined local is global and must use a very unique name, which in your case could be
function ToggleGroupFrame_OnCombatStateChanged e.g. but actually that's not needed. Keep it local.
Or create 1 global table ToggleGroupFrame = {} and add all functions and variables to that, so you can call it from XML code.
Else it may overwrite, or get overwritten!
Ok, for the virus I recon it was a bit too much but it's good to live on the edge
Now, I've followed your indications and came up with this, it works though my saved variables .. aren't saved anymore so more work needed.

Code:
ToggleGroupFrame = {}

local addon = { name = "ToggleGroupFrame" }
local em = GetEventManager()
local groupFrameHidden = true
local showInCombat = true

ZO_CreateStringId("SI_BINDING_NAME_TOGGLE_GROUP_FRAME", "Show/Hide group frame")
ZO_CreateStringId("SI_BINDING_NAME_TOGGLE_GROUP_FRAME_IN_COMBAT", "Show/Hide in combat")

function addon.OnAddOnLoaded(event, addonName)
   if addonName ~= addon.name then return end
   addon.Initialize()
end

function addon.Initialize()
   ZO_UnitFramesGroups:SetHidden(true)

   local savedVariables = ZO_SavedVars:New("ToggleGroupFrameSavedVariables", 1, nil, {}, GetWorldName())

   SLASH_COMMANDS['/togglegroupframe'] = function()
      groupFrameHidden = not groupFrameHidden
      ZO_UnitFramesGroups:SetHidden(groupFrameHidden)
      savedVariables.groupFrameHidden = groupFrameHidden
   end
   SLASH_COMMANDS['/tgf'] = SLASH_COMMANDS['/togglegroupframe']
   
   SLASH_COMMANDS['/togglegroupframeincombat'] = function()
      showInCombat = not showInCombat
      savedVariables.showInCombat = showInCombat
      addon.UpdateGroupFrameVisibility()
   end
   SLASH_COMMANDS['/tgfc'] = SLASH_COMMANDS['/togglegroupframeincombat']
   SLASH_COMMANDS['/tgfic'] = SLASH_COMMANDS['/togglegroupframeincombat']

   em:RegisterForEvent(addon.name, EVENT_PLAYER_COMBAT_STATE, addon.OnCombatStateChanged)
   em:UnregisterForEvent(addon.name, EVENT_ADD_ON_LOADED)
end

function addon.UpdateGroupFrameVisibility()
   if IsUnitInCombat("player") then
      if showInCombat then
         ZO_UnitFramesGroups:SetHidden(false)
      else
         ZO_UnitFramesGroups:SetHidden(true)
      end
   else
      ZO_UnitFramesGroups:SetHidden(groupFrameHidden)
   end
end

function addon.OnCombatStateChanged(event, inCombat)
   if not groupFrameHidden then
      return
   end

   addon.UpdateGroupFrameVisibility()
end

em:RegisterForEvent(addon.name, EVENT_ADD_ON_LOADED, addon.OnAddOnLoaded)
Thank you very much lads! You know how it is when you .. just can't anymore. It's a lot to learn.
  Reply With Quote