WoW:User defined functions: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Move page script moved page User defined functions to User defined functions without leaving a redirect)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{wowapi}}
{{userfunc}}
These are cut-and-paste functions that you can use in your addons, submitted by WoWWiki contributors.  See also [[UI Snippets]].


These are cut-and-paste functions that you can use in your addons, submitted by WoWWiki contributors.
Additional snippets and useful ideas:<br />
&rarr; [[AddOn defined functions]]<br />
&rarr; [[User defined macros]]<br />
&rarr; [[User defined modules]]<br />


== API ==
== Reference ==
The following functions aim at extending the information WoW API provides; some are provided by specialized addons rather than being copy/pastable.
The following functions aim at extending the information WoW API provides; some are provided by specialized addons rather than being copy/pastable.
:[[GetSellValue]](item) - Returns an item's merchant sell price.
:[[GetMinimapShape]]() - Returns a string describing the shape of the minimap (square? round? and more)
:[[allAreType]](type,...) - Returns true if all the additional parameters are of the type specified.


== Arguments & Returns ==
=== API ===
: [[setArgs]](myTable, "name", ...) - remember list of arguments to use for a callback
:{{id|USERAPI|allAreType}}(type,...) - Returns true if all the additional parameters are of the type specified.
: [[getArgs]](myTable, "name", ...) - retreive stored list of arguments, plus optional extras
: [[GetReturnValues]](order, functionCall) - Get the return values from ''functionCall'' in any ''order'' you want.


== Color Functions ==
=== Arguments & Returns ===
:[[ColorGradient]](perc, R1, G1, B1, R2, G2, B2[, ...]) - Converts a percent value into a gradient from 2 or more RGB values
:{{id|USERAPI|setArgs}}(myTable, "name", ...) - remember list of arguments to use for a callback
:[[HexToRGB]]("string") - Converts a hex color string to RGB values (0-255)
:{{id|USERAPI|getArgs}}(myTable, "name", ...) - retreive stored list of arguments, plus optional extras
:[[HexToRGBPerc]]("string") - Converts a hex color string to RGB values (0.0-1.0)
:{{id|USERAPI|GetReturnValues}}(order, functionCall) - Get the return values from ''functionCall'' in any ''order'' you want.
:[[RGBToHex]](red, green, blue) - Converts a RGB value (0-255) into a hex string
:[[RGBPercToHex]](red, green, blue) - Converts a RGB value (0.0-1.0) into a hex string


== Cursor Functions ==
=== Color Functions ===
:[[GetCursorScaledPosition]]() - Return the exact position the cursor is at based on scale.
:{{id|USERAPI|ColorGradient}}(perc, R1, G1, B1, R2, G2, B2[, ...]) - Converts a percent value into a gradient from 2 or more RGB values
:{{id|USERAPI|HexToRGB}}("string") - Converts a hex color string to RGB values (0-255)
:{{id|USERAPI|HexToRGBPerc}}("string") - Converts a hex color string to RGB values (0.0-1.0)
:{{id|USERAPI|RGBToHex}}(red, green, blue) - Converts a RGB value (0-255) into a hex string
:{{id|USERAPI|RGBPercToHex}}(red, green, blue) - Converts a RGB value (0.0-1.0) into a hex string


== Event Functions ==
=== Cursor Functions ===
:[[User:Egingell/PLAYER MONEY|PLAYER_MONEY]] - Add a message to the chat frame when you gain or spend money.
:{{id|USERAPI|GetCursorScaledPosition}}() - Return the exact position the cursor is at based on scale.


== Frame Functions ==
=== Event Functions ===
: [[GetQuadrant]](frame) - Find which quadrant of the screen a frame lies in.
:{{id|USERAPI|PLAYER_MONEY}} - Add a message to the chat frame when you gain or spend money.
: [[GetUIParentAnchor]](frame) - Returns SetPoint args for the frame, anchor is relative to the nearest corner of the screen.
: [[Frame:RegisterEvents]](...) - Register '''n''' number of events at once.
: [[Frame:UnregisterEvents]](...) - Unregister '''n''' number of events at once.
: [[Frame:SetManyAttributes]](...) - Simple function to embed in a frame to set many atributes at once.
: [[UnregisterEventFromAllFrames]](event) - Tell all the frames listening for '''event''' to stop listening for it.
: [[addDropDownMenuButton]](uid, dropdown, index, title, usable, onClick [, hint]) - Adds a new button to a drop down (right-click popup) list of your choice.


== Guild Functions ==
=== Frame Functions ===
:[[GuildNameToIndex]](name, searchOffline) - Takes a name and converts it to their index within the guild for other guild functions.
:{{id|USERAPI|GetQuadrant}}(frame) - Find which quadrant of the screen a frame lies in.
:{{id|USERAPI|GetUIParentAnchor}}(frame) - Returns SetPoint args for a frame, anchor is relative to nearest corner of screen.
:{{id|USERAPI|Frame|RegisterEvents}}(...) - Register '''n''' number of events at once.
:{{id|USERAPI|Frame|UnregisterEvents}}(...) - Unregister '''n''' number of events at once.
:{{id|USERAPI|Frame|SetManyAttributes}}(...) - Simple function to embed in a frame to set many atributes at once.
:{{id|USERAPI|UnregisterEventFromAllFrames}}(event) - Tell all the frames listening for '''event''' to stop listening for it.
:{{id|USERAPI|addDropDownMenuButton}}(uid, dropdown, index, title, usable, onClick [, hint]) - Adds a new button to a drop down (right-click menu)


== Item Functions ==
=== Guild Functions ===
:[[EquipItemByLink]](link) - Equips the first matching item found in the player's bags.
:{{id|USERAPI|GuildNameToIndex}}(name, searchOffline) - finds the 'guild' player index for a player name, for other guild functions.


== Localization Tables ==
=== Item Functions ===
: [[LocalizedClassNames]] - Table of localized classes
:{{id|USERAPI|EquipItemByLink}}(link) - Equips the first matching item found in the player's bags.


== Map Functions ==
=== Localization Tables ===
:[[GetPlayerBearing]]() - Returns the player's current facing bearing based on minimap arrow
:{{id|USERAPI|LocalizedClassNames}} - Table of localized classes


== Menu Functions ==
=== Map Functions ===
:[[Context Menu Maker]] - Pseudo-Class. Add/Remove menu items and show the menu when you're ready.
:{{id|USERAPI|GetPlayerBearing}}() - Returns the player's current facing bearing based on minimap arrow


== Metatables ==
=== Metatables ===
: [[Memorizing table]] - A special table that calculates values as needed and saves them into itself
:{{id|USERAPI|Memorizing table}} - A special table that calculates values as needed and saves them into itself


== Number Functions ==
=== Number Functions ===
: [[round]](input, n) - Round '''input''' to '''n''' places.
:{{id|USERAPI|round}}(input, n) - Round '''input''' to '''n''' places.
: [[truncate]](number, n) - Truncate a number to n decimal places.
:{{id|USERAPI|truncate}}(number, n) - Truncate a number to n decimal places.


== Slash Function ==
=== Slash Function ===
: [[GetSlashFunc]] - Get an existing slash command function for hooking.
:{{id|USERAPI|GetSlashFunc}} - Get an existing slash command function for hooking.
: [[RunSlashCmd]] - Passes a slash command to the chatframe
:{{id|USERAPI|RunSlashCmd}} - Passes a slash command to the chatframe
: [[SlashCmdList AddSlashCommand|SlashCmdList_AddSlashCommand]](name, func, ...)
:{{id|USERAPI|SlashCmdList_AddSlashCommand}}(name, func, ...)


== String Functions ==
=== String Functions ===
:[[ChunkSplit]](string [, length [, endChars]]) &nbsp; - Split a string into groups of "length" each ending with "endChars" (identical to the [http://php.net/chunk-split PHP function] of the same name).
:{{id|USERAPI|ChunkSplit}}(string [, length [, endChars]]) - Splits string into groups of 'length' each ending with 'endChars' (identical to [http://php.net/chunk-split PHP] ChunkSplit).
:[[CountChars]](haystack, needle) &nbsp; - Return how many times ''needle'' is contained in ''haystack''.
:{{id|USERAPI|CountChars}}(haystack, needle) - Return how many times ''needle'' is contained in ''haystack''.
:[[GetWords]]("string") &nbsp; - Split words on space boundary, return table
:{{id|USERAPI|GetWords}}("string") - Split words on space boundary, return table
:[[printMSG]]("string") &nbsp; - Displays a custom message to the default chat frame, for the user to see (time, code and size saver).
:{{id|USERAPI|printMSG}}("string") - Displays a custom message to the default chat frame, for the user to see (time, code and size saver).
:[[strfindt]](tabCaptures, ...) &nbsp; - Wrapper for [[API strfind|strfind]]() that returns captures in a table - can be used in if() clauses!
:{{id|USERAPI|strfindt}}(tabCaptures, ...) - Wrapper for [[API strfind|strfind]]() that returns captures in a table. can be used in if() clauses!
:[[StringHash]]("string") &nbsp; - Create a fair-quality 32-bit hash of a string
:{{id|USERAPI|StringHash}}("string") - Create a fair-quality 32-bit hash of a string
:[[substr]]("string", start [, length]) &nbsp; - Imp [[API strsub|strsub]]. Returns a string starting from ''start'' to ''length'' characters from ''start'' (identical to the [http://php.net/substr PHP function] of the same name).
:{{id|USERAPI|substr}}("string", start [, length]) - Imp [[API strsub|strsub]]. Returns a string from ''start'' of ''length'' (identical to [http://php.net/substr PHP] substr).


== Table Functions ==
=== Table Functions ===
:[[EraseTable]](t) - Erase all values from a table
:{{id|USERAPI|EraseTable}}(t) - Erase all values from a table
:[[tinsertbeforeval]](tab, valBefore, val) &nbsp; - Insert one value before another (without knowing its index)
:{{id|USERAPI|tinsertbeforeval}}(tab, valBefore, val) - Insert one value before another (without knowing its index)
:[[tremovebyval]](tab, val) &nbsp; - Remove a value (without knowing its index)
:{{id|USERAPI|tremovebyval}}(tab, val) - Remove a value (without knowing its index)
:[[tcount]](tab) &nbsp; - Count table members (works on non-integer-indexed tables)
:{{id|USERAPI|tcount}}(tab) - Count table members (works on non-integer-indexed tables)
:[[tcopy]](tabTo, tabFrom) &nbsp; - Recursively copy contents of one table to another
:{{id|USERAPI|tcopy}}(tabTo, tabFrom) - Recursively copy contents of one table to another


== Time Functions ==
=== Time Functions ===
:[[GameTime:Get]]() &nbsp; - Get server time including seconds and milliseconds
:{{id|USERAPI|GameTime|Get}}() - Get server time including seconds and milliseconds
:[[wait]](delay,function [, param [,param [,...]]]) &nbsp; - Wait a specified amount of time before running a function with the given parameters.
:{{id|USERAPI|wait}}(delay,function [, param [,param [,...]]]) - runs a function (with paramenters) after specified amount of time before
:[[SecondsToDays]]() &nbsp; - Converts seconds to days/hours/minutes/seconds
:{{id|USERAPI|SecondsToDays}}() - Converts seconds to days/hours/minutes/seconds


== See also ==
== See also ==
* [[:Category:HOWTOs]] - must have cut-and-pasteable code snippets
* [[:Category:HOWTOs]] - must have cut-and-pasteable code snippets
[[Category:Interface customization]]
[[Category:User defined functions| User defined functions]]
[[Category:User defined functions| User defined functions]]

Latest revision as of 04:49, 15 August 2023

This page documents a <i>user-defined function</i> that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.

These are cut-and-paste functions that you can use in your addons, submitted by WoWWiki contributors.  See also UI Snippets.

Additional snippets and useful ideas:
AddOn defined functions
User defined macros
User defined modules

Reference[edit]

The following functions aim at extending the information WoW API provides; some are provided by specialized addons rather than being copy/pastable.

API[edit]

allAreType(type,...) - Returns true if all the additional parameters are of the type specified.

Arguments & Returns[edit]

setArgs(myTable, "name", ...) - remember list of arguments to use for a callback
getArgs(myTable, "name", ...) - retreive stored list of arguments, plus optional extras
GetReturnValues(order, functionCall) - Get the return values from functionCall in any order you want.

Color Functions[edit]

ColorGradient(perc, R1, G1, B1, R2, G2, B2[, ...]) - Converts a percent value into a gradient from 2 or more RGB values
HexToRGB("string") - Converts a hex color string to RGB values (0-255)
HexToRGBPerc("string") - Converts a hex color string to RGB values (0.0-1.0)
RGBToHex(red, green, blue) - Converts a RGB value (0-255) into a hex string
RGBPercToHex(red, green, blue) - Converts a RGB value (0.0-1.0) into a hex string

Cursor Functions[edit]

GetCursorScaledPosition() - Return the exact position the cursor is at based on scale.

Event Functions[edit]

PLAYER_MONEY - Add a message to the chat frame when you gain or spend money.

Frame Functions[edit]

GetQuadrant(frame) - Find which quadrant of the screen a frame lies in.
GetUIParentAnchor(frame) - Returns SetPoint args for a frame, anchor is relative to nearest corner of screen.
RegisterEvents(...) - Register n number of events at once.
UnregisterEvents(...) - Unregister n number of events at once.
SetManyAttributes(...) - Simple function to embed in a frame to set many atributes at once.
UnregisterEventFromAllFrames(event) - Tell all the frames listening for event to stop listening for it.
addDropDownMenuButton(uid, dropdown, index, title, usable, onClick [, hint]) - Adds a new button to a drop down (right-click menu)

Guild Functions[edit]

GuildNameToIndex(name, searchOffline) - finds the 'guild' player index for a player name, for other guild functions.

Item Functions[edit]

EquipItemByLink(link) - Equips the first matching item found in the player's bags.

Localization Tables[edit]

LocalizedClassNames - Table of localized classes

Map Functions[edit]

GetPlayerBearing() - Returns the player's current facing bearing based on minimap arrow

Metatables[edit]

Memorizing table - A special table that calculates values as needed and saves them into itself

Number Functions[edit]

round(input, n) - Round input to n places.
truncate(number, n) - Truncate a number to n decimal places.

Slash Function[edit]

GetSlashFunc - Get an existing slash command function for hooking.
RunSlashCmd - Passes a slash command to the chatframe
SlashCmdList_AddSlashCommand(name, func, ...)

String Functions[edit]

ChunkSplit(string [, length [, endChars]]) - Splits string into groups of 'length' each ending with 'endChars' (identical to PHP ChunkSplit).
CountChars(haystack, needle) - Return how many times needle is contained in haystack.
GetWords("string") - Split words on space boundary, return table
printMSG("string") - Displays a custom message to the default chat frame, for the user to see (time, code and size saver).
strfindt(tabCaptures, ...) - Wrapper for strfind() that returns captures in a table. can be used in if() clauses!
StringHash("string") - Create a fair-quality 32-bit hash of a string
substr("string", start [, length]) - Imp strsub. Returns a string from start of length (identical to PHP substr).

Table Functions[edit]

EraseTable(t) - Erase all values from a table
tinsertbeforeval(tab, valBefore, val) - Insert one value before another (without knowing its index)
tremovebyval(tab, val) - Remove a value (without knowing its index)
tcount(tab) - Count table members (works on non-integer-indexed tables)
tcopy(tabTo, tabFrom) - Recursively copy contents of one table to another

Time Functions[edit]

Get() - Get server time including seconds and milliseconds
wait(delay,function [, param [,param [,...]]]) - runs a function (with paramenters) after specified amount of time before
SecondsToDays() - Converts seconds to days/hours/minutes/seconds

See also[edit]