Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Passing variables to threads  (Read 5705 times)

0 Members and 1 Guest are viewing this topic.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Passing variables to threads
« on: September 16, 2009, 02:04:10 am »
I have a while loop which handles a fired bullet. The bullet goes to x distance than disappears. While the bullet is traveling the character cannot move.

To solve this I tried to create a class to start a new thread. In order for it to function I need to pass the bullet sprite and the timer used to measure the bullet distance to the thread/class instance but I don't know a solution besides making them both external. Can anybody show me how to pass this information instead?


(I also have 'App' update the scenery inside the loop while the bullet moves but I figure I can get rid of it once I have a separate working thread)

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Passing variables to threads
« Reply #1 on: September 16, 2009, 06:50:25 am »
Don't use a thread for a simple bullet !  :shock:

You just have to create a bullet object, and update its position every time passed (assign a speed to the bullet). When you draw the sprites, the bullet will go further and further by the time passing. And you could move everything else by the way ;)
Mindiell
----

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Passing variables to threads
« Reply #2 on: September 16, 2009, 08:54:39 am »
Never use nested loops to make something move! The most important thing is not to break your main loop and execution flow, so that everything always gets updated and drawn.

To manage your bullet, just assign it a velocity and make it move slightly at each new frame according to the time elapsed.
Laurent Gomila - SFML developer

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Passing variables to threads
« Reply #3 on: September 16, 2009, 11:47:48 pm »
Quote from: "Laurent"
Never use nested loops to make something move! The most important thing is not to break your main loop and execution flow, so that everything always gets updated and drawn.

To manage your bullet, just assign it a velocity and make it move slightly at each new frame according to the time elapsed.



Thanks, I fixed it and it works fine now. Out of curiosity though how would you pass data to another thread just in case you needed to?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Passing variables to threads
« Reply #4 on: September 17, 2009, 07:47:59 am »
Threads share the same memory, so you can access any variable from any thread.

You can also explicitely pass a variable to the thread function through its "UserData" parameter.
Laurent Gomila - SFML developer