[Solved] Check for diagonal neighbor on grid possible? Auto Tile now possible in GDevelop

So in short of it i have this condition to check if there is same object left from current object
On grid
I have same conditions for top right and bottom

But my question is would it be possible using this method to check if there is diagonal neighbor on said grid
Like in 2nd image where 1 is origin object and 2 is diagonal neighbor
Would it be somehow possible to check if there is same object or not?

What is your plan? What happens? You can check if there’s an object in spaces around a particular sprite by using the point is inside using the objects center x,y plus or minus the grid size like x-32, y-32 It all depends on how you’re choosing the reference object and what it’s for.

This is for extension
So i can finish AutoTile so now gdevelop would have proper AutoTile extension
So idk if point inside object would work for me
And only piece i miss are inner corners for central tile depending what tiles are surrounding it
And detecting that diagonal tiles neighbors is kinda essential for it

Here is link to project with extension
If i get that diagonal neighbor detection working everything else will be downhill job

I’m on phone. I can look at your project later.

Going off your example logic. If object is 1

The upper-left would be
Tile.CenterX() < object.CenterX()
Tile.CenterY() < object.CenterY()

Upper right
Tile.CenterX() > object.CenterX()
Tile.CenterY() < object.CenterY()

The lower-left would be
Tile.CenterX() < object.CenterX()
Tile.CenterY() > object.CenterY()

Lower right
Tile.CenterX() > object.CenterX()
Tile.CenterY() > object.CenterY()

This all assumes that there’s only 1 object in each direction. If there were multiple in any direction, it would pick them all.

It works just fine for none diagonal so i guess for diagonal would work just fine as well
I gonna try what you just wrote but my concern is (since that was also my 1st idea and that’s why i came here)
I am pretty sure that is checking for not upper left but like is there tile above and is there tile to the left

But my mistake not checking it 1st but coming here
I will need some time to check it and report back

1 Like

I haven’t been able to check your project. So, I’m still not clear on your process.

My previous concept will work if those are the only sprite in the scene. Another method would be to use x-grid size, y-grid size but then the other sprites would have to be perfectly aligned to a grid for it to work. If they are perfectly aligned then I would use x plus or minus the grid size to check for the other tiles.

There’s a dungeon making extension. IDK if it uses a similar concept as yours. I took a peek at it’s code and it did something similar with a grid check but I couldn’t really understand the math looking at it on my phone. I think it was only counting the number of sprites but it might still be worth a look. It used 2 nested repeats to check a 3x3 grid. I believe anyways. There was a lot of math for a tiny screen.

Did this for upper left corner value

Then did check for 10 11 12 13
No luck nothing works

I looked at your project. It appears to be working. What isn’t it doing now that you want it to do?

It is properly “auto tilling”?
But problem is with inner corners
Look here


IDK if it is proper name but i call it inner corners
And problem is that i can only set this center tile based on is there other tiles left+right+above+below from it
So if i would have 9 tiles surrounding it then it would choose same tile

And here you have how it should look for cross shaped tiles made from 5 tiles


(Keep in mind you won’t see it in my project since i am unable to determine diagonal neighbors so you will see center tile as single color one)

Where center tile is simply this one for above image
image

If i could find how to check if there is at least one diagonal neighbor tile
I could find also other 3 and so i would be able to use different center tile with inner corners
So in other words i could make AutoTile extension complete
Because right now you can clearly see that corners are missing
And that is not so visible at 1st on my tiles
Imagine what if i make borders of tiles 1 or 2 pixels away from edge of tile?

And that’s why inner corners are important to have

I’m not sure if this will help but it’s all I have. When I played around with a classic pipe puzzle game, I found this technique to find the connected tiles. I made it work in Gdevelop. This is a simplified version that doesn’t check the pipe connectors or direction.

This would be more for the updating of broken rocks. BC there’s no reason to rescan the entire scene. It also demonstrates the ray cast. Use any or none of this project. It’s all I have on this at the moment.

Colliding with the blocks causes the adjacent blocks to be changed to red with the initial blocks that are collided with changing to red.

try it:

source:

1 Like

I did look into it but i have next to none knowledge how i could implement it into extension to find diagonal neighbor

If you could show me example of finding at least 1 diagonal neighbor i could do the rest myself

But until that moment i lack knowledge to do anything with it

Every time I read your code; I think I understand it a little more.

I think this works (I added the left-top)

As long as you remove the x and y checks below. Just leave the collision condition. It doesn’t seem to affect anything. It might be less efficient. Instead of removing them you might be able to add a check for the additional rows and columns. IDK

I don’t understand which texture is which. So, I tested it with the hue action and it turns the tiles which have another tile above and to the left.

The 16 would have to be set to a grid size. That bothers me a bit. IDK. It’s a start.

Edit: it’s tough because you can’t compare instances to instances. I’ve seen projects overcome that by adding a 2nd sprite in the same location as the target sprite. You can then use the extra sprite for reference or collisions testing. The only problem is you don’t want to cause a drop in the frame rate every time the tiles refresh. That’s why I thought the targeted update might be a good idea.

Have an initial complete conversion but then when a tile is changed or deleted, do a limited scan.

If i were to use it via events that would be awesome
But since it is for extension i would need to be able to either extension by size of one tile decide size of grid or let player set in extension that size of grid which you set to 16
If there is no other way i guess setting that size of grid will be last resort
And i am thankful for your input on this that is already some knowledge that i can find useful

BTW that is not my extension guy named Floki did it i just modified it to read images easily

So let me show you what i know and maybe that will help you help me
I mean i hope i understand it correctly

1st of all how this extension works

1 - here in setTile we have all you see in the center
2 - here it checks if from each tile there is another tile vertically or horizontally
Let’s take Y vertical layout as example
If there is one tile below origin tile then that tile below will get Y1 if there is one above then that tile that is above will get Y2
But if there are both then center tile will get Y3
That compare two numbers condition simply works as checking if something is on which side
This way you can change its image depending on how much neighbors it have
And next step would be add X to the mix but for now we are going with only Y
More or less it looks like this

3 - is simply adding to said variable 1 or 2 or 3 depending on number of colliding neighbors
We have X and Y variable so each X and each Y can be either 0 1 2 or 3

Now 2nd part

1 - In doStepPerEvents we have this in the center
2 - We check what is X and Y variable of said tile since it is 3 for X and Y we can already deduce it is center tile 5 of cross shaped tiles
3 - here depending on what X and Y is there we set proper frame to said tile

Here is how on spritesheet each tile is positioned and what number it have


So if you would use tool called sprite sheet slicer Sprite Sheet Slicer by isometric8
All your work as user who want to use this extension would be to download splice sheet slicer
Prepare all tiles on sprite sheet like you see on screenshot above
And add new animation to your sprite object and simply select them all as they were created/sliced out from sprite sheet
And that is all

So you can check that image/frame 13 is actually center tile which is full color square
Where image/frame 0 is single lone tile that have no neighbors
And so go on

Now what is the problem here?
Problem is that each tile which should have inner corners will have not because there is no way to check if there is diagonal neighbors to it
Example
Using above legend you can check that numbers of tiles match their image/frame

Only for first two i marked where corners are missing so i don’t spam whole screenshot but you can now clearly see the issue

Now imagine if i could check if there is any diagonal neighbor
Then i would be able to change that 1 tile which should have inner corners to match what it actually should be

I am trying to find a way to solve this problem for over half year
I sit everyday on gdevelop discord server solving other ppl problems in hope i will somehow have stroke of genius and find a solution but so far i found nothing

So if you have any idea that would get me closer to solution i would be in your debt

And my only problem is that i can find/check if there are any tiles in this position you see on the left on image below
But i addition to it i need to be able to find is there anything diagonally from center one which you see on right image indicated by ? marks

This is a long reply but I wanted to say everything at once.

I can see why it’s difficult. Most solutions require too many resources. I attempted using ray cast but it took way too long just to scan and I didn’t even get it to work. I was also trying to use an array, so I’m not sure what caused the most delay.

It would be 1 thing to make it for yourself but it’s another to make it easy for other people. You’re willing to use something that requires a complex setup or might have a bug but others won’t.

I didn’t test point is inside. But it’s more intense than a simple x,y check. Most of the work is done when the scene is started. Afterwards, it should just be minor changes to prevent lag. You wouldn’t need to check mire than a few pixels away from the tile but then the problem is that going through instances with a for each object prevents other instances from being checked. A workaround is to use a 2nd object but again. If it was your project a separate sprite would be easy to use.

I thought about the code from the pipe game bc that goes through all of the tiles without having to check diagonally. Each tile checks the 4 tiles around it. I still think something like that would work for quick updates.

The pipe game used a similar concept. I had 1 sprite with the unique pipe configurations. I rotated a few animations to create every combination of pipes.

My technique used binary. IDK if you know anything about binary. Each pipe opening was a bit position. So, 4 bits could represent any puzzle piece. I could also tell if one pipe could feed another pipe by checking certain bits. bit 1 was right, 2 was down, 4 was left, 8 was up. When combined, it represents every possible pipe configuration.

There’s only so many ways to detect nearby sprites. The x,y , ray cast, collision and storing the data in an array as a grid. That would also require a grid size property. You would use the position/ grid size as the arrays elements or column, row in a 2 dimensional array. Then it would be easy to check column 1, row-1. But again, that requires the grid size and processing time.

IDK if anything I said helped. If I think of something I’ll post it.

1 Like

Lets say i know something about it

This is my plan B i think i could use point inside object to check for diagonal adjacent tiles

+++++++
This is edited part
I remember i had problems with point inside object so i went with creating separate objects like 4 one pixel sprite objects and simply was creating them from each tile in all 4 directions
And each of that 1 pixel sprites was giving different value to colliding tile basically what you see on image above
Now i know i could go with 1 one pixel sprite and simply change it’s opacity to give different value to colliding tile depending on that
But this is what i am talking about below instead of point inside object
Explanation just to avoid confusion
+++++++

BUT that is no solution for extension so i could only make it as example
Or am i missing something?
Could i use 2 separate sprite objects for 1 extension?
So one would be for tiles and other that small object sprite which i would use to place inside other tiles?
Then i could simply change opacity of that small object depending on which side i am spawning it from origin tile
And so depending on the opacity it would add different amount of well value to variable of tile and this way i could find anything
I was able to do that with top down left right as you see from the link above but i am pretty sure i would be able to expand it to diagonal directions
But again i lack knowledge how to do it in extension
And about performance
For now i only care for it to work
A working AutoTile extension should exist for Gdevelop
And i can worry about solving performance problem later looking for other method of Auto Tille’ing

Option with determining grid size would also be not a problem if i could simply make it in extension so that user need to define what grid size he will be using
But again i have no idea how to do that in extension

So in short of it I perfectly understand what you are explaining to me yet i have no practical knowledge how to implement it into extension

Kinda like i know how keyboard works and when i press R i do not send R i send 82 which is then translated to R
But knowing that i still would not be able to program actual keyboard since i lack the knowledge

Ok, if you want to try the point is inside then put this anywhere in the setTile function outside of the for each tile. Since inside the for each you can’t reference more than 1 tile. I used the bounding box, so you wouldn’t need to know the tile size or where the points were and then added/subtracted 5 pixels just to be safe.

Again, I used random colors for testing. I enabled 1 event at a time.
red up-left, purple up-right, yellow down-left, cyan down-right (orange didn’t show well)
image

Edit: it seems to work and I didn’t notice any lag.

Cool idea, if some day it get into the extension store I will install it.

2 Likes

IDK what i’m doing wrong

I did try to enable them one at a time but none of them change color of tiles

I changed it so now you need to press Z to activate AutoTile and disabled it doing it’s thing at beginning of the scene

I copied my events to your newer project and it worked. It turns out that for your colors string, you have colons between the RGB numbers insted of semicolors. I changed them to semicolons and it seems to work fine. I usually use the color selecter to the right of the box.

image

2 Likes

1 - I once wasted half day with changing tint and recolorizer extension to change coins colors instead of making each coin animation separate color
Shoot some UFO’s

I ended up just recolorizing each coin animation in GIMP and importing it as separate animation

So i was aware abot semicolons instead of colons i simply forgot about it i even did not look carefully at your screenshot so 100% my fault

2 - I told you i only need to find one adjacent neighbor gd.games game build
Press Z to activate AutoTile
Q/E or mouse scroll to zoom in out
Any mouse button to drag camera
As soon as you move with WASD/arrows camera snaps to player
I wasted whole day to do it
If you care to check how it works here it is (everything with purple comments is stuff i added with your solution to AutoTile extension) Data package from May 3rd. - FileTransfer.io

3 - Originally this extension was made by Floki i then simplified it to read all tiles from singe animation imported as frames in exact order any spritesheet slicer would export them into separate images
(Floki went with making most tiles separate animation which if you had more tiles was time consuming to even add)
Floki also kinda abandon this extension not willing to go for that inner corners
Or more like moved to making it into work in conjunction with external map editor and/or something like Tiled or LDtl
So i gonna ask him will he publish it as his own extension or i will need to do it
No matter what will be his decision in both cases i would wish to list you as this extension creator since it is working only because your solution but i can’t make that decision for you
So i am asking do you want to be listed as creator of AutoTile or you don’t care?

4 - BIG THX i sit on gdevelop discord every day if you need anything and well if i can help feel free to @ZeroX4 with whatever you need i am really in your debt
Thx to you i can finally finish my MiniCraft clone

But 1st
Time to reduce lag or at least find a way to only update adjacent tiles when created/destroyed
Wish me luck

1 Like