Godot Version
v4.4.1.stable.official [49a5bc7b6]
Question
Hi, I would like to iterate through all 18 cubes that I have in the mesh library of a grid map and put the preview texture into a grid container, so that I can have an inventory of all cubes in a Minecraft-like project.
So I have tried the following code in my world.gd:
@tool
extends Node3D
@onready var grid_map: GridMap = $GridMap
@onready var grid_container: GridContainer = %GridContainer
@export var generate_previews: bool = false:
set(value):
print("generate_previews value: ", value)
generate_previews = value
if value:
generate_item_previews()
func generate_item_previews():
print("Engine.is_editor_hint(): ", Engine.is_editor_hint())
if not Engine.is_editor_hint():
return
# Remove previous previews if any
for c in grid_container.get_children():
grid_container.remove_child(c)
for item_id in grid_map.mesh_library.get_item_list():
print("item_id: ", item_id)
var preview := grid_map.mesh_library.get_item_preview(item_id)
if preview is Texture2D:
var tex_rect := TextureRect.new()
tex_rect.texture = preview
tex_rect.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
grid_container.add_child(tex_rect)
And then I do see the “Generate Previews” checkbox in the world.tcsn:
And I do see the 18 preview textures in the same scene in the editor:
But when I run my project then the grey Panel and the GridContainer are empty, why? (there is only the 3D hotbar at the bottom, but it is unrelated, please ignore):
Here is my whole project, thank you: