Variable Grid Code

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Variable Grid Code

Now that i have the basics of my button Grid Macro worked out, I need to take it the next step and create the number of cells / buttons based on the count of a list.

I am unsure how I go about coding the loop to handle the variance of the number of buttons.

For example...

If the list count = 32 I would end up with a 6x6 grid. (evenly distributed)
If the list count = 25 i would end up with a 5x5 grid.
If the list count = 24 I would end up with a 5 X 5 grid with the last row only having 4.

Any pointers how I achive this type of dynamic grid building?

CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: Variable Grid Code

Code: Select all

``[r: gridSize = 1+floor(squareroot(listCount))]``

Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Variable Grid Code

CoveredInFish wrote:

Code: Select all

``[r: gridSize = 1+floor(squareroot(listCount))]``

Also, you may wish to know how many cells are in the final row, which you can do the following the calculate. Depends on how you're implementing the table, but for me I'd need to know this number.

Code: Select all

``[r: lastRowCellsNumber = noOfCells - ((gridSize - 1) * gridSize)]``

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: Variable Grid Code

So using the following:

Code: Select all

``[r: TTCount = listCount(ttList)]<br>[r: gridSize = 1+floor(squareroot(TTCount))][r,count(gridSize,""),code:{  <tr>    [r, for(i,1,gridSize,1,""), code:{       [h: tokenName = "image:Treasure_Token_1"]      [r: tv = listGet(ttList, num)]      [r: button = strformat('<td><input type="image" src="%s" name="%s" value= "%s"></td>',getImage(tokenName),"btnChoice" + num, tv)]      [h: num = num +1]       } ]  </tr>}]``

The list size at the start is 32

Im only getting 30 buttons. 5(cols) x 6(rows) grid.

I am trying to get 32 buttons. 5 (cols) x 7(rows)-Last row would only have 2 buttons.

CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: Variable Grid Code

You have to start the for-loop at 0 or walk to gridsize+1

Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Variable Grid Code

CoveredInFish wrote:You have to start the for-loop at 0 or walk to gridsize+1

You often encounter this when for looping through arrays or objects, because the first index starts at 0, and the final entry is (length - 1).