How to only keep part of an Image as a Sprite Texture?

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

Hello everyone, I have to make a Puzzle game for work. The idea is that I download an image from our servers and I break it to pieces according to a mask I have that has a different Red value for each piece. The problem is that the moment I get the data of the image the data is gone. Manipulating it or not is irrelevant. If I just get the data and then immediately copy it to an ImageTexture, I get a blank texture.

Here’s what I have so far:

for i in range ($"/root/GobalVars".numberOfPieces):
		$"/root/GobalVars".pieces.push_back (load ("res://Game/Puzzle/Piece.tscn").instance());
		add_child($"/root/GobalVars".pieces[i]);
		$"/root/GobalVars".pieces[i].id = i+1;
		$"/root/GobalVars".pieces[i].position = Vector2 (570, 220) + Vector2 (($"/root/GobalVars".pieces[i].maxX - $"/root/GobalVars".pieces[i].minX) /2, ($"/root/GobalVars".pieces[i].maxY - $"/root/GobalVars".pieces[i].minY) /2);
		pieces.push_back(Image.new());
		pieces[i].create(468,468,false, Image.FORMAT_RGBA8);
		pieces[i].lock();
	
	var mask = Image.new();
	mask = $HiddenMask.texture.get_data();
	mask.lock();
	
	var pieceImage = Image.new();
	pieceImage = $"/root/GobalVars".selectedImage.get_data();
	pieceImage.resize(468,468);
	pieceImage.lock();
	
	for i in range (pieceImage.get_width()):
		for j in range (pieceImage.get_height()):
			if (int (mask.get_pixel(i, j).r * 255) != 0):
				pieces[int (mask.get_pixel(i, j).r * 255)-1].set_pixel(i,j, pieceImage.get_pixel(i,j));
				if ($"/root/GobalVars".pieces[int (mask.get_pixel(i, j).r * 255)-1].minX == -1):
					$"/root/GobalVars".pieces[int (mask.get_pixel(i, j).r * 255)-1].minX = i;

				$"/root/GobalVars".pieces[int (mask.get_pixel(i, j).r * 255)-1].maxX = i;

				if ($"/root/GobalVars".pieces[int (mask.get_pixel(i, j).r * 255)-1].minY == -1):
					$"/root/GobalVars".pieces[int (mask.get_pixel(i, j).r * 255)-1].minY = j;

				$"/root/GobalVars".pieces[int (mask.get_pixel(i, j).r * 255)-1].maxY = j;
	
	for i in range (len($"/root/GobalVars".pieces)):
		var pieceTexture = ImageTexture.new();
		pieceTexture = pieceTexture.create_from_image(pieces[i]);
		$"/root/GobalVars".pieces[i].texture = pieceTexture;
		$"/root/GobalVars".pieces[i].region_rect = Rect2($"/root/GobalVars".pieces[i].minX, $"/root/GobalVars".pieces[i].minY, $"/root/GobalVars".pieces[i].maxX - $"/root/GobalVars".pieces[i].minX, $"/root/GobalVars".pieces[i].maxY - $"/root/GobalVars".pieces[i].minY);
		pieces[i].unlock();
	
	mask.unlock();
	pieceImage.unlock();

Did You try to save new image as a png file ? Try if it works

Inces | 2022-08-24 12:13

Actually I found the problem.

I was doing:

pieceTexture = pieceTexture.create_from_image(pieces[i]);

While it just needs to be:

pieceTexture.create_from_image(pieces[i]);

I was getting a null reference otherwise.

CosmicHappiness | 2022-08-24 14:06

:bust_in_silhouette: Reply From: CosmicHappiness

Actually I found the problem.

I was doing:

pieceTexture = pieceTexture.create_from_image(pieces[i]);

While it just needs to be:

pieceTexture.create_from_image(pieces[i]);

I was getting a null reference otherwise.