_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:

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

The variable box1 is now equal to 10

or in a dynamic movieclip

num = 1;  
MovieClip[num].gotoAndStop(10)  

MovieClip1 is now on frame 10
Fast forward to Lua. I was looking for the same technique in my Corona app. I bought the “Programming in Lua” 2nd edition and it talks about how to do this on p. 129. I followed this and my function looks something like this:

-- Swap ability function  
local function swapAbilities(stat)  
 local ability1 = \_G[swapValue1]  
 \_G[swapValue1] = \_G[stat]  
 \_G[stat] = ability1  
 \_G[stat.."\_object"].text = \_G[stat]   
 \_G[swapValue1.."\_object"].text = \_G[swapValue1]  
 swapValue1 = nil  
end  

where “swapValue1” and “stat” are Global variables.

(I know this might not be the best way to do this but bear with me… :slight_smile:

Using just “main.lua” this works like a CHAMP!! but, the moment I use this code in another module, it fails! Even if I declare the variables in the same module. I have checked in the terminal and everything is there, i.e. the variables exist but using _G to dynamically work with the variables fails in the external modules.

Any ideas? (I’m sure it’s Operator Error)

Thanks!

Brian [import]uid: 71093 topic_id: 14545 reply_id: 314545[/import]

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.
[import]uid: 7563 topic_id: 14545 reply_id: 53814[/import]

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. :wink:

Thanks for taking the time to help!

Brian [import]uid: 71093 topic_id: 14545 reply_id: 53816[/import]

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?

\_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.  

Though it doesn’t fail using the “main.lua” and no external modules.

Scratching Head in Florida :slight_smile:

Brian [import]uid: 71093 topic_id: 14545 reply_id: 53824[/import]

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
[import]uid: 7563 topic_id: 14545 reply_id: 53828[/import]

Well, I am using:

module(..., package.seeall)  

in my external modules. I borrowed some code from the iDeveloper blog maintained by Beebe Games. It is a simple method for managing scenes and works quite nicely! Once I separated my code from a single to multiple scenes it broke on this part and this part only. I can avoid using the _G if it might cause a problem.

I read the article and it caused my brain to hurt but, I get the gist.

Thanks for the help and I hope they get this solved soon!

Brian [import]uid: 71093 topic_id: 14545 reply_id: 53903[/import]

Think of it again, and you will find that is *could* be easier and if you model it well, you could use arrays instead of mucking around with the _G variables, you can have a read of this article

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14545 reply_id: 56805[/import]