0 votes

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();
Godot version 3.5
in Engine by (22 points)

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

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.

1 Answer

0 votes
Best answer

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.

by (22 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.