Question
I am currently trying to make a drunkard's walk algorithm in godot 4.6. Currently, I have an error on line 11 that says "Not all code paths return a value.
I am following a tutorial and I have copied the code almost word for word (at least from what I can tell) Here is the tutorial: Drunkard’s Walk and Natural Cave Generation | How to Make Procedural Dungeons in GODOT
This is the code:
class_name DrunkGuy extends Node2D
@export var spawn_count : int = 1
@export var minimum_walk : int = 100
var drunkards: Array[Vector2i] = []
var step_count : int = 0
#creates the initial dungeon as an array filled with arrays
func generate (size: Vector2i) -> Array[Array] :
var map : Array[Array] = []
for i in size.x:
map.push_back([])
for j in size.y:
map[i].push_back([1])
step_count = 0
for count in spawn_count:
drunkards.push_back(spawn(size))
#carves out spwan point of each drunkard
for drunkard in drunkards:
map[drunkard.x][drunkard.y] = 0
for step in minimum_walk:
#until minimum steps is reached, step in random direction and carve out final position
crawl(size, map)
#when called, each drunkard will take 1 step in random direction
func crawl(size : Vector2i, map: Array[Array]):
var updated_drunkard : Array[Vector2i] = []
var directions : Array[Vector2i] = []
for drunkard in drunkards:
if drunkard.x > 0: # if drunkard is to the left side
directions.push_back(Vector2i(1, 0))
if drunkard.x < size.x - 1: # if drunkard is to the right side
directions.push_back(Vector2i(-1, 0))
if drunkard.y > 0: # if drunkard is at the top
directions.push_back(Vector2i(0, -1))
else:
directions.push_back(Vector2i(0,0))
var walk_direction : Vector2i = directions.pick_random() # moves left, right, and up, respectively
updated_drunkard.push_back((drunkard as Vector2i) + walk_direction)
map[drunkard.x][drunkard.y] = 0 #carves out the drunkard's position
drunkards = updated_drunkard
#spawn a drunkard in a random spot in the grid
func spawn(size: Vector2i):
#positions drunkard within 0 and the size of the cave (size), and returns position
var drunkard_position : Vector2i = Vector2i(randi_range(0, size.x), 0)
return drunkard_position
Does anyone know how to fix this? If you do, please let me know.