Can Defold handle hex based maps? (SOLVED)

#1

G’day, Im looking at making a computer version of a board game as an inexperienced C# programmer. Its probably too big a task for me, game engine or no - twelve months ago I started working on making business logic in C# and learned a lot about the language I didnt know, but didnt actually get it working.
Im looking at Defold and Id like to take another crack at this project using Defold, but Im curious about whether Defold can handle the hex based map Ill need to use for this game. I was using a library to handle the hexes when I last had a go at this project, but Im not sure that would work with Defold.

I did have a look at the FAQs mentioned above to see if there was anything in them about grids, but unfortunately the link is broken!

0 Likes

#2

There is no built in hex grid support but you can create it yourself using the standard components. @selimanac and @Dragosha are doing this: https://twitter.com/selimanac/status/1198565538676523011?s=19

1 Like

#3

Oh, I’ll fix this!

0 Likes

#4

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

#5

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

0 Likes

#6

Which link was broken?

0 Likes

#7

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?

0 Likes

#8

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.

3 Likes

#9

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

#10

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!

5 Likes

#11

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

0 Likes

#12

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

#13

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

#14

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

#15

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

2 Likes

#16

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 ) ?

0 Likes

#17

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

#18

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.

0 Likes

#19

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

#20

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

1 Like