#Textures won load correctly

6 messages · Page 1 of 1 (latest)

woven iron
#

Heyho, i managed to code a script which creates a rectangle when i press a key on my keyboard. This rectangle is call Item and gets created by a class. I tried to make wevy texture path global ind Textures.h and Textures.cpp to check if the texture or the texturepath loses runtime during creating and drawing the sprites. Somehow the menubuttons have the textures which i have defined. But the item in the key pressed script gots created but id doesnt have the defined texture

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R) {

               
    std::string test_item = "D:/Coding/Projects/Factory Game/Textures/Raw Ores/Raw Bronze.png";
    Item Test_Item = Item("TestItem", "TestItem", 200, 200, 64, 64, true, test_path, false, 100, 100, 1);

    std::string test_item_index = "1";
    Item_Con.addItem(test_item_index, Test_Item);

    
   
}

I splited the item class into Class.h and Class.cpp:

Item::Item(const std::string& ID, const std::string& onclick_name, int positionX, int positionY, int sizeX, int sizeY, bool visibility,
        const std::string& texturepath, bool dragable, int durability, int quality, int amount)
        : ID(ID), onclick_name(onclick_name), positionX(positionX), positionY(positionY), sizeX(sizeX), sizeY(sizeY), visibility(visibility),
        texturepath(texturepath), dragable(dragable), durability(durability), quality(quality), amount(amount){

        if (!texture.loadFromFile(this->texturepath)) {
            return;
        }

        sprite.setTexture(texture);


        

    }
shadow egretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

woven iron
#

i learn some things from you but i dont know or didnt learn how to fix textures problems

woven iron
#

I solved the problem by creating a copy constructor for the class. by creating the object the constructos creates the object, copys it and include the texture. The texture lifetime ends with creating the object. With the cpoy constructor the script creates a copy of the object and its parameters. This solves the problem of the texture life time

#

!solved