Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | YangTegap |
I am building a minesweeper clone. The cells are instantiations of a custom “Tile” scene, they are laid out on a GridContainer.
Upon selection of a cell, I call a function to check the neighbor cells to see if they’re a mine cell. If not, then I mark the current cell as clear and call that same function on all the neighboring cells
Everything works perfectly until I increase the grid size. I am attempting to play with a 30 X 30 grid, and when I click on a blank cell, the recursion needed to check all the cells for mines throws a Stack Overflow (Stack Size: 1024)
error. How would I fix this?
Select function on the Tile scene:
func select():
if not flagged and not selected:
selected = true
if mine:
Game.lose()
else:
var mines = check_neighbors() # Returns number of mines, int 0-8
match mines:
0:
set_texture(clear_blocked)
select_neighbors() # Runs select() on all neighbor cells
1: set_texture(number_block_1)
2: set_texture(number_block_2)
3: set_texture(number_block_3)
4: set_texture(number_block_4)
5: set_texture(number_block_5)
6: set_texture(number_block_6)
7: set_texture(number_block_7)
8: set_texture(number_block_8)
This issue is occurring is because you are creating to many items so your passing the limit.
You have two options, change the limit of nodes or use less nodes to do the same work.
The first option can be done, by looking thorugh the settings and finding where hte node limit is. The second option is a more efficient way of drawing the tiles. Use the tilemap. Map a 2d array or a Vector2 Dictionary to a tilemap so that it draws the tiles you want in a way that doesn’t hit the node limit. You can also just draw to the canvas directly was well.
codelyok13 | 2022-03-22 21:48
Here is an example of what I am talking about.
https://github.com/codelyok13/Rosetta-Code/blob/master/Garbahe%20Minesweeper.7z
https://tmpfiles.org/244040/garbaheminesweeper.7z
Two Hours from now to check.
codelyok13 | 2022-03-23 04:02
Thank you! This makes plenty of sense! I appreciate your time and input!
YangTegap | 2022-03-23 04:12
I recommend reading Inces answer because most likely he is correct as usually using to many slows you down not cause crashes. It was probably due to a rouge recursive function making to many calls.
codelyok13 | 2022-03-23 13:43