How do I display what tile ID I'm currently clicking on?

0 favourites
  • 6 posts
From the Asset Store
Piano tiles
$9.99 USD
Template for a piano tiles game, fully documented in comments and video
  • So I'm making a sort of sandbox crafting game along the lines of Terraria and Starbound. I have a tilemap that has the blocks that I'm making the world out of. I currently have grass and dirt. What I would like to happen in the game, is when I click on grass, it adds a value to a global variable: "grass," and when I click on dirt, it adds a value to a global variable: "dirt." And when I place grass, it subtracts 1 from the "grass" variable, and same with dirt. The problem is, no matter what tile I click, it adds 1 to the variables I currently have set up, which are grass and dirt. I'm also confused because even if I click on nothing, as long as it's within the tilemap's borders, it'll still add 1 to the grass and dirt variables. In the current game, I have in the event sheet, when the tilemap itself is clicked on, it adds to the grass variable. I know that's causing the problem; I just don't know how to add another condition that triggers the event when only grass is clicked on, or only dirt is clicked on.

    Basically, to sum it up, I'm looking for a way to have the game add to only the grass variable when I click on grass. Same with other blocks and their variables once I add them. I'm totally new to Construct 2, so I might not understand anything complex

  • Use the expression

    [Tilemap].TileAt(PositionToTileX(Mouse.X),PositionToTileY(Mouse.Y))[/code:1snuait9]
    when clicked, to retrieve the Tile ID at the current mouse position.
    
    You can then compare the Tile ID that was returned by the expression with the Tile ID of, e.g., grass and add to grass if the comparison is true.
    
    If you need further information/help, give me a shout.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Using a globals as requested.

    https://www.dropbox.com/s/07fus6w961l81 ... .capx?dl=0

    But, as you see, you need an event for each global. The event sheet will get way to long. And at one point you get lost in all those events. Why not use a dictionary ?

    https://www.dropbox.com/s/zfbkqdzuvoj5q ... .capx?dl=0

  • I used globals, and it worked very nicely! Thanks to the both of you. Just as a little extra question: I set up a way to build with certain blocks, grass and dirt. Every time I place grass, it subtracts 1 from the grass variable, and same with the dirt block and it's variable. The problem I'm having is that I can spam a block on the same tile, and the variable keeps subtracting. Is there any way to make sure that if I'm placing a block, I can't place it on another one?

    Here's a link to my video: youtube.com/watch ... e=youtu.be

  • Can't view your video since the URL has been shortened.

    But I'm pretty sure, that this would solve your problem:

    Check whether (when clicked), this expression

    [Tilemap].TileAt(PositionToTileX(Mouse.X),PositionToTileY(Mouse.Y))[/code:164jbhc9]
    returns [i]-1[/i].
    If it does, that means that the currently clicked tile is empty, meaning that there is no tile at the moment.
    If the expression returns anything else [i]but[/i] -1, there is a tile already.
  • Thanks! I appreciate it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)