i'm getting into creating my first WoW addon and during my research I have read that every frame creaated in WoW is global which really confuses me.
I've seen plenty of addons that just do f = CreateFrame(..), so wouldn't those be conflicting all the time?
There's probably an easy explanation for this so sorry if the question is a bit dumb but if someone could clear that up for me I would really appreciate it
CodePudding user response:
Every frame created from an XML file is global so that they can be accessed from a Lua file.
- hello.toc
## Interface: 90205
## Version: 1.0.0
## Title: Hello
hello.xml
- hello.xml
<Ui>
<Script file="hello.lua"/>
<Frame name="HelloWorldFrame"> <!-- global frame -->
<Scripts>
<OnLoad>
HelloWorld_OnLoad(self)
</OnLoad>
<OnEvent>
HelloWorld_OnEvent(self, event, ...)
</OnEvent>
</Scripts>
</Frame>
</Ui>
- hello.lua
function HelloWorld_OnLoad(self)
print("Loaded HelloWorldFrame")
self:RegisterEvent("CHAT_MSG_SAY")
end
function HelloWorld_OnEvent(self, event, ...)
print("Your character said something", event, ...)
end
While if you only use a Lua file and create your frame there you don't necessarily need to make it global.
local function OnEvent(self, event, ...)
print("Your character said something", event, ...)
end
local f = CreateFrame("Frame")
f:RegisterEvent("CHAT_MSG_SAY")
f:SetScript("OnEvent", OnEvent)
I've seen plenty of addons that just do f = CreateFrame(..), so wouldn't those be conflicting all the time?
As long as they make it local, they wouldn't conflict. If everyone makes a global f
frame then it would conflict yes.
CodePudding user response:
I know nothing about World of Warcraft specifically, but I am familiar with Lua enough that I feel I can answer this question. If frames are placed in the global environment by default, then that code example you posted:
f = CreateFrame(...)
Would create no such "conflict" per se - the frame may simply be assigned to f
twice, or once to f
and once to whatever name is passed to CreateFrame.
This causes no problems in Lua itself, since variable assignment is dynamic, you can pretty much do whatever you want with these values. Overwriting a variable that's already been set is not a problem, the old value will be freed and garbage collected automatically, or if the old value and the new value are the same reference, nothing will happen.
If you ever see the return value being assigned to a local variable, e.g. local f = CreateFrame(...)
inside of a function, it may be simply a matter of style that the programmer uses the local variable instead of the global name.
If I'm correctly presuming that the function sets a global variable but also returns the value, it may be redundant like you observed, but it's inconsequential, that is it won't cause any problems.