SFML community forums

Help => System => Topic started by: Jarwulf on September 16, 2009, 02:04:10 am

Title: Passing variables to threads
Post by: Jarwulf 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)
Title: Passing variables to threads
Post by: Mindiell 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 ;)
Title: Passing variables to threads
Post by: Laurent 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.
Title: Passing variables to threads
Post by: Jarwulf 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?
Title: Passing variables to threads
Post by: Laurent 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.