Godot GridMap Not Outputting with no error message

I’m Japanese.
I am learning English now.

Godot 3.5

Godot GridMap Not Outputting with no error message

Im thinking of developing a roguelike, Mystery Dungeon-like, automatically generated maze game using the game engine Godot 3.5

The automatically generated maze uses a digging method instead of a maze with rooms like Rogue. Reference website for automatic maze generation using hole digging method Reference website 1. Reference website 2 for automatic maze generation using the digging method Reference website 2.

Source code of self-made maze automatic generation program for Godot (GDScript).

extends Spatial

var maze_width = 10

var maze_height = 10

var _maze_width = maze_width * 2 + 1

var _maze_height = maze_height * 2 +1

const grid_wall = 0

const array_out_of_range = -1 #outside the maze

const array_passage = 0 #aisle
const array_wall = 1 #wall

class Array2D:

	var _width = 0

	var _height = 0

	var _pool = []

	func _init(w:int,h:int) -> void:

		_width = w

		_height = h

		for j in range(h):

			for i in range(w):

				_pool.append(0)

	func fill(v:int) -> void:

		for j in range(_height):

			for i in range(_width):

				_pool[i + j * _width] = v

	func setv(i:int,j:int,v:int) -> void:

		if i < 0 + 1 or i >= _width - 1:

			return #outside the maze

		if j < 0 + 1 or j >= _height - 1:

			return #outside the maze

		_pool[i + j * _width] = v

	func getv(i:int,j:int) -> int:

		if i < 0 + 1 or i >= _width - 1:

			return array_out_of_range #outside the maze

		if j < 0 + 1 or j >= _height - 1:

			return array_out_of_range #outside the maze

		return _pool[i +j * _width]

	func dump() -> void:

		print("[array2D]")

		for j in range(_height):

			var s = ""

			for i in range(_width):

				s += "%d, "%getv(i,j)

			print(s)
			
			
			
var _array = Array2D.new(_maze_width,_maze_height)

onready var gridmap = $GridMap 

# Declare member variables here. Examples:

# var a = 2

# var b = "text"


# Called when the node enters the scene tree for the first time.

func _ready() -> void:

	#pass # Replace with function body.

	_regenerate()

	_redraw()
	

func _regenerate() -> void:

	_array.fill(array_wall)
	
	var xstart = 1

	var ystart = 1

	while xstart % 2 == 1:

		xstart = randi()%_maze_width

	while ystart % 2 == 1:

		ystart = randi()%_maze_height
		
	
	_dig(xstart,ystart)
	
	_array.dump()
	
func _dig(x:int, y:int) -> void:

	_array.setv(x,y,array_passage)
	
	var dir_list = [

		Vector2(-1, 0),

		Vector2(0, -1),

		Vector2(1,0),

		Vector2(0,1)

	]

	dir_list.shuffle()

	for dir in dir_list:

		var dx = dir.x

		var dy = dir.y
		
		if _array.getv(x + dx * 2, y + dy * 2) == array_wall:
			
         #2 space ahead is a wall so you can dig
			_array.setv(x + dx, y + dy, 0)
			
     #Dig the next hole with recursive call
			_dig(x + dx * 2, y + dy * 2)
			
#GridMapに反映する

func _redraw() -> void:

	gridmap.clear()

	for j in range(_maze_height):

		for i in range(_maze_width):

			if _array.getv(i,j) == array_wall:

				gridmap.set_cell_item(i, 1, j, 0)
	
# Called every frame. 'delta' is the elapsed time since the previous frame.

#func _process(delta):

#	pass

Although this source code does not result in a syntax error,
the maze will not be automatically generated.

In the current source code (Godot 3.X) the maze is not actually reflected in the GridMap node.

Are there any descriptions that I am missing? Why won’t any errors show? And why is the node not being generated?

I am at a loss.

Thanks.

Reference website 3

I modify it.however it does not work.
I don’t know way Cube put GridMap.

extends Spatial

#tool class_name AutoGridMap extends GridMap

#class_name AutoGridMap extends GridMap

#export( bool ) var refresh setget set_refresh
export( Resource ) var mesh_library_3d = mesh_library_3d as MeshLibrary


var maze_width = 10
var maze_height = 10

var _maze_width = maze_width * 2 + 1
var _maze_height = maze_height * 2 +1

const grid_wall = 0

const array_out_of_range = -1 #迷路外
const array_passage = 0 #通路
const array_wall = 1 #壁

class Array2D:
	var _width = 0
	var _height = 0
	var _pool = []
	func _init(w:int,h:int) -> void:
		_width = w
		_height = h
		for j in range(h):
			for i in range(w):
				_pool.append(0)
	func fill(v:int) -> void:
		for j in range(_height):
			for i in range(_width):
				_pool[i + j * _width] = v
	func setv(i:int,j:int,v:int) -> void:
		if i < 0 + 1 or i >= _width - 1:
			return #迷路外
		if j < 0 + 1 or j >= _height - 1:
			return #迷路外
		_pool[i + j * _width] = v
	func getv(i:int,j:int) -> int:
		if i < 0 + 1 or i >= _width - 1:
			return array_out_of_range #迷路外
		if j < 0 + 1 or j >= _height - 1:
			return array_out_of_range #迷路外
		return _pool[i +j * _width]
	func dump() -> void:
		print("[array2D]")
		for j in range(_height):
			var s = ""
			for i in range(_width):
				s += "%d, "%getv(i,j)
			print(s)
			
			
			
var _array = Array2D.new(_maze_width,_maze_height)
#onready var gridmap = $"GridMap"
#onready var gridmap = get_node_or_null( "GridMap" )
onready var gridmap = get_node("GridMap")

# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	#pass # Replace with function body.
	_regenerate()
	_redraw()
	
func _regenerate() -> void:
	_array.fill(array_wall)
	
	var xstart = 1
	var ystart = 1
	while xstart % 2 == 1:
		xstart = randi()%_maze_width
	while ystart % 2 == 1:
		ystart = randi()%_maze_height
		
	
	_dig(xstart,ystart)
	
	_array.dump()
	
func _dig(x:int, y:int) -> void:
	_array.setv(x,y,array_passage)
	
	var dir_list = [
		Vector2(-1, 0),
		Vector2(0, -1),
		Vector2(1,0),
		Vector2(0,1)
	]

	dir_list.shuffle()

	for dir in dir_list:
		var dx = dir.x
		var dy = dir.y
		
		if _array.getv(x + dx * 2, y + dy * 2) == array_wall:
			
			#2マス先が壁なので掘れる
			_array.setv(x + dx, y + dy, 0)
			
			#再帰呼び出しで次の穴を掘る
			_dig(x + dx * 2, y + dy * 2)
			
#GridMapに反映する
func _redraw() -> void:
	#gridmap = GridMap.new()
	add_child(gridmap)
	#gridmap.name = "Maze"
	gridmap.cell_size = Vector3(1,1,1)
	gridmap.mesh_library = mesh_library_3d
	gridmap.set_owner(get_tree().edited_scene_root)
	#mesh_library_3d.find_item_by_name( "Cube" )
	#gridmap.clear()
	gridmap.set_clip(true)
	for j in range(_maze_height):
		for i in range(_maze_width):
			if _array.getv(i,j) == array_wall:
				gridmap.set_cell_item(i, 1, j, 0)
	print("set_cell_item is end.")
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass

Setv ang getv will not be able to get index 0 for I or j.

This could effect _regenerate if rand()% is zero.

Your dig has infinite recursion since every block is filled as a wall. This could be deleting all your blocks in a single frame.

Gridmap is already a child.

Should the y coordinate be zero, instead of 1?

Anyway my best suggestion would to just use the debugger in the editor, very powerful tool to catch logic issues.

No need to worry about that.

1 Like

Please tell me how to control GridMap with GDScript.

It seems I worded the question incorrectly.
I would like to ask a new question.

extends Spatial

#tool class_name AutoGridMap extends GridMap

#class_name AutoGridMap extends GridMap

#export( bool ) var refresh setget set_refresh
export( Resource ) var mesh_library_3d = mesh_library_3d as MeshLibrary


var maze_width = 10
var maze_height = 10

var _maze_width = maze_width * 2 + 1
var _maze_height = maze_height * 2 +1

const grid_wall = 0

const array_out_of_range = -1 #迷路外
const array_passage = 0 #通路
const array_wall = 1 #壁

class Array2D:
	var _width = 0
	var _height = 0
	var _pool = []
	func _init(w:int,h:int) -> void:
		_width = w
		_height = h
		for j in range(h):
			for i in range(w):
				_pool.append(0)
	func fill(v:int) -> void:
		for j in range(_height):
			for i in range(_width):
				_pool[i + j * _width] = v
	func setv(i:int,j:int,v:int) -> void:
		if i < 0 + 1 or i >= _width - 1:
			return #迷路外
		if j < 0 + 1 or j >= _height - 1:
			return #迷路外
		_pool[i + j * _width] = v
	func getv(i:int,j:int) -> int:
		if i < 0 + 1 or i >= _width - 1:
			return array_out_of_range #迷路外
		if j < 0 + 1 or j >= _height - 1:
			return array_out_of_range #迷路外
		return _pool[i +j * _width]
	func dump() -> void:
		print("[array2D]")
		for j in range(_height):
			var s = ""
			for i in range(_width):
				s += "%d, "%getv(i,j)
			print(s)
			
			
			
var _array = Array2D.new(_maze_width,_maze_height)
#onready var gridmap = $"GridMap"
#onready var gridmap = get_node_or_null( "GridMap" )
onready var gridmap = get_node("GridMap")

# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	#pass # Replace with function body.
	_regenerate()
	_redraw()
	
func _regenerate() -> void:
	_array.fill(array_wall)
	
	var xstart = 1
	var ystart = 1
	while xstart % 2 == 1 or xstart == 0:
		xstart = randi()%_maze_width
	while ystart % 2 == 1 or ystart == 0:
		ystart = randi()%_maze_height
		
	
	_dig(xstart,ystart)
	
	_array.dump()
	
func _dig(x:int, y:int) -> void:
	_array.setv(x,y,array_passage)
	
	var dir_list = [
		Vector2(-1, 0),
		Vector2(0, -1),
		Vector2(1,0),
		Vector2(0,1)
	]

	dir_list.shuffle()

	for dir in dir_list:
		var dx = dir.x
		var dy = dir.y
		
		if _array.getv(x + dx * 2, y + dy * 2) == array_wall:
			
			#2マス先が壁なので掘れる
			_array.setv(x + dx, y + dy, 0)
			
			#再帰呼び出しで次の穴を掘る
			_dig(x + dx * 2, y + dy * 2)
			
#GridMapに反映する
func _redraw() -> void:
	#gridmap = GridMap.new()
	#add_child(gridmap)
	#gridmap.name = "Maze"
	gridmap.cell_size = Vector3(1,1,1)
	gridmap.mesh_library = mesh_library_3d
	gridmap.set_owner(get_tree().edited_scene_root)
	#mesh_library_3d.find_item_by_name( "Cube" )
	#gridmap.clear()
	gridmap.set_clip(true)
	for j in range(_maze_height):
		for i in range(_maze_width):
			if _array.getv(i,j) == array_wall:
				gridmap.set_cell_item(i, 0, j, 0)
	print("set_cell_item is end.")
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass