WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   how is better to write ... (https://www.wowinterface.com/forums/showthread.php?t=57895)

gmarco 03-29-20 07:48 AM

how is better to write ...
 
Hi all,

having a lot of times in this period I am here polishing and refreshing my addons code ...

A couple of questions:

1) having code like this:

Lua Code:
  1. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  2. local dataobj = ldb:NewDataObject("gmVideoSwitch", {
  3.     type = "data source",
  4.     icon = "Interface\\Addons\\"..ADDON.."\\bluewin.tga",
  5.     text = "-"
  6. })

Lua Code:
  1. function dataobj.OnEnter(self)
  2.     -- code here
  3. end

or

Lua Code:
  1. dataobj.OnEnter = function(self)
  2.     -- code is here
  3. end

The above statement are equal I suppose, which is better to be written ?

And the second question.

Being defined as:

Lua Code:
  1. local dataobj

all its functions are local too automagically or it is better to defined them local too ?

Thanks.

myrroddin 03-29-20 09:15 AM

Look at my code in your other thread.

gmarco 03-29-20 11:54 AM

Hi myrroddin,

So it is better to write functions in this way ?

Lua Code:
  1. dataobject.OnClick = function(object, button)
  2.     -- open configuration
  3. end

even if I put them out of its context ?

But I'd like to know also if it could be a problem to write in the other way or at the end of the story both syntax works in the same way with the same results.

Thanks so much for your time.



Thanks.

Seerah 03-29-20 12:46 PM

As you said, they're equal.

gmarco 03-29-20 01:38 PM

Thanks sp much Seerah.

I have tried to write in both way and they obviusly works but before rewrite all addons in the same way and try to write everytime in the same ways I wanted to be sure :)

And about the fact that the "dataobj" is defined local ... all its functions are local by defaults or I have to defined them "local" to let them be local ?

Thanks.

SDPhantom 03-29-20 06:54 PM

In general, they're equal, but there is a very niche difference between them, and that's the order of when the function is compiled in memory and when the variable it's assigned to is created. This difference can only be seen when dealing with local functions trying to access their own pointer, usually by recursive calling.


Code:

local function RecursiveFunc()
The local is created before the function is compiled. This function can see the local variable and is capable of calling itself.


Code:

local RecursiveFunc=function()
The function is compiled before the local is created, so this function cannot see itself. Attempts to call recursively will try to look for it in the global environment.

myrroddin 03-29-20 11:39 PM

I'd suggest creating the LDB object in PLAYER_LOGIN because you only need to do that once. You could do in the main chunk of your addon code but that would force you to look up the object to see if it exists, and if so, get its value, and if not, create it. That's doing more CPU work than necessary. Anyway, here is the variant if you do not use PLAYER_LOGIN:
Code:

local dataobj = dataobj or ldb:NewDataObject("gmVideoSwitch", {
    -- blah
})


SDPhantom 03-30-20 03:13 AM

Quote:

Originally Posted by gmarco (Post 335450)
And the second question.

Being defined as:

Lua Code:
  1. local dataobj

all its functions are local too automagically or it is better to defined them local too ?

Thanks.

I missed this question and it looks like it hasn't been answered yet. dataobj in your code ends up being a table. Everything in the table are entries. They're neither locals nor globals. In terms of performance, each indexing operation you perform is akin to accessing a global. This also means when you have nested tables, each indexing operation stacks up when you navigate through them to reference an entry.

gmarco 03-30-20 08:07 AM

Quote:

Originally Posted by myrroddin (Post 335470)
I'd suggest creating the LDB object in PLAYER_LOGIN because you only need to do that once. You could do in the main chunk of your addon code but that would force you to look up the object to see if it exists, and if so, get its value, and if not, create it. That's doing more CPU work than necessary.

I see what you mean ... the problem is for my way of writing that usually I create the frames in the last part of the code of the addon and if I put the create of LDB object there I was unable to build all the others part of the dataobj .


Usually I write in similar way:

Lua Code:
  1. local ADDON, namespace = ...
  2. local L = namespace.L
  3.  
  4. -- code
  5.  
  6. local LibQTip = LibStub('LibQTip-1.0')
  7. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  8.  
  9. local dataobj = ldb:NewDataObject(ADDON, {
  10.     type = "data source",
  11.     icon = "Interface\\Addons\\"..ADDON.."\\icon.tga",
  12.     text = "-"
  13. })
  14.  
  15. local frame = CreateFrame("Frame")
  16.  
  17. local function OnRelease(self)
  18.      -- code
  19. end  
  20.  
  21. local function Button_OnClick(row, arg, button)
  22.      -- code
  23. end
  24.  
  25. local function anchor_OnEnter(self)
  26.  
  27.     arg = {}
  28.  
  29.     if self.tooltip then
  30.         LibQTip:Release(self.tooltip)
  31.         self.tooltip = nil  
  32.     end
  33.     local row,col
  34.     local tooltip = LibQTip:Acquire(ADDON.."tip", 6,"LEFT","LEFT","LEFT","LEFT","RIGHT","RIGHT")
  35.     self.tooltip = tooltip
  36.     tooltip:SmartAnchorTo(self)
  37.     tooltip:EnableMouse(true)
  38.     tooltip.OnRelease = OnRelease
  39.     tooltip.OnLeave = OnLeave
  40.     tooltip:SetAutoHideDelay(.1, self)
  41.  
  42.     row,col = tooltip:AddLine("")
  43.     row,col = tooltip:AddLine("")
  44.  
  45.     if #deaths > 0 then
  46.  
  47.         row,col = tooltip:AddLine()
  48.         tooltip:SetCell(row,1,L["Deaths Reports"],"CENTER",6)
  49.         tooltip:SetColumnTextColor(1,1,1,0)
  50.         row,col = tooltip:AddLine("")
  51.  
  52.     -- code
  53.  
  54.     row,col = tooltip:Show()
  55. end
  56.  
  57. function dataobj.OnEnter(self)
  58.     anchor_OnEnter(self)
  59. end
  60.  
  61. function dataobj.OnLeave(self)
  62.     -- Null operation: Some LDB displays get cranky if this method is missing.
  63. end
  64.  
  65. function dataobj.OnClick(self, button)
  66.    -- code
  67. end
  68.  
  69. --code
  70. frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  71. frame:SetScript("OnEvent", function(self, event, ...)
  72.    
  73.     if event == "ZONE_CHANGED_NEW_AREA" or event == "PLAYER_ENTERING_WORLD" then   
  74.         -- code
  75.     end
  76.    
  77.     if event == "COMBAT_LOG_EVENT_UNFILTERED" then
  78.                -- code
  79.     end
  80. end)


Thanks so much for your inputs !


All times are GMT -6. The time now is 07:30 AM.

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