• 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!

GML Tileset Hight-Table for x-rows...

E

e1ketzu

Guest
Hi I am trying to build a hight table from an tileset wich is bigger than 1 row.
But whenever I try to set a second for loop in the draw event the game freezes.

I know my way is not the fastest without using buffers but it is just needed once at start.

My code so far:

Create Event:
Code:
/// @description Game Setup
//Collision tiles
//Add all tiles to the screen
#macro TILE_SIZE 64

var k = 1; //Variable for Tile_data

tiles_x = sprite_get_width(sCol) / TILE_SIZE;
tiles_y = sprite_get_height(sCol) / TILE_SIZE;

heightstoget = tiles_x * TILE_SIZE;
rowstoget = tiles_y * TILE_SIZE;


//Make Tile layer
var layerid = layer_create(0,"Tiles");
tilemapid = layer_tilemap_create(layerid,0,0,tCollision,tiles_x,tiles_y);

//Create Tiles
var data
for (var j = 0; j <= tiles_y; j++)
{
    for (var i = 0; i <= tiles_x; i++)
    {
        tilemap_set(tilemapid, k, i, j);
        show_debug_message("Tile " + string(i) + " set");
        k++;
    }
}
Draw Event:
Here now is just the loop for the first row which is working perfect.
Code:
/// @description Build height table then start game
{
    draw_tilemap(tilemapid,0,0);
   
        for (var i = heightstoget-1; i >= 0; i--)
        {
            var check = 0;
            while (check <= (TILE_SIZE))
            {
                global.heights[i] = check;
                if (check == TILE_SIZE) break;
                if (surface_getpixel(application_surface,i,check) != c_black) break;
               
                check++;
            }
        }
}

room_goto_next();
How in the world I get it to work with the following colums?

Thanks for any help.
 
Last edited by a moderator:
S

seorin

Guest
I'm using the same tutorial as you, but I haven't gotten around to updating my tileset to multiple rows yet (working on getting some other additions finished up first). Since I plan on doing it soon enough anyway, I just updated my code for multiple rows and it seems to be working fine on my single row. It should theoretically keep working even if I give it a sprite with multiple rows, but I technically haven't tested it, so I can't say for 100% sure. Here's how I did it:

Create:
Code:
width_pixels = sprite_get_width(sCollisionTileset);
var tiles_wide = width_pixels / TILE_SIZE;
tiles_high = sprite_get_height(sCollisionTileset) / TILE_SIZE;

//Create Tiles
var tile_layer = layer_create(0, "Tiles");
collision_tilemap = layer_tilemap_create(tile_layer, 0, 0, tsCollisionSlopes, tiles_wide, tiles_high);

var tile_index = 0;
for(var i = 0; i < tiles_high; i++)
{
    for(var j = 0; j < tiles_wide; j++)
    {
        tilemap_set(collision_tilemap, tile_index++, j, i);
    }
}
Draw:
Code:
//continue tileset setup
draw_tilemap(collision_tilemap, 0, 0);
for(var row = tiles_high-1; row >= 0; row--)
{
    var row_offset = row * TILE_SIZE;
    for (var i = width_pixels - 1; i >= 0; i--)
    {
        var n = 0;
        while (n < TILE_SIZE)
        {
            //if (n == TILE_SIZE) { break; }
            if (surface_getpixel(application_surface, i, n) != c_black) { break; }
            n++;
        }
  
        global.heights[row_offset+i] = n;
  
    }
}

room_goto_next();
I commented out the first break in the draw event because it shouldn't be needed as long as you change the <= in the while to <, but there could be something I'm overlooking. It shouldn't hurt to leave it in either.
 
E

e1ketzu

Guest
I did try but it did not work:

When steping on ground which is noch in the first row:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object oPlayer:

Push :: Execution Error - Variable Index [0,1713] out of range [1,1024] - -5.heights(100002,1713)
 at gml_Script_InFloor (line 10) -        var thefloor = global.heights[(argument1 mod TILE_SIZE) + pos*TILE_SIZE];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_InFloor (line 10)
called from - gml_Object_oPlayer_Step_0 (line 102) - var floordist = InFloor(tilemap,x,bbox_bottom+vsp)
 
S

seorin

Guest
Just a minor error in my thinking. I chopped my current tileset into two rows and got it working.
Change the Draw event to:
Code:
//continue tileset setup
draw_tilemap(collision_tilemap, 0, 0);
for(var row = tiles_high - 1; row >= 0; row--)
{
    var y_offset = row * TILE_SIZE;
    var x_offset = row * width_pixels;
    for (var i = width_pixels - 1; i >= 0; i--)
    {
        var n = 0;
        while (n < TILE_SIZE)
        {
            //if (n == TILE_SIZE) { break; }
            if (surface_getpixel(application_surface, i, y_offset + n) != c_black) { break; }
            n++;
        }
   
        global.heights[x_offset+i] = n;
   
    }
}

room_goto_next();
 
E

e1ketzu

Guest
Yeah right - I see the offset was missing - okay - I tried it and now its working. thanks !
 
Top