_G and Dynamic Variables

My background prior to about 6 weeks ago was Adobe Flash back to the Flash 3 and the Macromedia days. It was very common for some of the swf apps I wrote to use dynamic variables for repetative tasks. In ActionScript it would be something like this:

1
2
num = 1;
eval("box" + num) ==  10

One possible problem is you declared your swapAbilities function as local. The function won't be defined in other modules if you do that.

Also FYI, there is a built in Lua-ism for swapping. In Lua, you can do:
a=1
b=2
a,b = b,a
-- a is now 2, b is now 1

You don't need a temporary variable.

Brilliant!! I will incorporate this into the function. I will test your local theory on the function as well. There are probably much better ways to do this but I think I will stumble on and try to work it through. If anything it might teach me a few things. ;)

Thanks for taking the time to help!

Brian

Does removing the word "Local" make the function Global? If so, it didn't work. It's almost like the "_G" is for some reason not available to the external module. Is that possible?

1
2
3
4
_G[swapValue1],_G[stat] = _G[stat],_G[swapValue1] 
-- Where swapValue1 = "dex" and stat = "str", this FAILS
 
str,dex = dex,str -- These are global variables, work fine.

If you are using the Lua module system, I think there are visibility things that change the environment to insulate changes to _G to not contaminate your main program.

module(..., package.seeall) has something to do with this.

http://lua-users.org/wiki/LuaModuleFunctionCritiqued

Well, I am using:

1
module(..., package.seeall)
views:1889 update:2011/10/11 8:57:46
corona forums © 2003-2011