View Single Post
  #4  
Unread 10-22-2011, 01:14 PM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Oct 2010
Posts: 343
Quote:
Originally Posted by SanDBoX
perhaps, but I was under the impression that in lua anything declared in a function was local to the function by default, but worth a shot.
Lunarwtr is definately correct about the scoping. If you declare a function with a variable inside it, call the function and then reference the variable outside the function you will see that the variable is scoped outside the function (unless you declare it with "local").

You can do this interactively using the debug.lua class from the Cards plugin:
Start the Cards plugin. Start a game of "Ring". Enter the command "/ringgame debug" to start the interactive debugger. It is usually a good idea to select "Plugin Environment" at the top of the debug window but it's not necessary for this example.
Create a function "testfunc" by entering
Code:
testfunc=function() abcdef="hello" end
and click Execute. Then run the function by entering
Code:
testfunc()
and click Execute. At this point, we are no longer in the scope of the function, but if you enter
Code:
return abcdef
and click Execute, it displays "hello" in the standard chat.
You can see where the testfunc and abcdef exist in the object viewer at the bottom. If you selected "Global Environment" they will be defined just inside the "_G" global object. If you chose "Plugin Environment" you will have to expand the "GaranStuff" object, then the "Cards" object.


The recursive filling of the object viewer list in debug.lua may give you some insight into how to resolve your problem.

Last edited by Garan : 10-22-2011 at 01:21 PM.
Reply With Quote