Why is draw_texture_rect showing white square instead of the texture?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Methamane

Hi, I’ve created my own grid system that I draw using draw_rect which is working great.
I wanted for certain cells to have a texture instead of a square with 1 color so I tried loading and drawing the texture this way:

var pic = Image.new()
var pic_tex = ImageTexture.new()
pic.resize(grid.size, grid.size)
draw_texture_rect(pic_tex, row.rect, false)

If it matters the size that I’m resizing it to is 60x60, I’m doing this so that it fits the grid that I’ve created. This is drawing just a white square instead of the image that I’ve loaded, any help would be appreciated.

:bust_in_silhouette: Reply From: whiteshampoo

I have tried it like this, and it works for me:

extends Node2D

var grid : Rect2 = Rect2(Vector2(0, 0), Vector2(32, 32))
var pos : Rect2 = Rect2(Vector2(32, 32), Vector2(32, 32))
var pic = Image.new()
var pictex = ImageTexture.new()

func _ready():
	pic.resize(grid.size.x, grid.size.y)

func _process(_delta):

func _draw():
	draw_texture_rect(pictex, pos, false)

you should maybe use preload instead of load. You also don’t need to resize the image, because draw_texture_rect can resize it on-the-fly with the Rect2.size value

I can’t really preload as ~95% of the assets would be loaded by the users on the fly, and so all of the loading and conversions are done right now within the _draw() function because the node is used only as a canvas for the grid which is dynamic.
Anyway moving:

var pic = Image.new()
var pic_tex = ImageTexture.new()

to be outside of the _draw() function did the trick with everything else staying within the _draw().
Thanks for the help

Methamane | 2020-07-08 13:06