Scene2D UI - make grid

Hello, i am trying to make an inventory for my game, and for that i need to make a grid.
I am trying to get this done with the table -

table = new Table();
table.setFillParent(true);
stage.addActor(table);

Where should i go from here?
I want my grid / inventory to look similar to this -

http://hydra-media.cursecdn.com/minecraft.gamepedia.com/thumb/a/a2/2011-07-18_12.55.56.png/256px-2011-07-18_12.55.56.png

Funny enough this is actually already done, when you create a table it has empty cells. All you would need to do is set a fixed table size, say 200x200px, you could fit 10 actors in each row if they are 20px each, once it gets to the end it starts on a new collumn.

All you would need to do is occupy the table with xx amount of placeholder actor, like have a class called EmptySlot.

There is a way to fill the table without using loop or to hardcode everything?
Just something like this -

table.fill(howManyCells, cellSize)

I am not aware of anything like that… I think you would be best off creating your own method that takes in your desired parameters and handles mass creation of some actor.

Why don’t you just make your own grid? :smiley:

I bet Notch made those “grids” himself.

I really don’t like the look of all those static methods… I too once went down that path as i began game dev over a year ago I have come so much since then. Don’t let this happen to yourself

Everything you could want to know about Table <-- This is the same Table that is in scene2d ui

Quick take at a fill() method:


public void fill(Table t, Image blankSlot, int columns, int rows) {
      for(int r = 0; r < rows ; r++) {
            for(int c = 0; c < columns; c++)
                  table.add(blankSlot).pad(2);
            table.row();
      {
}

Take a good look through those docs, they’re good.

I forgot to update you guys, but i used his idea to create my grid for the inventory. and it goes pretty nice.
the only downside is that the code is really dirty, and there are too much data to calculate, but its nothing that some “clean ups” won’t fix.

Thanks :wink:

If you don’t mind share the code, I am sure someone in the future who looks at the post would appreciate it