sprite out of scope because it's local
<p>I'm having trouble implementing this function. </p>

<p>//Engine.cpp</p>

<pre><code>void Game::createPlayer(sf::Sprite &amp;player)
{   ///Can't get this to work
    sf::Texture player_texture;
    if (!player_texture.loadFromFile("player.png")) 
    {
        //Error Loading
    }
    player.setTexture(player_texture);
}
</code></pre>

<p>I want it to replace the "creates player in void Game::run but I realize that Player_texture is local to Createplayer and that it won't exist when the function returns.</p>

<pre><code>void Game::run() 
{
    sf::RenderWindow window(sf::VideoMode(SCREEN_X, SCREEN_Y), "Shogun Master");
    srand((unsigned int)time(NULL));
    //Creates Player    [Makes into function]
    sf::Texture player_texture;
    player_texture.loadFromFile("sprites/player.png");
    sf::Sprite player(player_texture);
    //Creates Enemy [Make into function]
    sf::Texture enemy_texture;
    enemy_texture.loadFromFile("sprites/enemy.png");
    sf::Sprite enemy[MAX_ENEMIES];
    for (int x = 0; x &lt; MAX_ENEMIES; x++) 
    {
        enemy[x].setTexture(enemy_texture);
        enemy[x].setPosition(rand_int(100, SCREEN_X - 100), rand_int(100, SCREEN_Y - 100)); //Spawning Point
    }
    //Sets Positions
    player.setPosition(500, 300);

    while (window.isOpen()) 
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            check_closeWindows(event, window);  //Closes Game if Executed
            player_movement(event);             //Moves Character
            attack(event);                      //Character's attacks
        }
        border(player);                 //Border so player does not go off screen
        for (int x = 0; x &lt; total_enemies; x++)
            border(enemy[x]);
        movementUpdate(player, enemy);  //Player &amp; Enemy Movement Updates
        collision(player, enemy[0]);

        window.clear();
        window.draw(player);    //Draws Player
        for (int x = 0; x &lt; total_enemies; x++)
            window.draw(enemy[x]);      //Draws Enemy
        window.display();
    }
}
</code></pre>

<p>so how would I implement this, so that my sprite doesn't return a white box because it went out of scope.</p>

<p>//Engine.h</p>

<pre><code>void Game::createPlayer(sf::Sprite &amp;player);
</code></pre>