WoW:USERAPI wait: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(Created page with '{{userfunc}} <!-- Leave this line in! --> Call this function to wait a specified amount of time before running another function with the given parameters. This function is usefu...')
 
(→‎Code: Fixed to pass all the varargs. Still has trouble with nils)
Line 49: Line 49:
         else
         else
           count = count - 1;
           count = count - 1;
           f(p);
           f(unpack(p));
         end
         end
       end
       end
     end);
     end);
   end
   end
   tinsert(waitTable,{delay,func,...});
   tinsert(waitTable,{delay,func,{...}});
   return true;
   return true;
end
end

Revision as of 00:37, 29 June 2009

This page documents a user-defined function 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

Call this function to wait a specified amount of time before running another function with the given parameters. This function is useful when you rely on system messages to trigger your code as (especially with Guild related system messages) the system message is dispatched by the server sometimes before the results have been updated. Waiting to perform the next step of your code becomes easy with this function.

<PREFIX>_wait(delay, func [, param [,param [,...]]])

Function Parameters

Arguments

delay
Number - the amount of time to wait (in seconds) before the provided function is triggered
func
Function - The function to run once the wait delay is over.
param
any - A list of any additional parameters (of any assorted type) to pass to the provided function when it is triggered.

Returns

success
Boolean - true if it succeded in adding the wait to the wait table, If the first parameter is not a number, or the seconds parameter is not a function, any call to wait will automatically fail with a result of false.


Example

<PREFIX>_wait(2.5,<PREFIX>_printMSG,"Hello");

Result

returns true after the initial call
after a 2.5 seconds delay, the string "Hello" will be displayed in the default chat frame.

Code

local waitTable = {};
local waitFrame = nil;

function <PREFIX>__wait(delay, func, ...)
  if(type(delay)~="number" or type(func)~="function") then
    return false;
  end
  if(waitFrame == nil) then
    waitFrame = CreateFrame("Frame","WaitFrame", UIParent);
    waitFrame:SetScript("onUpdate",function (self,elapse)
      local count = #waitTable;
      local i = 1;
      while(i<=count) do
        local waitRecord = tremove(waitTable,i);
        local d = tremove(waitRecord,1);
        local f = tremove(waitRecord,1);
        local p = tremove(waitRecord,1);
        if(d>elapse) then
          tinsert(waitTable,i,{d-elapse,f,p});
          i = i + 1;
        else
          count = count - 1;
          f(unpack(p));
        end
      end
    end);
  end
  tinsert(waitTable,{delay,func,{...}});
  return true;
end