How to reference an object including an argument?

As a newbie, I’m trying to convert one of my AS2 Flash applications to Lua/Corona to get my feet wet, but am getting hung up on how to reference an object (movie clip in this instance) in a function using an argument.

The original actionscript code looked something like this:

["mymovieclip"+myarg].gotoAndStop(2)  

The square brackets merged (concatenated?) “mymovieclip” and the passed in argument just fine when called; for example if myarg=1, the end result would be mymovieclip1.gotoAndStop(2) - which worked in making the MC with the instance name “mymovieclip1” go to and stop at frame 2.

I can’t seem to figure out how to accomplish the same task in Lua. Doing something like the following…

function myFunction(myarg)  
 mymovieclip[myarg]:stopAtFrame(2)  
end  

…doesn’t work. When I call the function myFunction(1) I get a runtime error that it can’t index ‘mymovieclip’ (the argument is not being included when calling the object.)

Can anyone point me in the right direction on how to do this properly?

My apologies if it’s as simple as I’d imagine, but I can’t seem to find the appropriate reference and have tried quite a few things to no avail. [import]uid: 49694 topic_id: 20892 reply_id: 320892[/import]

In Lua, mymovieclip[1] is accessing index 1 of the table (an array, for all intents and purposes) mymovieclip, not the nonexistent object “mymovieclip1”

When instantiating your objects, you can add them to a table of objects, and pass the index as the argument to your function:

  
local mymovieclips = {}  
local foo = require("objectclass")  
  
for i=1,10 do  
 mymovieclips[i] = foo.new() --reference to instaniated object  
end  
  
local myfunction = function(arg)  
 mymovieclips[arg]:doSomething()  
end  
  
myfunction(1) --make object 1 do something  
  

Lua being Lua, there are a bunch of different ways to achieve the same result, but this one should be pretty close to what you’d see in AS. [import]uid: 120 topic_id: 20892 reply_id: 82355[/import]

Thank you kindly for the reply Bryan! That indeed does do the trick.

While there’s only 5 possible arguments for this particular function so it’s certainly no trouble making the table/array as you’ve suggested, I am still curious for future reference if the table is necessary to reference an object including an argument?

The actual code (working now - thanks!)

[lua]buttonHold1:addEventListener( “tap”, buttonHold1 )

function buttonHold1:tap( event )
holdDraw(1)
end[/lua]

[lua]function holdDraw(cardNum)

local myButtons = { buttonHold1, buttonHold2, buttonHold3, buttonHold4, buttonHold5 }

if hold[cardNum] == true then
hold[cardNum] = false;
myButtons[cardNum]:stopAtFrame(1)

elseif hold[cardNum] == false then
hold[cardNum] = true;
myButtons[cardNum]:stopAtFrame(2)

end
end [/lua]

Again, works great - I’m just thinking under different circumstances it may get a little convoluted having to make tables first in order to reference things anytime an argument is passed in.

If that’s the way it is in Lua so be it, just wanted to make sure there was no equivilent to actionscripts simple [“mymovieclip”+myarg] using a different syntax.

Thanks again.
[import]uid: 49694 topic_id: 20892 reply_id: 82363[/import]

Glad it’s working for you matthew… now that I see what you’re trying to do, maybe this is more helpful:

  
--empty tables  
local foo = {}  
local bar = {}  
  
--create table functions  
foo.new = function()  
  
 print("foo")  
  
end  
  
bar.new = function()  
  
 print("bar")  
  
end   
--create function to call .new() for the table passed as an argument  
local baz = function(ref)  
  
 --print out the address of the passed table  
 print(tostring(ref))  
  
 --execute the .new function of the passed table  
 ref.new()  
  
end  
  
print(tostring(foo))  
  
print(tostring(bar))  
--executes foo.new()  
baz(foo)  
  
executes bar.new()  
baz(bar)  
  

And this:

[code]

–function executes .fade() of the table passed in
local buzz = function(event)
print("===")

–mem address of passed table, should be identical to img
print("event: " … tostring(event.target))

–call the table function to decrease alpha
event.target.fade()

–if you dont want to use a table function, you could do:
–event.target.alpha = event.target.alpha - 0.2

end

–mem address of buzz table
print("buzz: " … tostring(buzz))
local img = display.newImage(“path_to_image.png”)

–table function to decrease alpha of the image
img.fade = function()

img.alpha = img.alpha - 0.2

end

–mem address of img
print("img: " … tostring(img))

–add touch listener
img:addEventListener(“touch”, buzz)

[/code] [import]uid: 120 topic_id: 20892 reply_id: 82378[/import]

Cheers Bryan, much appreciated! [import]uid: 49694 topic_id: 20892 reply_id: 82436[/import]