• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Tilemap Options and Problems

G

gadgetkk

Guest
Hello everyone,

I have started dipping into the world of tile maps since everyone seems think this is the bee's knees for GMS 2. While i don't understand much about how they work, i get the gist. So right now have a game that will randomly generate a map using Tiles on a tiles map layer (called tilemap). I will be wanting to allow the player to do a significant amount of manipulations to the tiles map as they will be able to create and destroy tiles while playing.

How can i achieve that?

Here is a code that i have when a tile is clicked:
var i = mouse_x/TILE;
var j = mouse_y/TILE;


if (map_grid[i,j] = WALL)
{
instance_create_layer(i,j,layer,obj_rock_to_mine);
var map_id = layer_tilemap_get_id(tilemap);
var mx = tilemap_get_cell_x_at_pixel(map_id, mouse_x, mouse_y);
var my = tilemap_get_cell_y_at_pixel(map_id, mouse_x, mouse_y);
var data = tilemap_get(map_id, mx, my);
var ind = tile_get_index(data);
data = tile_set_index(data, 0);
tilemap_set(map_id, data, mx, my);
}


The object gets created but the tiles don't go away. Now this is a copy/paste from the manual so it seems to be a bit repetitive in my mind to be done for every click, but once i can get it working i will fiddle with it more.

The error message i get is:
tilemap_get_cell_x_at_pixel() - couldn't find specified tilemap
tilemap_get_cell_y_at_pixel() - couldn't find specified tilemap
tilemap_get() - couldn't find specified tilemap
tilemap_set() - couldn't find specified tilemap

tilemap is a globalvar i use to call the tilemap in different places and it works fine in those other places. I appreciate anyone who can assist me.

Also, if there is an easier way to do what i want please let me know.

Also also, can anyone explain to me what "tiledata" is in the manual example? I have no idea what that is indicating and i can't find any information on that field anywhere.

Thanks.
 
Code:
layer_id= layer_get_id("Tilemap");
tilemap_id = layer_tilemap_get_id(layer_id);
layer_tilemap_get_id needs a layer id as argument, thats why it doesnt find the tilemap.

EDIT: overread the 2nd question, "tiledata" is the tile which is set.
 

CMAllen

Member
Code:
layer_id= layer_get_id("Tilemap");
tilemap_id = layer_tilemap_get_id(layer_id);
layer_tilemap_get_id needs a layer id as argument, thats why it doesnt find the tilemap.

EDIT: overread the 2nd question, "tiledata" is the tile which is set.
After digging through the code shown in the GMS2 documentation to do something similar for one of my own projects, I found the whole process absurdly convoluted and difficult to follow. The layers of functions needed to address the desired information is not what I would call 'good' design. So I find the confusion to be understandable.
 
Yeah, its a bit counterintuitive at first, but if one wants to make layers (in)visible or make shaders it starts making sense to differentiate between them.
Further, you only need to do it once in the create event.
 
G

gadgetkk

Guest
So I put in the layer line:
var layer_id = layer_get_id("Tile_Layer")
var map_id = layer_tilemap_get_id(layer_id);

And there is no change (other than getting an additional line on the error message).

Is there an easier way to do this where I can have a large room and not thousands of objects?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, if you are creating a layer it's a "blank" layer and has nothing on it. If you want to add tiles to the layer, you must first add a "tilemap element". This element is what you would then target to add the tiles, not the layer. If you add the tilemap layer in the room editor, then you are actually adding a layer with a tilemap element already on it...

Now, in your code you have this:

var map_id = layer_tilemap_get_id(tilemap);

What is "tilemap"? Is this the ID of the layer you want to target first? Ideally you should be doing this:

Code:
var _layer = layer_get_id("tilemap_layer"); // or whatever it is the layer is called in the room
or

Code:
var _layer = layer_create(0, "tilemap_layer"); // if the layer doesn't already exist
And then using the layer ID in the get tilemap function:

Code:
var _tilemap = layer_tilemap_get_id(_layer);
Once you have the layer element ID, all the tilemap functions should target that. Also, you don't need to do all that get/set data stuff. SImply set the tile with the index you want if you want to change it, and don't forget that index 0 is ALWAYS an empty tile. You only need to use the data get/set stuff if you are flipping or rotating tiles. :)

SO your final code should look more like this:

Code:
var _layer = layer_get_id("tilemap_layer");
var _tilemap = layer_tilemap_get_id(_layer);
var mx = tilemap_get_cell_x_at_pixel(_tilemap, mouse_x, mouse_y);
var my = tilemap_get_cell_y_at_pixel(_tilemap, mouse_x, mouse_y);
tilemap_set(_tilemap, 0, mx, my);
If you are going to be accessing the same layer and element all the time, then it's probably best if yo store their ID values in instance vars on create so that all you have to do is set the tile index at the position required, rather than keep getting the layer and element ID every time.

Hope that helps!
 
G

gadgetkk

Guest
I actually figured out what my issue was, and it was far more simple than i thought...

I had a draw event that was drawing the tile based on an array and i didn't change the array. so no matter what i was change the array was only drawing the 1 tile. I made a second if statement to draw other parts of the array and now it works perfectly... 3 days it took me...

Thanks everyone for your input and confirming for me that tilemaps are hard to understand.
 
G

gadgetkk

Guest
I have a very good and very valid reason... because i don't know what i'm doing!

I have actually meshed up 2 bits of information i'm using someones code that i found on youtube to randomly generate a map using tiles and then i'm using Shaun Spalding's tile collision code. This allows me to create large random maps with little to no computing power and very few objects.

If there is a better way to do it, i'm all ears.
 
If you dont need pathfinding in your game, the concept you have done is good enough already. One (or more) tilelayer for interaction/graphics and a collision tilelayer. If you manipulate one, you manipulate the collision layer in the same codeblock. More is not needed for the core concept.
 
Top