Can anyone explain why i get false for Array.all?

Hi there! I have created tilemap with script to show some zone on it under the mouse cursor. And it works fine for every tile on map except (0, 0). Can anyone explain me why? I expect that zone with (0, 0) must be green


Steps to reproduce

  1. Create Tilemap node, set 64x64 size, create 2 layers
  2. On layer0 draw rectangle map
  3. Add script with code and hit play:
extends TileMap

var current_mouse_position_in_tilemap_coords : Vector2i

var map_side_x : Array
var map_side_y : Array

enum {
	layer0 = 0, 
	layer1 = 1,
	}

func _ready():
	map_side_x = range(self.get_used_rect().position[0], self.get_used_rect().end[0])
	map_side_y = range(self.get_used_rect().position[1], self.get_used_rect().end[1])
	
func _physics_process(_delta) :
	
	current_mouse_position_in_tilemap_coords = self.local_to_map(self.get_global_mouse_position())
	
	clear_layer(layer1)

func _process(_delta):
	var object : Rect2i = Rect2i(current_mouse_position_in_tilemap_coords, Vector2i(2,2))
	var object_foundation : Array = []
	
	for i in range(object.abs().position.x, object.abs().end.x+1) : 
		for y in range(object.abs().position.y, object.abs().end.y+1) :
			object_foundation.append(Vector2i(i, y))
	
	if object_foundation.all(CanBuild) : 
		
		for object_cell in object_foundation.size() :
			self.set_cell(layer1, object_foundation[object_cell], 0, Vector2i(0,0))
	
	else :
		print("Can't build") 
		
		for object_cell in object_foundation.size() : 
		
			self.set_cell(layer1, object_foundation[object_cell], 0, Vector2i(1,0))

func CanBuild(tile) :
	if (tile.x in map_side_x and tile.y in map_side_y) :
		return tile

Tested versions
Reproducible: v4.2.2.stable.official [15073af]

System information
Godot v4.2.2.stable - Windows 10.0.19045 - Vulkan (Mobile) - integrated Intel(R) Iris(R) Xe Graphics (Intel Corporation; 31.0.101.3616) - 11th Gen Intel(R) Core™ i5-1145G7 @ 2.60GHz (8 Threads)

actually tilemap size and doesnt really matter, choose any you like

Why cant you just print some output inside CanBuild and see what’s happening. Also this method should be return a bool. You are returning a tile.

Calls the given Callable on each element in the array and returns true if the Callable returns true for all elements in the array. If the Callable returns false for one array element or more, this method returns false.

1 Like

That the method returns the tile (a Vector2i) instead of a bool is actually the problem, because Vector2i.ZERO is falsy, i.e. if Vector2i.ZERO: print("hello') never prints hello. Every other value for the vector does.

1 Like

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