How do I pick a tilemap tile nearest to a sprite?

0 favourites
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • i have a tile map that will only let you click on an adjacent block. once clicked, the surrounding tiles can then be clicked

    (red yellow green. red is no, yellow can be clicked, green has been clicked)

    i need this to run without clicking, but on a playerbase arriving.

    ill need the playerbase to be able to pick a path to the corresponding nearest tile.

    i could: spawn a sprite at each tile, once it changes tile type. delete it then i could just pathfind to closest sprite.

    but.. it will be a big map, potentially hundreds of these sprites. is there a more efficient way?

    Tagged:

  • Im not entirely sure what you are trying to do but I will do my best to help.

    PositionToTileX(X) Converts an X coordinate in the layout to the nearest X coordinate on the tilemap. (same with PositionToTileY(Y) just with the Y coordinate)

    With Pathfinding you can also just pathfind to a position rather than a sprite. To get this position you can use TileToPosition(x). (again same with TileToPosition(Y) just with the Y coordinate)

    Can you elaborate a bit on what you are trying to achieve?

  • ok, so, i have a digging game. like dungeon keeper.

    imps break 3d blocks. and i want them to then go claim the ground once the block has been destroyed. (they can already dig)

    i used a tile map. with all the different floors and room floors. Right now, there are 3 floor types:

    ground, ground that can be claimed, and claimed floor.

    i have made it so only ground next to a claimed floor can be claimed, once claimed, the surrounding "ground" tiles become "ground that can be claimed" tiles (simple code. just on click change tile at x,y+1 etc)

    as any pattern can be cut from the rock, making the tilemap reactive. i will never specifically know which tile where is "ground ready to be claimed". (or.. maybe i can?..there isnt an option for "for each" tile. only "for each" tilemap)

    i half thought about running a loop to check each tile. but, that wont get me the nearest.

    i suppose... the imp will always be on a certain tile... so i could reference its x,y to the tilemap... claimable tiles will always be reachable... meh, again i cant pinpoint tiles that have a certain tile.... unless... i have a second tilemap that logs imps location, and have a flood function to check area around the imp... but, i would need a tileset per imp so checks dont overlap.... i .. could maybe just set it as the imps child...but at that point i have spawned in 30 tilemaps... maybe i would be better just spawning in a sprite on "ground that can be claimed" and have the imp go to the nearest sprite

    im running with the spawn in a sprite on "claimable tile" atm. then imps go to the sprite, start work and hoping the tile at imps location changes instead of using an on click event.

    hope this elaboration helps lol.. hope you can see my train of thought on ideas, and any input or suggestions you have would be appreciated!

  • Does the ground claiming system you describe work like this?

    Green is claimed, blue is available to claim, and red is unclaimable/unreachable.

    Spots become available if they are adjacent to claimed ground.

    If this is the case, then I think this could be a possible solution:

    Keep a list of the X/Y positions of claimed ground. (so you don't have to check the entire map)

    Also keep a list of the X/Y positions of ground that is ready to claim.

    Upon updating your map, run the list of claimed ground points through a function that takes them as parameters.

    This function will check the neighboring tiles to the claimed ground. (x-1, x+1, y-1, y+1)

    If the tile being checked is already claimed then do nothing, but if its unclaimed, change it to be available.

    Store the X/Y of the available tile in the second list and use that for referencing where the tiles are. (you can also use this to pick the closest point to the Imps)

    Lastly go through both lists and check if any points are in both of them. If there is a match, then remove the point from the available to claim list. (so that ground that is already claimed isn't mistaken for available to claim ground)

    (you could also probably do some optimization to remove points from the list that have all 4 neighbors already claimed so you aren't checking them multiple times)

  • I agree on the list approach. Keeping a list of x,y positions of tiles that are available to claim makes sense to me.

    To find the nearest tile without resorting to sprites, you can use the distance expression. You'll have to go through the whole list though to find the nearest but I think "pick nearest sprite" doesn't really do anything different to that. So you would go through the whole list, store the distances of each and then use the one with the smallest distance.

    But realistically, there is nothing really wrong with just using invisible helper sprites either... It's a functional solution with little to think about and probably already optimized to run as fast as possible. Many roads lead to rome.

  • WackyToaster

    youtu.be/Q4hOhwigPjc

    got it to work using spites! sorry its a video on my phone. for the main game, everything is scaled up, so pathfinding is easier

    yes... digging ming and map done.

    building should be easy now

  • BaconSwagg

    wow wow wow. yup exactly what i was doing, and a great solution!

    if i sense the sprites are causing lag, im defiantly doing as you suggest!

    didnt see your post.. just wackytoasters.. or i wouldnt have spent half a day using sprites...

    at least its working right now. see how well it gets implemented into main game

  • It's nice to see you got it working! Good luck on making your game! :)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • cool project.

    I was working on something similar a few years ago. I'm not sure i still have the file to share, but this thread my give some indications how i approached some of the same problems.

    https://www.construct.net/en/forum/construct-3/your-construct-creations-9/fungeon-keeper-139024

  • mOOnpunk

    dude, hell yea i watched your clips! honestly, they were a huge inspiration going into this project.

    my sprites still have variables from a kenshi style game i was working on, but i shifted to dungeon keeper after seeing your posts

    maybe i can find a use for detaching limbs in dungeon keeper...

    was thinking on going down a pokemon route, where creatures can evolve

    either way, i see you are still active! and will reference your work if i get a problem!

    my major concern going forward is checking room size. i saw a fill example. that basically filled a set area walled off, within a tilemap.

    did you ever work with tilemaps, and get into doing a "size/area" as dungeon keeper did, to work out room efficiency. what creatures share a bedroom.

    understood it was a few years ago.

  • Honestly cool to see someone else having a go at it. The Dungeon keeper mechanic is so great and lends itself to lots of interesting ideas and possibilities.

    did you ever work with tilemaps, and get into doing a "size/area" as dungeon keeper did, to work out room efficiency. what creatures share a bedroom.

    I don't think i ever got as far as proper room construction unfortunately.

    I think i used multiple tilemaps over the top of each other to show different graphics, then arrays to hold info on each tile coordinate, like if it was a floor, or wall, wall/rock type with each type having its own health remaining so it takes a different amount of time to dig, number of imps digging at a specific tile etc.

    I coded my own floodfill, but theres this https://www.construct.net/en/make-games/addons/252/tilemap-flood-fill extension. Not sure what features it has.

  • yo mOOnpunk

    you did picking up imps and placing them.

    1, you had awesome drop animation how did you achieve it?

    2, did you ever have difforent things to pick up? like gold?

    3, did you ever deal with overlapping objects that react to being clicked?

    nearly nailed the pick up system. but, im stuck with picking up gold and an imp at the same time... at least i organized it so i can use the same playerbase for monsters and imps if needed.

  • I can't honestly remember how i did it, it was like 5 yeas ago.

    I assume i used the "pick top/bottom", or "pick nearest".

    I only picked up imps, but you can use families.

    Pick up and dropping animations was probably part of the finite state machine, when the state of an imp was picked up do pickup stuff.

  • ':D yeah, thought i may be shooting in the dark on a 5 year ago project.

    with lionz help i found out about families. been 6 years since i used that.

    but using families and a GV uid check i can pick up individual objects.

    1 more question, sorry bud.

    imps are digging tiles 1 behind the front line. they wont unless its the only option.

    it only happens occasionally. but, its a bug, maybe you faced?

    im using, pick nearest, find path. but despite it being an unreachable route, they do it anyway.

    are you still making c3 games?

  • Can you show a video of it happening or a picture of your code you are using to pick where they go?

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