3 Replies - 1387 Views - Last Post: 09 December 2012 - 07:15 AM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

SFML thread variable argument passed by reference not changed

Posted 08 December 2012 - 06:43 PM

I pass Player player as a reference to the ReceiveMessageThread() thread function but the changes in the function do not actually change the player object's variables.


The irrelvant parts of code have been removed and replaced with "..."

...
//////////////////////////////////////////////////////////////////////////////
// THREAD FOR RECEIVING MESSAGES //
void ReceiveMessageThread(Player& p)
{
	 ...

		 else // message receivied
		 {
                      packet >> rDStruct;
       
			 p.xPos = rDStruct.xPos;// sets xPos to 95
			 ...

			 std::cout<< p.xPos <<std::endl; // this outputs 95 as it should.
	 }
}




///////////////////////////////////////////////////////////////////
// *** ENTRY POINT OF PROGRAM *** //
int main()
{
	...

	Player player;

        ...

	 //Launch a thread of receiving messages
	 sf::Thread ReceiveThread(&ReceiveMessageThread, player);
	 ReceiveThread.launch();


	 //start the game loop
	 while(Mainwindow.isOpen())
	 {

	/////////////////////////////////////////////////////////////////
	// UPDATE //
		 std::cout<<player.xPos<<std::endl;  // This outputs 100, but it should be 95.

	...

	return 0;
}



This post has been edited by Nano511: 08 December 2012 - 06:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: SFML thread variable argument passed by reference not changed

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: SFML thread variable argument passed by reference not changed

Posted 08 December 2012 - 09:14 PM

You are passing to the ReceiveThread function will that need to be a reference as well?


Here they use a pointer.

Tutorial - System - Using threads and mutexes

This post has been edited by #define: 08 December 2012 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML thread variable argument passed by reference not changed

Posted 09 December 2012 - 07:05 AM

Im using SFML 2.0, the tutorial you posted is for 1.6. In the tutorial for 2.0 on threads, http://www.sfml-dev....stem-thread.php, they dont do it like that. I will try it the way it is shown in 1.6 tutorial just in case though.
Was This Post Helpful? 0
  • +
  • -

#4 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML thread variable argument passed by reference not changed

Posted 09 December 2012 - 07:15 AM

Yes that fixed it thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1