1
General / [Threading] findPath
« on: December 20, 2012, 08:37:54 pm »
Hello,
Currently I have a pathfind algorithm for a set of enemies, which uses quite alot of resources and CPU time. So before I ran the algorithm each frame, causing alot of FPS lag. So I made a class with a thread, that takes care of the calculation.
So here's a sketch of what I have:
Npc class
Now for each new NPC I create, I create a thread managing the path calculations
Thread
that's a pretty rought sketch, but you see what I mean.
Also, with the walkPath method, I just always walk to the second element because, by the time the enemy gets there, i'll probably have a new path, calculated from the node the enemy arrives on and just goes from there
Right now, the program just crashes when I use the thread instead of calculating the path each frame due to a segmentation fault. I don't know what I'm doing wrong...
Please help me, thanks!
Currently I have a pathfind algorithm for a set of enemies, which uses quite alot of resources and CPU time. So before I ran the algorithm each frame, causing alot of FPS lag. So I made a class with a thread, that takes care of the calculation.
So here's a sketch of what I have:
Npc class
class NPC {
public:
void setPath(std::vector<Node*>);
void walkPath();
....
private:
std::vector<Node*> m_path;
}
void walkPath() {
if(m_path.size() >= 2) {
walkToNode(m_path.at(1));
}
public:
void setPath(std::vector<Node*>);
void walkPath();
....
private:
std::vector<Node*> m_path;
}
void walkPath() {
if(m_path.size() >= 2) {
walkToNode(m_path.at(1));
}
Now for each new NPC I create, I create a thread managing the path calculations
Thread
class Thread {
private:
m_player;
m_enemy;
}
void Thread::run() {
sf::Mutex mutex;
mutex.lock();
std::vector<Node*> path = findPath(m_enemy, m_player);
m_enemy->setPath(path);
mutex.unlock();
}
private:
m_player;
m_enemy;
}
void Thread::run() {
sf::Mutex mutex;
mutex.lock();
std::vector<Node*> path = findPath(m_enemy, m_player);
m_enemy->setPath(path);
mutex.unlock();
}
that's a pretty rought sketch, but you see what I mean.
Also, with the walkPath method, I just always walk to the second element because, by the time the enemy gets there, i'll probably have a new path, calculated from the node the enemy arrives on and just goes from there
Right now, the program just crashes when I use the thread instead of calculating the path each frame due to a segmentation fault. I don't know what I'm doing wrong...
Please help me, thanks!