generate/target variable names dynamically

Hi - I’ve just started playing around with lua and have found myself wanting to generate/target variable names dynamically. I know I’m going about this in the wrong way but want to get stuck in.

So can you target a series of variables or object ids in a loop along the lines of this?

“cSlide”+i = event.target.id+1;

Code below is what I’m playing around with - click on a panel and it changes to the next one, currentSlide is stored from the event.target.id in cSlide# (from the parent group id).

Thanks for any advice.

local math_abs = math.abs

local cw, ch, ox, oy = display.contentWidth, display.contentHeight, math_abs(display.screenOriginX), math_abs(display.screenOriginY)

display.setStatusBar( display.DefaultStatusBar )

– declare vars

local cSlide1 = 1;

local cSlide2 = 1;

local cSlide3 = 1;

local panels = display.newGroup();

local rect = display.newRect( ox, oy, cw, ch );

panels:insert(rect);

local function slideTouch(event)

        local phase = event.phase ; local eventX = event.x ; local eventY = event.y

        if ( “began” == phase ) then

            print(event.target.name…" “…event.target.id…” pressed");

            

            tempX = event.target.parent.x;

            event.target.parent.x = tempX - cw;    

        end

end

local function setup()

    for j=1,3 do

        cSlideGroup = display.newGroup();

        cSlideGroup.id = j;

        cSlideGroup.name = “group”;

        for i=1,4 do

            thisSlideGroup = display.newGroup();

            thisSlideGroup.id = i;

            thisSlideGroup.name = “slide”

            local rect = display.newRect( ox, oy, cw, ch/3 );

            thisSlideGroup:insert( rect );

            rect:setFillColor( j*55, 255-i*55, 220 );

            cSlideGroup:insert(thisSlideGroup);

            

            thisSlideGroup.x = (cw * (i-1));

            thisSlideGroup:addEventListener( “touch”, slideTouch )  --core touch listener (only touch listener)

        end

        cSlideGroup.y = (ch/3 * (j-1));

    end

    

    

end

setup();

Lua doesn’t have dynamic variable names.  The closest you can come is to use tables either like an array with a numeric index:

cSlide[1]

cSlide[2] etc.

Or you can build dynamic key names like:

cSlide[“fred” + index]

to reference cSlide[“fred1”]

Thanks - that’s made things much clearer.

Lua doesn’t have dynamic variable names.  The closest you can come is to use tables either like an array with a numeric index:

cSlide[1]

cSlide[2] etc.

Or you can build dynamic key names like:

cSlide[“fred” + index]

to reference cSlide[“fred1”]

Thanks - that’s made things much clearer.