GM:S 1.4 [SOLVED] How to find which tile at a position

Discussion in 'Programming' started by Steneub, Jun 4, 2019.

  1. Steneub

    Steneub Member

    Joined:
    Jun 3, 2019
    Posts:
    10
    I would like to know what tile is at a given position.
    Code:
    if (mouse_check_button_released(mb_left)) {    
        //d is defined as depth of tiles
        var tile = tile_layer_find(d, mouse_x, mouse_y);    
        if (tile != -1) {        
            show_debug_message(string( tile_get_background(tile) ));
        }
    }
    This is helpful in finding the tile and which Background, but it doesn't give me enough information about the tile itself. I want to know which tile is being indexed here. If I have two tiles next to eachother which use, say, the grass tile, the tile value is unique between them.

    When I click two adjacent positions that use the same tile from the same background asset, these values appear in my console:
    Code:
    10000000
    10000001
    Which I've discovered these values are what are generated sequentially in the order in which they were placed (starting with 10000000 apparently).

    If I could even get the x and y offset into the background asset, that would still be helpful enough to do some simple math to determine which tile is at the position.

    Any help would be appreciated.
     
  2. Steneub

    Steneub Member

    Joined:
    Jun 3, 2019
    Posts:
    10
    I'm continuing to dig, check this out
    Code:
    <tile bgName="tileset2" x="24" y="24" w="8" h="8" xo="0" yo="16" id="10000029" name="inst_D5885631" depth="1000000" locked="0" colour="4294967295" scaleX="1" scaleY="1"/>
    This is pulled from the room file in a text editor.
    • x,y are for roomspace;
    • w,h are for the size of the tile; and
    • xo,yo are the offset where that tile is pulled from in the background asset source
    If I could extract this xo and yo from the tile id, I might be well on my way to solving this problem
     
  3. Steneub

    Steneub Member

    Joined:
    Jun 3, 2019
    Posts:
    10
    After poring through the documentation again (for the nth time), I found what I was looking for! The not so intuitively named get_tile_left and get_tile_top

    Together, these functions will return the offsets within the Background asset for the given tile. Here is my updated script to show how I'm doing this
    Code:
    if (mouse_check_button_released(mb_left)) {   
        var tile = tile_layer_find(d, mouse_x, mouse_y);   
        if (tile != -1) {       
           
            var left = tile_get_left(tile) / TILE_SIZE;
            var top = tile_get_top(tile) / TILE_SIZE;      
           
            show_debug_message("("+ string(left) + ", " +string(top) +")"); 
        }
    }
    I was able to figure out my problem and I'm trying to be responsible by showing the solution instead of fading into obscurity

    [​IMG]
     
    Japster, woods and rIKmAN like this.
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,530
    Thanks - it's a real pet peeve of mine.

    Congrats on solving it yourself too.
     
    Steneub likes this.
  5. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    awesome.. ive been struggling with similar problem... i am glad to posted your issue AND your fix ;o)
     
    Steneub likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice