WoW:API CreateFrame: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (→‎Notes: Added 3rd note)
m (Move page script moved page API CreateFrame to API CreateFrame without leaving a redirect)
 
(20 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{wowapi}} __NOTOC__
{{wowapi}} __NOTOC__


Creates a new UI frame.
Creates a new [[UIOBJECT_Frame|UI frame]].


  newFrame = CreateFrame("frameType", "frameName", parentFrame[, "inheritsFrame"]);
  newFrame = CreateFrame("frameType"[, "frameName"[, parentFrame[, "inheritsFrame"]]]);


== Parameters ==
== Parameters ==
=== Arguments ===
=== Arguments ===


:;frameType : String - Type of the frame to be created (XML tag name): "Frame", "Button"... etc.
:;frameType
:;frameName : String - Name of the newly created frame. If nil, no frame name is assigned.
:: String - Type of the frame to be created (XML tag name): "Frame", "Button", etc. See [[UIOBJECT_Frame]]
:;parentFrame : Frame - The frame object that will be used as the created Frame's parent (cannot be a string!)
:;frameName
:;inheritsFrame : String - Name of a (virtual) frame to inherit (the same as in XML)
:: String - Name of the newly created frame. If nil, no frame name is assigned. The function will also set a global variable of this name to point to the newly created frame.
:;parentFrame
:: Frame - The frame object that will be used as the created Frame's parent (cannot be a string!) Does not default to UIParent if given nil.
:;inheritsFrame
:: String - a comma-delimited list of names of virtual frames to inherit from (the same as in XML). If nil, no frames will be inherited. These frames cannot be frames that were created using this function, they must be created using XML with '''virtual="true"''' in the tag.


=== Returns ===
=== Returns ===


:;newFrame : Frame - Pointer to the newly created frame.
:;newFrame
:: Frame - Reference to the newly created frame.


 
== Example ==
== Example ==  
Result: displays the horde and alliance insignias in the middle of the screen.
Result: displays the horde and alliance insignias in the middle of the screen.
  local f = CreateFrame("Frame",nil,UIParent)
  local f = CreateFrame("Frame",nil,UIParent)
  f:SetFrameStrata("BACKGROUND")
  f:SetFrameStrata("BACKGROUND")
  f:SetWidth(128) -- Set These to whatever height/width is needed  
  f:SetWidth(128) -- Set these to whatever height/width is needed  
  f:SetHeight(64) -- for your Texture
  f:SetHeight(64) -- for your Texture
   
   
Line 32: Line 36:
  f:SetPoint("CENTER",0,0)
  f:SetPoint("CENTER",0,0)
  f:Show()
  f:Show()


== Notes ==
== Notes ==
* CreateFrame() was added in 1.10
* CreateFrame() was added in 1.10
* The fourth argument, inheritFrame, was added in 1.11
* The fourth argument, inheritFrame, was added in 1.11
* In creating frames with CreateFrame(), you cannot use the OnLoad script handler (e.g. Frame:SetScript("OnLoad", ''function'')) since CreateFrame() loads the frame before Frame:SetScript() is called. If you want to use the OnLoad script handler, define the frame in an XML file that is called before the LUA file or call the OnLoad function manually at the end of the LUA file.
* Frames CANNOT be deleted. Reuse them.
* The frame's [[UIHANDLER_OnLoad|OnLoad handler]], which will only exist if inherited, will be executed during the CreateFrame call. Any OnLoad script handlers set after CreateFrame() will not execute; consider adding any non-inherited OnLoad code directly after a CreateFrame call or use XML frames instead.
* The returned frame reference is not strictly necessary because you can access frames by their global name if you give them one. For performance reasons, however, it is advised to store frame references in local variables and use those instead of getting frames by their global name.
* See also [[API_Frame_SetScript]] to handle OnFunction scripts when not using a template.
* The most common uses for this function are:
** Creating anonymous [[Events (API)|event handlers]].
** Creating anonymous [[Widget handlers|script handlers]].
** Creating frames on the fly for occasions when you don't know how many frames will be needed.
** Creating infrequently used frames "on demand", for example: config dialogs, raid frames.
* Never forget to unset the frames parent, if you want to get rid of a frame. I would suggest to hide the frame via frame:[[API_Region_Hide|Hide]]() and to use frame:[[API_Region_SetParent|SetParent]](nil) afterward (this will remove the frame from its parents child list). If you just hide the frame without this additional step, frames created afterward will get a higher [[FrameLevel|framelevel]] than the hidden one. After a while, you will get frames at maximum [[FrameLevel|framelevel]] which are likely to be drawn in a distorted way (false order caused by equal [[FrameLevel|framelevel]]).

Latest revision as of 04:45, 15 August 2023

WoW API < CreateFrame

Creates a new UI frame.

newFrame = CreateFrame("frameType"[, "frameName"[, parentFrame[, "inheritsFrame"]]]);

Parameters[edit]

Arguments[edit]

frameType
String - Type of the frame to be created (XML tag name): "Frame", "Button", etc. See UIOBJECT_Frame
frameName
String - Name of the newly created frame. If nil, no frame name is assigned. The function will also set a global variable of this name to point to the newly created frame.
parentFrame
Frame - The frame object that will be used as the created Frame's parent (cannot be a string!) Does not default to UIParent if given nil.
inheritsFrame
String - a comma-delimited list of names of virtual frames to inherit from (the same as in XML). If nil, no frames will be inherited. These frames cannot be frames that were created using this function, they must be created using XML with virtual="true" in the tag.

Returns[edit]

newFrame
Frame - Reference to the newly created frame.

Example[edit]

Result: displays the horde and alliance insignias in the middle of the screen.

local f = CreateFrame("Frame",nil,UIParent)
f:SetFrameStrata("BACKGROUND")
f:SetWidth(128) -- Set these to whatever height/width is needed 
f:SetHeight(64) -- for your Texture

local t = f:CreateTexture(nil,"BACKGROUND")
t:SetTexture("Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Factions.blp")
t:SetAllPoints(f)
f.texture = t

f:SetPoint("CENTER",0,0)
f:Show()

Notes[edit]

  • CreateFrame() was added in 1.10
  • The fourth argument, inheritFrame, was added in 1.11
  • Frames CANNOT be deleted. Reuse them.
  • The frame's OnLoad handler, which will only exist if inherited, will be executed during the CreateFrame call. Any OnLoad script handlers set after CreateFrame() will not execute; consider adding any non-inherited OnLoad code directly after a CreateFrame call or use XML frames instead.
  • The returned frame reference is not strictly necessary because you can access frames by their global name if you give them one. For performance reasons, however, it is advised to store frame references in local variables and use those instead of getting frames by their global name.
  • See also API_Frame_SetScript to handle OnFunction scripts when not using a template.
  • The most common uses for this function are:
    • Creating anonymous event handlers.
    • Creating anonymous script handlers.
    • Creating frames on the fly for occasions when you don't know how many frames will be needed.
    • Creating infrequently used frames "on demand", for example: config dialogs, raid frames.
  • Never forget to unset the frames parent, if you want to get rid of a frame. I would suggest to hide the frame via frame:Hide() and to use frame:SetParent(nil) afterward (this will remove the frame from its parents child list). If you just hide the frame without this additional step, frames created afterward will get a higher framelevel than the hidden one. After a while, you will get frames at maximum framelevel which are likely to be drawn in a distorted way (false order caused by equal framelevel).