GML Replacing all tiles on tilemap with solid object

M

mpolder

Guest
I've been trying to make a platformer with an object that I use as a solid wall for any solid objects. Instead of making the whole stage it'd be easier to replace all the tiles on the tilemap with the solid automatically so i dont have to update it all the time. I've made some progress (i think) but got stuck. Does anyone know what is going wrong?

Just a bit of clarification, this code is meant to loop through all the layers and find the ones starting with "S_" (meaning they're solid) and place objects on every single tile that the layer contains. Right now all it does is find the layer and fill the entire stage with the object since I didnt find a way to actually see if the cell on the tilemap was "occupied".

Code:
///Spawn solidblocks
var l = layer_get_all();
for (var i = 0; i < array_length_1d(l); i++;) {
    var lName = layer_get_name(l[i]);
    if(string_pos("S_", lName) == 1) {
        for (var xx = 0; xx<=tilemap_get_width(l[i]);xx+=1) {
        for (var yy = 0; yy<=tilemap_get_height(l[i]);yy+=1) {
            var ted = tilemap_get(l[i], xx, yy);
            var obj = instance_create_layer(xx*32, yy*32, 1, obj_solid);
            //show_message(string(xx) + "-" + string(yy));
            with(obj) {
                image_xscale = tilemap_get_tile_width(ted) / sprite_get_width(spr_solid);
                image_yscale = tilemap_get_tile_height(ted) / sprite_get_height(spr_solid);
            }
        }
        }
    }
}
I appreciate any help :)
 
S

Shawn Shipton

Guest
I've been trying to make a platformer with an object that I use as a solid wall for any solid objects. Instead of making the whole stage it'd be easier to replace all the tiles on the tilemap with the solid automatically so i dont have to update it all the time. I've made some progress (i think) but got stuck. Does anyone know what is going wrong?

Just a bit of clarification, this code is meant to loop through all the layers and find the ones starting with "S_" (meaning they're solid) and place objects on every single tile that the layer contains. Right now all it does is find the layer and fill the entire stage with the object since I didnt find a way to actually see if the cell on the tilemap was "occupied".

Code:
///Spawn solidblocks
var l = layer_get_all();
for (var i = 0; i < array_length_1d(l); i++;) {
    var lName = layer_get_name(l[i]);
    if(string_pos("S_", lName) == 1) {
        for (var xx = 0; xx<=tilemap_get_width(l[i]);xx+=1) {
        for (var yy = 0; yy<=tilemap_get_height(l[i]);yy+=1) {
            var ted = tilemap_get(l[i], xx, yy);
            var obj = instance_create_layer(xx*32, yy*32, 1, obj_solid);
            //show_message(string(xx) + "-" + string(yy));
            with(obj) {
                image_xscale = tilemap_get_tile_width(ted) / sprite_get_width(spr_solid);
                image_yscale = tilemap_get_tile_height(ted) / sprite_get_height(spr_solid);
            }
        }
        }
    }
}
I appreciate any help :)
I can only say, I don't think it can be done any better than this for the collision portion...

You could automatically place collision tiles on another layer if the other layers you are testing contains the tile you are testing for, then use the GMWolf tutorial technique for the collisions.
 

Jakylgamer

Member
i made a little script that allows me to grab a tiles index maybe it will help you out here.
Code:
///@function return_tile_index
///@param layer id
///@param x
///@param y
var lay_id = layer_get_id(argument[0]);
var map_id = layer_tilemap_get_id(lay_id);
var mx = tilemap_get_cell_x_at_pixel(map_id,argument[1],argument[2]);
var my = tilemap_get_cell_y_at_pixel(map_id,argument[1],argument[2]);
var data = tilemap_get(map_id, mx, my);
var ind = tile_get_index(data);
//var tile_exist = tilemap_get(map_id, mx,my); use this if you want to make sure one doesnt exist there

return data;
use it just like a function
example:
Code:
if (return_tile_index(tile_layer,x,y)==tile_number) {
   //do this
}
 
I would keep the tiles as tiles, don't place objects. Using the video tutorial posted above, it's very easy to directly use tiles for collision purposes.
 
M

mpolder

Guest
I would prefer not to have a second layer for collisions but to have it automatically created. That's a tad easier since it allows for quick level editing. Maybe in the future a seperate layer would be better for performance but short-term this would be slightly easier.
 
Top