ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Label Scope (https://www.esoui.com/forums/showthread.php?t=1753)

Specko 06/09/14 08:35 PM

Label Scope
 
Hello All,

I'm having trouble referencing a label in another function. At this point, I'm just learning and need a little help. Thanks to Zork for some skeleton code. Please code review and let me know why I'm getting nil reference on print(c.label:GetAlpha())

Line 58 works as expected.

Code:


  -----------------------------
  -- Learning
  -- Specko
  -----------------------------

  -----------------------------
  -- VARIABLES
  -----------------------------

  local wm = GetWindowManager()
  local em = GetEventManager()
  local print = d

  local KillingBlow = ZO_Object:Subclass()

  -----------------------------
  -- FUNCTIONS
  -----------------------------


  function KillingBlow:New()
    self.addonName = "KillingBlow"
    self.addonList = {}
    self:Initialize()
  end


  function KillingBlow:Initialize()
    self.control = self:CreateControl()
    self:InitializeEventManagement()
  end


  function KillingBlow:CreateControl()

        local path = "EsoUI/Common/Fonts/univers67.otf"
        local size = 25
        local style = "thick-outline"

    --main control
    local c = wm:CreateTopLevelWindow()
    c:SetDimensions(300,200)
    c:SetAnchor(CENTER,GuiRoot,CENTER,0,0)
    c:SetHidden(false)
    c:SetMovable(false)
    c:SetMouseEnabled(false)
    c:SetClampedToScreen(true)

        --label
    c.label = wm:CreateControl(nil, c, CT_LABEL)
    c.label:SetDimensions(300,200)
        c.label:SetAnchor(CENTER, c, CENTER, 75, -150)
        c.label:SetFont(path .. "|" .. size .. "|" ..  style)
        c.label:SetColor( 128,128,0,.5 )
        c.label:SetHorizontalAlignment( CENTER )
        c.label:SetVerticalAlignment( CENTER )
        c.label:SetText ( c.label:GetAlpha() )

        return c, c.label

  end

  function KillingBlow:labelScope()

        print(c.label:GetAlpha())

  end

  --OnAddOnLoaded event trigger func
  function KillingBlow:OnAddOnLoaded(event, name)
    self.addonList[#self.addonList] = { event = event or "unknown", name = name or "unknown"}
  end

  --OnPlayerActivated event trigger func
  function KillingBlow:OnPlayerActivated()
    print("KillingBlow Loaded")
  end




  --init events func
  function KillingBlow:InitializeEventManagement()
    --event registering
    em:RegisterForEvent(self.addonName.."_OnAddOnLoaded", EVENT_ADD_ON_LOADED, function(...) self:OnAddOnLoaded(...) end)
    em:RegisterForEvent(self.addonName.."_OnPlayerActivated", EVENT_PLAYER_ACTIVATED, function(...) self:OnPlayerActivated(...) end)
        em:RegisterForEvent(self.addonName.."_labelScope", EVENT_MOUNTED_STATE_CHANGED, function(...) self:labelScope(...) end)
  end

  -----------------------------
  -- CALL
  -----------------------------

  KillingBlow:New()


Garkin 06/09/14 09:05 PM

Quote:

Originally Posted by Specko (Post 9247)
Hello All,

I'm having trouble referencing a label in another function. At this point, I'm just learning and need a little help. Thanks to Zork for some skeleton code. Please code review and let me know why I'm getting nil reference on print(c.label:GetAlpha())

Line 58 works as expected.

Code:


  -----------------------------
  -- Learning
  -- Specko
  -----------------------------

  -----------------------------
  -- VARIABLES
  -----------------------------

  local wm = GetWindowManager()
  local em = GetEventManager()
  local print = d

  local KillingBlow = ZO_Object:Subclass()

  -----------------------------
  -- FUNCTIONS
  -----------------------------


  function KillingBlow:New()
    self.addonName = "KillingBlow"
    self.addonList = {}
    self:Initialize()
  end


  function KillingBlow:Initialize()
    self.control = self:CreateControl()
    self:InitializeEventManagement()
  end


  function KillingBlow:CreateControl()

        local path = "EsoUI/Common/Fonts/univers67.otf"
        local size = 25
        local style = "thick-outline"

    --main control
    local c = wm:CreateTopLevelWindow()
    c:SetDimensions(300,200)
    c:SetAnchor(CENTER,GuiRoot,CENTER,0,0)
    c:SetHidden(false)
    c:SetMovable(false)
    c:SetMouseEnabled(false)
    c:SetClampedToScreen(true)

        --label
    c.label = wm:CreateControl(nil, c, CT_LABEL)
    c.label:SetDimensions(300,200)
        c.label:SetAnchor(CENTER, c, CENTER, 75, -150)
        c.label:SetFont(path .. "|" .. size .. "|" ..  style)
        c.label:SetColor( 128,128,0,.5 )
        c.label:SetHorizontalAlignment( CENTER )
        c.label:SetVerticalAlignment( CENTER )
        c.label:SetText ( c.label:GetAlpha() )

        return c, c.label

  end

  function KillingBlow:labelScope()

        print(c.label:GetAlpha())

  end

  --OnAddOnLoaded event trigger func
  function KillingBlow:OnAddOnLoaded(event, name)
    self.addonList[#self.addonList] = { event = event or "unknown", name = name or "unknown"}
  end

  --OnPlayerActivated event trigger func
  function KillingBlow:OnPlayerActivated()
    print("KillingBlow Loaded")
  end




  --init events func
  function KillingBlow:InitializeEventManagement()
    --event registering
    em:RegisterForEvent(self.addonName.."_OnAddOnLoaded", EVENT_ADD_ON_LOADED, function(...) self:OnAddOnLoaded(...) end)
    em:RegisterForEvent(self.addonName.."_OnPlayerActivated", EVENT_PLAYER_ACTIVATED, function(...) self:OnPlayerActivated(...) end)
        em:RegisterForEvent(self.addonName.."_labelScope", EVENT_MOUNTED_STATE_CHANGED, function(...) self:labelScope(...) end)
  end

  -----------------------------
  -- CALL
  -----------------------------

  KillingBlow:New()


Table (or top level window) "c" is defined as local in function KillingBlow:CreateControl(), it is not visible outside of the function. However when you have initialized object, reference to this table was stored in self.control, so you can use this reference instead of "c":

Lua Code:
  1. function KillingBlow:labelScope()
  2.  
  3.    print(self.control.label:GetAlpha())
  4.  
  5. end

Specko 06/09/14 10:23 PM

Quote:

Originally Posted by Garkin (Post 9248)
Table (or top level window) "c" is defined as local in function KillingBlow:CreateControl(), it is not visible outside of the function. However when you have initialized object, reference to this table was stored in self.control, so you can use this reference instead of "c":

Lua Code:
  1. function KillingBlow:labelScope()
  2.  
  3.    print(self.control.label:GetAlpha())
  4.  
  5. end

Works, much appreciated. I'll get a handle on this eventually.


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

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