Combining bitmaps ? (binary or)

Godot Version

4.2.1.stable

Question

Hi. I’m using one bitmap for each character in my game to store the positions they see (so that this info can be cached and reused if a unit don’t move). I’m trying to combine theses bitmaps into one, but looping through all the bits in the bitmap from gdscript is too slow. Is there a data structure better suited for that ? If not, I could implement something custom, but I would rather know if I missed something.

If you can use a bitmap, you can use a single array with an associated width. This requires more math on your part, but it is much more performant.

ok that’s what I thought, I started doing that. Thanks.

class NewBitMap:
	var x : int
	var y : int
	var width : int
	var data : PackedInt64Array
	func _init(_x: int, _y: int):
		assert(_x % 64 == 0 and _y % 64 == 0)
		x = _x
		y = _y
		width = _x / 64
		data = PackedInt64Array()
		data.resize(_x*_y / 64)
		data.fill(0)
		
	func operator_or(right : NewBitMap) -> void:
		assert(x == right.x and y == right.y)
		var sz = size()
		for i in range(sz):
			data[i] |= right.data[i]
		
	func set_bit(_x : int, _y : int) -> void:
		assert(_x < x and _y < y)
		var i : int = _x / 64 + _y * width
		var b : int = _x % 64
		data[i] |= 1 << b
		
	func size() -> int:
		return x * y / 64

I’m updating with what I came up with, the performance are good enough for now, if the project ever become serious, I will consider rewriting this in c++.

godot already compiles down to the equivalent of a C++ application, so unless you don’t need most of godot’s features, you’re just shooting yourself in the foot in the name of performance =P

hmm, there is no way interpreted langages (gdscript) can have as good performances as C++, but maybe its difficult to integrate with Godot, I will have to look sometimes, at least by curiosity. But of course I will continue to use Godot, there are good reasons I chose this (amazing) engine.

Scripts get compiled at runtime. Yeah, it’s not as performant as AoT, but unless you’re generating code at runtime, you’re only gonna compile each class once. There’s even decen lambda cacheing and Callable is not as naive as you seem to think.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.