WoW:API RunScript: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Robot: converting/fixing HTML)
m (Move page script moved page API RunScript to API RunScript without leaving a redirect)
 
(3 intermediate revisions by 2 users not shown)
Line 6: Line 6:
== Parameters ==
== Parameters ==
=== Arguments ===
=== Arguments ===
:("script")
:;script : string - code to be executed


:;script : String - The code which is to be executed.
=== Returns ===
=== Returns ===
:''nil''
:none


== Example ==
== Example ==
:To define a function dynamically you could do:
:To define a function dynamically you could do:
<!-- begin code -->
<pre>
local retExpr = '"Hello " .. UnitName("target")';
local retExpr = '"Hello " .. UnitName("target")';
RunScript("function My_GetGreeting() return " .. retExpr .. ";end");
RunScript("function My_GetGreeting() return " .. retExpr .. ";end");
<!-- end code -->
</pre>
==== Result ====
 
=== Result ===
:The My_GetGreeting() function will be defined to return Hello followed by the name of your target.
:The My_GetGreeting() function will be defined to return Hello followed by the name of your target.


Line 26: Line 26:
: 1. It'll do whatever you tell it, that includes calling functions, setting variables, whatever.
: 1. It'll do whatever you tell it, that includes calling functions, setting variables, whatever.


: 2. It does things globally, so to get data out of it you have to pollute at least one global variable. This isn't a big deal but it's mildly annoying.
: 2. Errors in the script string produce the error popup (at least, they produce the UI_ERROR_MESSAGE event).
 
: 3. Errors in the script string produce the error popup (at least, they produce the UI_ERROR_MESSAGE event).


: On the other hand, it's invaluable if you need to run arbitrary code, or do self-generating code.
: On the other hand, it's invaluable if you need to run code that is input by the player at run-time, or do self-generating code.


== See Also ==
== See Also ==


* The standard Lua function [[API loadstring]], which can overcome all of the problems of RunScript described above.
* The standard Lua function [[API loadstring]], which can overcome all of the problems of RunScript described above.

Latest revision as of 04:47, 15 August 2023

WoW API < RunScript

Execute a string as LUA code.

RunScript("script")

Parameters[edit]

Arguments[edit]

script
string - code to be executed

Returns[edit]

none

Example[edit]

To define a function dynamically you could do:
local retExpr = '"Hello " .. UnitName("target")';
RunScript("function My_GetGreeting() return " .. retExpr .. ";end");

Result[edit]

The My_GetGreeting() function will be defined to return Hello followed by the name of your target.

Details[edit]

This function is NOT recommended for general use within addons for a number of reasons:
1. It'll do whatever you tell it, that includes calling functions, setting variables, whatever.
2. Errors in the script string produce the error popup (at least, they produce the UI_ERROR_MESSAGE event).
On the other hand, it's invaluable if you need to run code that is input by the player at run-time, or do self-generating code.

See Also[edit]

  • The standard Lua function API loadstring, which can overcome all of the problems of RunScript described above.