content

An acquaintance in a Godot Discord channel mentioned today that Godot has custom iterators: https://docs.godotengine.org/en/4.7/tutorials/scripting/gdscript/gdscript_advanced.html#custom-iterators

The example in the documentation as of 4.7 is:

class ForwardIterator:
	var _start
	var _end
	var _increment

	func _init(start, end, increment):
		_start = start
		_end = end
		_increment = increment

	func _should_continue(current):
		return current < _end

	func _iter_init(iter):
		# Initialize the state to store the current value.
		iter[0] = _start
		return _should_continue(iter[0])

	func _iter_next(iter):
		iter[0] += _increment
		return _should_continue(iter[0])

	func _iter_get(iter):
		# The state is not wrapped in an array for `_iter_get()`.
		# The iteration value is the same as the state.
		return iter

My acquaintance couldn't find it because they were searching for a "generator function" which is a related concept. I could potentially use the custom iterators to implement generator functions, to make custom coroutines.

I didn't know about this feature and I was wondering how far back it went. I found reference to it as far back as 3.4

meta

created:

commit: 817f2708