0 votes


I'm making a city builder game, and I'm trying to figure the best way to implement roads.
What's the easiest way to implement them to connect when adjacent? That is, they change graphics depending on other roads 1 tile up/down/left/right. My idea is to flip/transpose them in order to not make 2 or 4 images of the same road in different directions.
Remember that not only the placed tile needs to be set to it's correct sprite and position, but the adjacent ones also need to be changed.
I'm leaving this for a latter status of the game, but meanwhile, I'll leave the question: Is this possible to do with Tilemaps? Is it any easier with overlaid sprites?


in Engine by (14 points)

It's a bit ad, but you may check my GitHub, cause I'm doing city-builider too ;)
Hope, that 'll be usefull. If you'll have any question about my code, ask me here, on Github or Tumblr.
PS: #pastastyleallert - that's no the best solution...

Ok, nice. It looks promising, although with some glitches in the UI (namely, the initial city placement is activated while scrolling with the right mouse button, and the add new houses/road window randomly places objects on the map).
I also uploaded my first revision of my project, https://github.com/nicfer/godotcity , which is even more crude, but it's something.

Thanks for feedback. About your GH, I have 404 error, you added point to url :) About that random placement, can't confirm this (have you last commit?), btw, I suggest move discusion about my project to GH/issues, casue that's no about your question. Thanks.

Ok, I'll continue the chat about your project there. And I fixed the URL, it was using the comma in front as part of it, now it should work.

3 Answers

+1 vote

You can easily script this if you know the list of tiles that make up roads. For a given road tile, look at the 4 neighbors. If they are roads or not, you have different cases. This way you have 4x4 cases: one of them being no road, it's 15 cases in which you choose which tile to put and how to flip/transpose it.

That's the easiest way, but if you like binary there is a clever way using bit masks of 4 bits, where each bit is a direction where the road tile can go. You then obtain a number between 0 and 15, and you map it to a tile ID and flip/transpose.

This question comes up a lot in 2D game dev, I'm sure you can find more examples and variations on the web, it's not specific to Godot ;)

by (27,831 points)
0 votes

Even if Tilemap offer a structure capable of doing that, I suggest to create your own matrix with values to handle logic (could be a class/dictionary with cell data), it will be easier to manage, then just use the canvasitems to draw your data on screen mapping the data to tiles and other objects (tilemaps, sprites or a big changing texture).

You can look around for general approaches for this kind of games and see which one suits better your design.

by (7,860 points)
0 votes

Use these bitmask for your Tilemap:

Road bitmask

by (14 points)

How to use this?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.