Page 1 of 1

Concatenate variable name?

Posted: Mon Jan 24, 2022 3:16 pm
by PolyphonicBoy
Very new to this, so I hope I'm explaining myself well.

I'm creating a Picross game in love2d. I have the different puzzles saved in arrays. For example...

Code: Select all

puzzle2 = {}
  puzzle2.xClues = {11,43,231,111,121,112,211,51,11,4}
  puzzle2.yClues = {5,22,111,211,42,121,111,21,111,7}
  puzzle2.grid = {0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,0,0,1,1,1,1,0,0,0,1,1,0,
            0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,0}
When the player completes a puzzle I want the next puzzle to become the current puzzle. Something like...

currentPuzzle.grid = puzzle2.grid
then
currentPuzzle.grid = puzzle3.grid

I think I need a way to concatenate the variable name. I have a variable called level, which gets incremented when a puzzle is completed. So I was thinking something like...

currentPuzzle.grid = (puzzle ..level.. .grid)

Obviously that syntax wouldn't work. But I'm hoping someone can help make this work.

Re: Concatenate variable name?

Posted: Mon Jan 24, 2022 4:04 pm
by GVovkiv

Code: Select all

levels = {}
levels[1] = {}
levels[1].xClues = {11,43,231,111,121,112,211,51,11,4}
levels[1].yClues = {5,22,111,211,42,121,111,21,111,7}
levels[1].grid = {0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,0,0,1,1,1,1,0,0,0,1,1,0,
            0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,0}

currentPuzzle.grid = levels[i].grid -- where i in numeric value
something like that, uh?
contain levels in numeric array, not via *key - value*

Re: Concatenate variable name?

Posted: Mon Jan 24, 2022 4:10 pm
by PolyphonicBoy
Thanks for you help.

So how would I increment the number (i)?

Re: Concatenate variable name?

Posted: Mon Jan 24, 2022 4:21 pm
by GVovkiv
PolyphonicBoy wrote: Mon Jan 24, 2022 4:10 pm Thanks for you help.

So how would I increment the number (i)?
well:

Code: Select all

curLevel = 1

levels = {}
levels[1] = {}
levels[1].xClues = {11,43,231,111,121,112,211,51,11,4}
levels[1].yClues = {5,22,111,211,42,121,111,21,111,7}
levels[1].grid = {0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,0,0,1,1,1,1,0,0,0,1,1,0,
            0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,0}
            levels[1] = {}
levels[2].xClues = {11,43,231,111,121,112,211,51,11,4}
levels[2].yClues = {5,22,111,211,42,121,111,21,111,7}
levels[2].grid = {0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,0,0,1,1,1,1,0,0,0,1,1,0,
            0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,0}

currentPuzzle.grid = levels[curLevel].grid
like, use value for controlling what exactly level you have right now
and just change it when you need:

Code: Select all

curLevel = curLevel + 1
curLevel = curLevel - 1
curLevel = 102

Re: Concatenate variable name?

Posted: Mon Jan 24, 2022 5:06 pm
by PolyphonicBoy
Fantastic! Thanks so much. Working like a dream! :awesome: