* * *

Author Topic: Simple multithreading code example in Free Pascal  (Read 1360 times)

semichaud1

  • New member
  • *
  • Posts: 36
Re: Simple multithreading code example in Free Pascal
« Reply #15 on: April 18, 2018, 02:52:38 am »
Thanks cdbc!

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Simple multithreading code example in Free Pascal
« Reply #16 on: April 18, 2018, 02:58:09 am »
So from what I can figure the shaking graphics (only characters are shaking, the background tiles are not) comes from the fact that the main thread is rendering the characters while the additional thread is still processing the characters coordinates when they move.
Not entirely. The shaking comes from the fact that things are not in sync (or simply take too much time).

Quote
So I have no choice but to include the procedure in the main thread instead.
No, yo still can use threads (and when implemented properly they are able to speed up your engine). However, most games are linear to such extend that using threads makes absolutely no sense at all.

On the other hand (and as a hopefully simple example) imagine that you have need for a longwinded movement calculation for an endboss that you know is upcoming if the player continues onward, then you could calculate some things in advance while the player hasn't reached this endboss yet, and loads it's graphics from disk.

Quote
I will find some other use for the additional thread, for example  quicksaves without interrupting gameplay. I was thinking I could also use an additional thread for the calculations relating to combat in the game. Then have the main thread render the combat graphics separately but I will have to only process variables that have no impact on the graphics at all. Like how much hp a target loses and what status effects are applied but that so low in computational demands that I'm starting to wonder why have multithreading at all except for quicksaves. Sound effects maybe.
Threads requires syncing and that doesn't play very well with linear advancement of a 2d game that needs direct visualization in order to react (speedy) on game-events.

I really wonder what it is that takes so much time to calculate because that is usually not a problem unless you already have a bottleneck somewhere in there. Mind that a mistake/oversight can be made very easily in game-development. have you timed your calculation and are perhaps able to provide us an average time that is taken by your calculations ?

PS: why do people keep advertising 3th party thread libraries. SDL has it's own threading support and none of the 3th party thread libraries will actually solve TS' problem instead just waste TS' time.

semichaud1

  • New member
  • *
  • Posts: 36
Re: Simple multithreading code example in Free Pascal
« Reply #17 on: April 18, 2018, 03:45:27 am »
Thanks for all the advice molly.

What I am running in a separate thread is only the procedure for updating character position based on input from the player. Also my game is a party-based rpg so I have three other characters following the controlled character around. In that procedure is also the code for updating the other characters' position so they follow the controlled character. I am not using an A star pathfinding algorithm for that. I simply made my own algorithm based on what I observed playing Chrono Trigger :) (I have programmed A star in the past and I plan to do it again for this project). So the algorithm is very light. Now that I think about it part of that algorithm is situated in a procedure in the main loop as well. It could be that also causing the problem. Because the only graphics shaking are the sprites for the characters. The background and object tiles layers are not shaking. Unforunately, I cannot decouple that code from the procedure in the main loop. The character follow algorithm would not run properly and there is no other structure that would make it run properly, according to logic. I'll have a look again later today. It's 2:45 am and I have to start work in a bit.
« Last Edit: April 18, 2018, 03:48:20 am by semichaud1 »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Simple multithreading code example in Free Pascal
« Reply #18 on: April 18, 2018, 04:49:09 am »
No problem semichaud1. Please take your time.

If i got you to think about your implementation and this made you come up with a possible culprit (and indeed that sounds like one) then the goal was reached  :)

Your 'follow' algorithm could be an example that could be situated inside a thread. As long as the path doesn't change during (at least some) frame updates then you could pre-calc that path for a certain amount of frames and use that inside your main thread to graphically update your character's sprite positions (in a timely/synced) manner of course. Every time you main character changes direction you can start that thread again and repeat the process (or do a thread for every individual sprite, although that seems counterproductive for a-search paths) . If it is actually worth the effort in that you will gain speed in your overall performance is questionable though, especially when calculations are 'light'.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus