WoW:API loadstring: Difference between revisions

m
no edit summary
({{luaapi}})
mNo edit summary
Line 1: Line 1:
{{luaapi}}
{{wowlua}}
Parse a string as Lua code and return it as a reference to a function.
Parse a string as Lua code and return it as a reference to a function.
  func, errorMessage = loadstring("luaCodeBlock"[, "chunkName"]);
  func, errorMessage = loadstring("luaCodeBlock"[, "chunkName"]);


== Arguments ==
== Arguments ==
;luaCodeBlock:String - a string of Lua code. Can be very long.
;luaCodeBlock
;chunkName:String - optionally name the code block. Will be shown as the "file name" in error messages. If not given, the file name will be "[string: ''first line of your Lua code here...'']".
:String - a string of Lua code. Can be very long.
;chunkName
:String - optionally name the code block. Will be shown as the "file name" in error messages. If not given, the file name will be "[string: ''first line of your Lua code here...'']".


=== Returns ===
=== Returns ===
;func:Function reference - nil if there was a syntax error in the code block
;func
;errorMessage:String - will contain an error message if ''func'' was nil.
:Function reference - nil if there was a syntax error in the code block
;errorMessage
:String - will contain an error message if ''func'' was nil.


== Examples ==
== Examples ==
Line 20: Line 24:




=== Catching all errors ===  
=== Catching all errors ===
  local func, errorMessage = loadstring("DEFAULT_CHAT_FRAME:AddMessage(\"Hello, World!\")");
  local func, errorMessage = loadstring("DEFAULT_CHAT_FRAME:AddMessage(\"Hello, World!\")");
  if(not func) then
  if(not func) then