Can Defold handle hex based maps? (SOLVED)

AFAIK you should be able to create anything yourself with game components.


This is a very good read about implementing a hex grid.

3 Likes

It is! Thats the library I was using in my earlier efforts on this project, actually.

Which link was broken?

Its disappeared now. It was pinned to the top of the forum, below the current announcement, welcoming me to the forum and suggesting I read the FAQs before posting.

The link was to https://defold.com/faq/ and it was near the bottom of the post. Seeing as its vanished now, I am guessing that perhaps its specific to new users?

Hex grids are not the something related to engines. It is just a logic. You can use hex grids almost every engine.
I’m using Tiled to edit the grid for now. You can simply export and parse the json file.
It isn’t so hard to parse the json but Tiled coordinate system is different than Defold, you should consider this.

4 Likes

tiled is free and it’s really, REALLY useful for this sort of thing. I used it for my grid-based game (exporting lua tables). And, yes, it would be easy to create a hex grid (it’s basically a square grid but every other row is offset by 0.5, it’s not something crazy)

2 Likes

I’ve been experimenting with Hex grids in Defold lately as well. I am NOT strong in math, or in code, and I’ve managed to get a working prototype complete with some basic A*

2 Resources I can not recommend enough:


for basically everything related to layout and coordinates of the hex grid (I used the “cube coordinates” method detailed here, flat-top version)

and

https://www.mathopenref.com/coordpolycalc.html

used to create the actual tile sprites, as well as the custom .convexshape collider

I have each tile as a game object with the custom collision shape, for mouse-over detection. There may be a better way, but this suits me fine.

edit sorry, didn’t notice the redblob site was already linked! oh well! it’s a great resource for this in any case!

6 Likes

Hi ryanscottlandry,
I was searching for a hex grid system like you did. Can you please share this code sample with hex grid system…

I’m not sure if anyone has anything packaged into a reusable piece of code to handle hex grid. But I bet you can get far by using game objects for the individual hexagons and some of the algorithms presented in the blog post above to handle calculations.

1 Like

Hi,
I am new in Defold. I tried to develop a game using android studio (java) and I followed " Red Blob Games: Hexagonal Grids" tutorial too. There are some issue in that game (Pan and Zoom not working properly). Recently I installed Defold and trying to develop the same game. Now, I am learning tilemap and tilesource to implement this grid. I think, creating a GO for each hex will be more expensive. Anybody can help me…


This is what I developed in android studio.

1 Like

It is not expensive at all. You can use it safely. You can simply loop through an array and change the positions (x,y) of the grid gameobjects.

3 Likes

Game objects are not expensive. You should be totally fine to use game objects with a spirit for each hexagon.

2 Likes

Thank you for your reply,
Is it possible that to create multiple sprint ( hex image ) from a single image using script ? Or manually create sprite for each hex ( if its 8*8 total 64 hex image/sprite ) ?

Yes. You can easily create and position game objects with sprites using GO factories. You can also use maths to put each hex in the correct space.

2 Likes

Hi 8.josh,

You can easily create and position game objects with sprites using GO factories.

Can you share any code snippet or related article for doing that. I have only created GO by right clicking on that collection.

You can also use maths to put each hex in the correct space.

Yes, I am confident to do logic for that as of now. I am wondering how to create multiple GO or sprite using script. I could use position with single image, but here, I need multiple GO/sprite using single image.

What you are looking for is a factory component that can be used to spawn game object instances:

Docs: https://defold.com/manuals/factory/
API: https://defold.com/ref/stable/factory/
Example: https://defold.com/examples/basics/spawn/

2 Likes

Thank you very much,
Let me play with these things :slight_smile:

1 Like

And, also to mention the https://defold.com/ref/stable/collectionfactory/ which is slightly more powerful than the factory (which spawns a single game object)

1 Like

Hey Albert,

My hex implementation is pretty hacked-together mess, and was really just a test of “hey, I wonder if I could make this work…” with no game in mind, really, and I haven’t looked at it since I posted this in December :rofl:

Just as a basic overview from what I remember in case it helps, each tile is a separate pre-made Game Object with a sprite and a custom .convexshape collision shape file, and no script.

I have a “map builder” script that uses a Factory to spawn however many hexes are needed for a given map, at the proper positions/in the proper order, and assigns them a number or other “tag” in a table so they can be individually referenced later.

Once the map space is populated with basic pre-made Game Objects, I have a sort of set of “map data” table set which determines which type of tile each one should be (it’s basically like… { 1, 1, 1, 0, 2, 0, 2, 2…} with each number representing a tile type (mountains, grass, water, etc)).

The sprite of each tile is changed to the visual representation of the tile type ( 0 = grass, 1 = water, etc).

These numbers are also used any time a tile is interacted with. Ex: a Tank takes a movement penalty when passing through a forest (if tile_type == 5 then movement_points -1, or whatever).

Navigation through the map is done with a hacky A* implementation (my first attempt at it, messy but functional!). Tile positions are referenced through cube coordinates, stored as a vec3 for each tile in a big ol’ table, if I remember correctly. Neighbour tiles are determined by a preset list of what defines a “neighbour,” something along the lines of “North Eastern neighbour = my X, my Y-1, my Z+1”

That’s about all I can remember. Let me know if you have any specific questions and I can dig out the old project to see what else I can see, but I can guarantee you that you don’t want to see the actual code :grimacing:

4 Likes

Tank you ryanscottlandry,
I am learning now, playing with Factory and Collection factory. I will come back to you with questions once I learned . Anyway thank you for your detailed answer :slight_smile:

1 Like