Game Development Community

Dynamical created tile layer not visible [solved]

by Max Kielland · in Torque Game Builder · 01/17/2013 (2:19 am) · 4 replies

Hi,

I want to add a new tile layer to my existing TileMap and fill it with some random background tiles.
Here is some test code:

function FactoryTileMap::onLevelLoaded(%this) {

    // Get gloab Tile Map
  %tileMap = $LastLoadedScene.getGlobalTileMap();
  
  // Check number of layers
  %layerCount = %tileMap.getTileLayerCount();
  
  // Create a new layer
  %layer = %tileMap.createTileLayer("10 10","10 10");
  
  // Check if the layer was created
  %layerCount = %tileMap.getTileLayerCount();
  
  // Get first layer
  //%layer = %tileMap.getTileLayer(0);
  
  // Place tile on layer
  %layer.setStaticTile("4 3", "TileMapImageMap",9);

}

When inspecting the %layerCount variable I can see that a new layer is created and added to the %tileMap, but if I place a tile on the new tile layer it is not shown.

If I select the first layer (layer index 0) the tile shows up.
Do I have to do something to show the new layer on screen?

[EDIT]
There was some copy/paste errors in the test code.

#1
01/17/2013 (6:18 am)
Perhaps call .setVisibie(true)? Is the whole tilemap invisible or just the layer?
#2
01/17/2013 (6:57 am)
No, it does nothing.

I checked the visible value and it is already set to true after it has been created.

%visible = %layer.getVisible();
%layer.setVisible(true);

More ideas to try?
#3
01/17/2013 (7:27 am)
Okay I solved it :)

The new tile layer's scene size was not updated to the bigger grid size I had. My test tile was draw outside the scene view.

The following row of code solved it:

%layer.Size = "100 100";

So to create a background layer of randomly selected tiles I made this little snippet:

// Get global Tile Map
  %tileMap = $LastLoadedScene.getGlobalTileMap();
  
  // Create a new layer
  %layer = %tileMap.createTileLayer("10 10","10 10");
  %layer.Size = "100 100";
  %layer.Layer = 20;

  // Populate layer with tiles
  for(%y=0; %y<%layer.getTileCountY(); %y++) {
    for(%x=0; %x<%layer.getTileCountX(); %x++) {
      %layer.setStaticTile(%x SPC %y, "TileMapImageMap",getRandom(0,9));
    }
  }
#4
01/17/2013 (9:35 am)
Sweet. Thanks for sharing - my idea was just a shot in the dark anyway; visibility for scene objects usually defaults to true....