WoW:USERAPI GetReturnValues: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (It's new)
 
m (Move page script moved page USERAPI GetReturnValues to WoW:USERAPI GetReturnValues without leaving a redirect)
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{userfunc}} <!-- Leave this line in! -->
{{userfunc}} <!-- Leave this line in! -->
<center>'''{{PAGENAME}}''' ''- by [[User:Egingell|Egingell]] -''</center>


Returns a list of values in whatever order you specify in ''order'' and only those you specify in ''order''. Mostly useful for functions that return multiple values.
Returns a list of values in whatever order you specify in ''order'' and only those you specify in ''order''. Mostly useful for functions that return multiple values.
  ret1, ret2, ... retN = {{PAGENAME}}(order, functionCall)
  ret1, ret2, ... retN = {{PAGENAME}}(order, functionCall)
* ''Note:'' the efficiency of this approach is questioned. See the [[Talk:{{PAGENAME}}|{{discussiontab}} page]].


== Function Parameters ==
== Function Parameters ==
Line 41: Line 41:
<!-- Paste your function(s) here. Make sure to prefix each line with at least one space. You may want to replace some troublesome characters with HTML entities when necessary, e.g. "<" becomes &lt;, etc.. -->
<!-- Paste your function(s) here. Make sure to prefix each line with at least one space. You may want to replace some troublesome characters with HTML entities when necessary, e.g. "<" becomes &lt;, etc.. -->


local output = {}
  function {{PAGENAME}}(order, ...)
  function {{PAGENAME}}(order, ...)
     local input = {}
     output = wipe(output)
    for i = 1, select('#', ...) do
     order = tostring(order)
        input[i] = select(i, ...);
    end
    local output = {}
    order = tostring(order);
    for i=1, strlen(order) do
        local key = tonumber(strsub(order, i, i));
        if input[key] ~= nil then
            table.insert(output, input[key]);
        end
    end
    return unpack(output);
end
Variant
local {{PAGENAME}}_In;
local {{PAGENAME}}_Out;
function {{PAGENAME}}(order, ...)
    {{PAGENAME}}_In = {}
    for i = 1, select('#', ...) do
        {{PAGENAME}}_In[i] = select(i, ...);
    end
    {{PAGENAME}}_Out = {}
     order = tostring(order);
     for i=1, strlen(order) do
     for i=1, strlen(order) do
         local key = tonumber(strsub(order, i, i));
         local value = select(tonumber(strsub(order, i, i)), ...)
         if {{PAGENAME}}_In[key] ~= nil then
         if value ~= nil then
             table.insert({{PAGENAME}}_Out, {{PAGENAME}}_In[key]);
             table.insert(output, value)
         end
         end
     end
     end
     return unpack({{PAGENAME}}_Out);
     return unpack(output)
  end
  end


__NOTOC__
__NOTOC__
[[Category:User Defined Functions]]
[[Category: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.

User defined functions

Returns a list of values in whatever order you specify in order and only those you specify in order. Mostly useful for functions that return multiple values.

ret1, ret2, ... retN = USERAPI GetReturnValues(order, functionCall)
  • Note: the efficiency of this approach is questioned. See the discussion page.

Function Parameters

Arguments

(order, functionCall)
order
number or numeric string
e.g. 17 or "17"
functionCall
a function call that returns a list of values.
e.g GetItemInfo(itemLink)

Returns

the number of and order of return values depend on the number specified in order and on the function passed to functionCall

Example:1

local sName, iRarity, sType, iStackCount = USERAPI GetReturnValues(1368, GetItemInfo(16846));
message(sName..","..iRarity..","..sType..","..iStackCount);

Result:1

USERAPI GetReturnValues will return the first, third, sixth, and eighth return values (in that order) from GetItemInfo(16846)
A message box will pop-up with -
Giantstalker's Helmet,4,Armor,1.

Example:2

local reason, name = USERAPI GetReturnValues(61, GetAddOnInfo("Uber Addon"));
if reason then
    _ERRORMESSAGE(name.." didn't load.\nReason code: "..reason..".");
end

Result:2

USERAPI GetReturnValues will return the sixth and first return values (in that order) from GetAddOnInfo("Uber Addon")
A error message box will pop-up with -
Uber Addon didn't load.
Reason code: MISSING

Code

local output = {}
function USERAPI GetReturnValues(order, ...)
    output = wipe(output)
    order = tostring(order)
    for i=1, strlen(order) do
        local value = select(tonumber(strsub(order, i, i)), ...)
        if value ~= nil then
            table.insert(output, value)
        end
    end
    return unpack(output)
end