Thread Tools Display Modes
06/09/14, 08:35 PM   #1
Specko
 
Specko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 16
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()
  Reply With Quote
06/09/14, 09:05 PM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Specko View Post
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
  Reply With Quote
06/09/14, 10:23 PM   #3
Specko
 
Specko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 16
Originally Posted by Garkin View Post
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.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Label Scope


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