Concatenate variable name?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
PolyphonicBoy
Prole
Posts: 3
Joined: Mon Jan 24, 2022 2:59 pm

Concatenate variable name?

Post 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.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Concatenate variable name?

Post 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*
PolyphonicBoy
Prole
Posts: 3
Joined: Mon Jan 24, 2022 2:59 pm

Re: Concatenate variable name?

Post by PolyphonicBoy »

Thanks for you help.

So how would I increment the number (i)?
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Concatenate variable name?

Post 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
PolyphonicBoy
Prole
Posts: 3
Joined: Mon Jan 24, 2022 2:59 pm

Re: Concatenate variable name?

Post by PolyphonicBoy »

Fantastic! Thanks so much. Working like a dream! :awesome:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests