I'm having some issues with inheritance for one of my libGDX projects, I'm making a game similar to (very stripped down) Terraria or Starbound and I am using the TiledMap to load the world, everythings generating fine but the problem comes when I try to place blocks and from my experimenting it seems there's an issue with inheritance that I can't seem to fix. I have a Block class (extends Cell) and and InventoryObject class with two children, InventoryBlock and InventoryItem. I tried to implement the blocks similar to the Minecraft code, and I am having some troubles, destroying blocks works fine and I have used a similar system so I'm guessing it's something simple that I'm over looking. Any help would be appreciated :)

Block.java (parts of)

public class Block extends Cell{

private String name;
private TextureRegion tex;
private boolean hasInventory;
private int maxStackSize = 200;
private static TiledMapTileSet tileset;
private InventoryObject dropped;
Block[] blocks = new Block[256];

public Block(TiledMap map, Material material){
    System.out.println(map == null);
    tileset = map.getTileSets().getTileSet(0);
//each of these e.g. BlockStone, BlockDirt, is an individual class.
public static final Block stone = new BlockStone(Play.map, Material.STONE);
public static final Block dirt = new BlockDirt(Play.map, Material.DIRT);
public static final Block grass = new BlockGrass(Play.map, Material.GRASS);
public static final Block air = new BlockAir(Play.map, Material.AIR);
public static final Block leaf = new BlockLeaf(Play.map, Material.LEAF);
public static final Block log = new BlockLog(Play.map, Material.LOG);


public class InventoryObject extends Texture{

private Texture tex;
private int MaxStackSize;
private String name;
private Block block;

public InventoryObject(String name, int MaxStackSize, Block block){
    super(Gdx.files.internal("tiles/" + name + ".jpg"));
    tex = new Texture(Gdx.files.internal("tiles/" + name + ".jpg"));
    this.MaxStackSize = MaxStackSize;
    this.name = name;
    this.block = block;
}//getters and setters below

InventoryBlock.java public class InventoryBlock extends InventoryObject{

private int maxStackSize;

public InventoryBlock(String name, int maxStackSize, Block block){
    super(name, maxStackSize, block);

InventoryBlock[] InventoryBlocks = new InventoryBlock[256];

public static final InventoryBlock stone = new InventoryBlockStone("Stone", 200, Block.stone);
public static final InventoryBlock dirt = new InventoryBlockDirt("Dirt", 200, Block.dirt);
public static final InventoryBlock grass = new InventoryBlockGrass("Grass", 200, Block.grass);
public static final InventoryBlock leaf = new InventoryBlockLeaf("Leaf", 200, Block.leaf);
public static final InventoryBlock log = new InventoryBlockLog("Log", 200, Block.log);

I'm wondering if it's an issue with how I'm parsing the block into the InventoryBlocks, When I try to access an InventoryBlock's corresponding block to place it, it's just returning null. any ideas?

This is the create block method:

public void placeBlock(int x, int y, TiledMapTileLayer layer, Inventory inventory){
    if(layer.getCell(x, y).getTile() != null && layer.getCell(x, y).getTile().getId() == Block.air.getTile().getId()){

        if(inventory.getStack(player.getSelected()) != null){
            InventoryBlock block = (InventoryBlock)inventory.getStack(player.getSelected()).getObj();
            layer.setCell(x, y, block.getBlock()); 
            inventory.removeItem(player.getSelected(), 1);

The InventoryBlockStone/Dirt etc. simply extend the InventoryBlock class and have a constructor calling super(String name, int maxStackSize, Block block).

I tried echoing out the name of the InventoryBlock and that works fine, so It's just the Block object which is null. Sorry for the long question but I'm stumped, If you need any more information just ask and I'll add it to the question :)


UPDATE: I tried printing the value of Block.stone in the InventoryBlock constructor and it's returning null so could it be something to do with the order the classes are called? Any help would be appreciated

  • 1
    \$\begingroup\$ I'm having trouble following. A minimal self-contained example would help. \$\endgroup\$ – Anko Sep 1 '15 at 23:49
  • \$\begingroup\$ Do you actual need all those bazillion classes ? Your implementations feels very weird to me. I would just have an enum STONE, DIRT, etc, not make a class for every single thing, it is such an overkill and makes something really simple, real hard. Pretty sure you need to redo this... Also why does the Block contain an array of 256 blocks ? As far as your question goes, what does the BlockStone constructor look like ? \$\endgroup\$ – dimitris93 Sep 2 '15 at 4:59
  • \$\begingroup\$ What does "printing the value of Block.stone is returning null" mean ? Does it give you a null exception ? At any case you have identified that part of the problem has to be in the BlockStone creation process. Is Play.map initialized when you call the BlockStone constructor ? I think that should be the problem here. You should show us the BlockStone constructor as well. \$\endgroup\$ – dimitris93 Sep 2 '15 at 5:13
  • \$\begingroup\$ This is really not a place where you give us broken code and we fix it for you. The problem is it won't help anyone except you. \$\endgroup\$ – nikoliazekter Sep 4 '15 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.