title: C++ problem with sf::sprite in a std::vector not keeping its updated position (despite passing by reference)?
body: <ol>
  <li>In main, create soldier object</li>
  </ol>
  <p>Soldier soldier;</p>
  <ol start="2">
  <li>create soldier sprite and set its position</li>
  </ol>
  <p>//Load a soldier sprite</p>
  <pre><code>sf::Texture texture2;
  if (!texture2.loadFromFile(resourcePath() + &quot;soldier.png&quot;))
  {
      return EXIT_FAILURE;
  }
  sf::Sprite spriteSoldier(texture2);
  spriteSoldier.setPosition(winwidth/2, winheight/2);
  spriteSoldier.setOrigin(spriteSoldier.getLocalBounds().width/2,
  spriteSoldier.getLocalBounds().height/2);
  </code></pre>
  <ol start="3">
  <li>Load bullet texture for later
  //load bullet texture
  <pre><code>if(!bulletTexture.loadFromFile(resourcePath()+&quot;Bullet.png&quot;)){
      return EXIT_FAILURE;
  }
  
  </code></pre>
  </li>
  <li>Inside while (window.isOpen())</li>
  </ol>
  <p>call to processEvents</p>
  <pre><code>while(timeSinceLastUpdate &gt; TimePerFrame)
      {
          timeSinceLastUpdate -= TimePerFrame;
          
          processEvents(window, spriteSoldier, game, soldier, bulletTexture,
                      elapsedTime);
          
      }
  
  void processEvents(sf::RenderWindow&amp; window, sf::Sprite&amp; spriteSoldier, Game&amp; g, Soldier&amp; 
  soldier, sf::Texture&amp; bulletTexture, sf::Time elapsed)```
  </code></pre>
  <ol start="5">
  <li><p>Initially this if statement isn’t entered as there are no bullets in the vector</p>
  <p>std::vector tvec = soldier.RetClip();
  if ( tvec.size() &gt; 0 )//only enters if created a bullet and added
  {
  //update position of the bullet, this is the sprite
  sf::Vector2f currentpos{};
  currentpos = tvec[0].getPosition();
  //std::cout &lt;&lt;&quot; currentpos.x before &quot; &lt;&lt; currentpos.x &lt;&lt; std::endl;</p>
  <pre><code> currentpos += (tvec[0].getVelocity() * elapsed.asSeconds());
   //std::cout &lt;&lt;&quot; currentpos.x after &quot; &lt;&lt; currentpos.x &lt;&lt; std::endl;
  
   //set to current position
   tvec[0].setPosition(currentpos);
   //std::cout &lt;&lt;&quot; currentpos.x after reset &quot; &lt;&lt; tvec[0].m_sprite.getPosition().x&lt;&lt; 
   std::endl;
   }
  </code></pre>
  </li>
  <li><p>Later on in this function inside</p>
  </li>
  </ol>
  <p>sf::Event event;
  while (window.pollEvent(event))</p>
  <pre><code>//synch soldier position with its sprite.
          soldier.setPosition(spriteSoldier.getPosition());
          if (event.key.code == sf::Keyboard::Space)
          {
              //going to be fired immediately, it's the data that is put into the bulletclass
              float currentrotation = (soldier.getRotation() + 90.0); //align bullet with gun
              //create sprite
              sf::Sprite tempSprite(bulletTexture);//maybe rotate it??
              tempSprite.rotate(currentrotation);//keeps this its whole life
              tempSprite.Transformable::setPosition(spriteSoldier.getPosition());//start where soldier is
              //create a temp bulletclass object to hold sprite details
              //BulletClass tempBulletClass(currentrotation, direcNormalised, spriteSoldier.getPosition(), tempSprite);
              std::unique_ptr&lt;BulletClass&gt; tempBulletClass ( new BulletClass(currentrotation, direcNormalised, spriteSoldier.getPosition(), tempSprite) );
              //add this tempbullet object so the soldier's clip
              soldier.addBulletToClip(*tempBulletClass);//hoping they are copying object
          }
  </code></pre>
  <p>This creates a bulletclass instance,
  this is sent to soldier.addBulletToClip</p>
  <pre><code>void Soldier::addBulletToClip(BulletClass&amp; bullet)
  {
      std::cout &lt;&lt;&quot; bullet added to clip &quot; &lt;&lt; std::endl;
      BulletClip.push_back(bullet);
  }
  </code></pre>
  <p>So the bulletclass instance is added to the std::vector BulletClip;</p>
  <p>This all seems ok, BUT BUT BUT when it returns to main and that if statement again …</p>
  <pre><code>std::vector&lt;BulletClass&gt; tvec = soldier.RetClip();
  if ( tvec.size() &gt; 0 )//only enters if created a bullet and added
  {
      //update position of the bullet, this is the sprite
      sf::Vector2f currentpos{};
      currentpos = tvec[0].getPosition();
      //std::cout &lt;&lt;&quot; currentpos.x before &quot; &lt;&lt; currentpos.x &lt;&lt; std::endl;
  
      currentpos += (tvec[0].getVelocity() * elapsed.asSeconds());
      //std::cout &lt;&lt;&quot; currentpos.x after &quot; &lt;&lt; currentpos.x &lt;&lt; std::endl;
  
      //set to current position
      tvec[0].setPosition(currentpos);
      //std::cout &lt;&lt;&quot; currentpos.x after reset &quot; &lt;&lt; tvec[0].m_sprite.getPosition().x&lt;&lt; std::endl;
  }
  </code></pre>
  <p>The problem seems to be that whenever this is looped through (once a bullet exists)</p>
  <p>This bit of code always states that the x component of the bullet sprite is 1300 (i.e. where the soldier sprite is at and the initial value of the bullet sprite as it was added to the std::vector).</p>
  <p>This bit of code does do what is expected and apparently change the value of currentpos based on velocity and elapsed time</p>
  <pre><code>currentpos += (tvec[0].getVelocity() * elapsed.asSeconds());
  </code></pre>
  <p>I then want to make sure that currentpos does update the position of the sprite, hence</p>
  <pre><code>//set to current position
  tvec[0].setPosition(currentpos);
  //std::cout &lt;&lt;&quot; currentpos.x after reset &quot; &lt;&lt; tvec[0].m_sprite.getPosition().x&lt;&lt; std::endl;
  </code></pre>
  <p>This all seems fine … it does call</p>
  <pre><code>sf::Vector2f BulletClass::setPosition(sf::Vector2f in)
  {
      m_position.x = in.x;//keep local copy
      m_position.y = in.y;
      std::cout &lt;&lt;&quot; in.x to change x &quot; &lt;&lt; in.x &lt;&lt; std::endl;
      m_sprite.setPosition(in);
      std::cout &lt;&lt;&quot; sprite.x now...  &quot; &lt;&lt; m_sprite.getPosition().x &lt;&lt; std::endl;
  }
  </code></pre>
  <p>and the checks seem to tell me, yes all is good I’ll update the sprite i’m storing for you …</p>
  <p>BUT then next time round, in process events …</p>
  <pre><code>std::vector&lt;BulletClass&gt; tvec = soldier.RetClip();
  if ( tvec.size() &gt; 0 )//only enters if created a bullet and added
  {
      //update position of the bullet, this is the sprite
      sf::Vector2f currentpos{};
      currentpos = tvec[0].getPosition();
      //std::cout &lt;&lt;&quot; currentpos.x before &quot; &lt;&lt; currentpos.x &lt;&lt; std::endl;
  </code></pre>
  <p>the currentxpos is back to the very initial value of 1300, consequently the sprite on the screen never move and is always at the same place just below the feet of the soldier.</p>
  <p>This is the code at the bottom of main that draws the bullet</p>
  <p>//try and draw the bullets details, stored in the clip</p>
  <pre><code>std::vector&lt;BulletClass&gt; temp = soldier.RetClip(); //returns a ref to the clip
      for (auto bull = temp.begin(); bull != temp.end(); ++bull)
      {
          window.draw(bull-&gt;returnSprite());
      }
      window.display();
  </code></pre>
  <p>It seems that any call to process events (ie. updating the bullet position) isn’t kept (it’s like groundhog day - the bullet always starts off at the initial place) …</p>
  <p>I have checked that I am sending the soldier object by reference so that any changes to these objects in process events should be permanent</p>
  <p>Soldier&amp; soldier</p>
  <p>Clearly the soldier object keeps its bullet object in its std::vector (otherwise it wouldn’t keep returning its position of 1300 on x-axis).</p>
  <p>So inside the keyboard::space (in void processevents) the soldier object was permanently modified. But inside the if statement (also in void processevents) the soldier object wasn’t permanently modified.</p>
  <p>Would appreciate any help as I’m completely stumped.</p>