Hello There,

I am currently making a small game in c++ and Qt. In the game, an npc call's the Wander() function which makes it randomly wander around. However, only one of these npc's can call the Wander() function at any one time - Is there a way around this?

If you need any more explaining then please leave a post. Been stuck on this silly problem for a while and was wondering if somebody could help me out.

Thanks in Advance,
Matt

Recommended Answers

All 7 Replies

Is there a way around this?

Run each dude in its own thread of execution. Or write Wander() so that it handles steps rather than the whole random wandering that I assume it does. That way you can process one step for each NPC in sequence and it'll appear as if things are happening simultaneously.

Honestly, if you can avoid threads without bumping or exceeding the complexity of using threads, I'd suggest that you do so.

Run each dude in its own thread of execution. Or write Wander() so that it handles steps rather than the whole random wandering that I assume it does. That way you can process one step for each NPC in sequence and it'll appear as if things are happening simultaneously.

Honestly, if you can avoid threads without bumping or exceeding the complexity of using threads, I'd suggest that you do so.

Thanks man, I completely get where you are going but there are very heavy complexities when running each npc in it's own thread. The game runs off lua so there is a lot of dynamic creation and variables that need to constantly be updated. At the moment, I have gotten it to work to some degree - I have used threads and it has cost me a lot of CPU and a lot of FPS drops :( I will keep trying though thanks :)

but there are very heavy complexities when running each npc in it's own thread

Yes, that's why I recommended against it if possible and gave an alternative that runs sequentially in small steps.

Yes, that's why I recommended against it if possible and gave an alternative that runs sequentially in small steps.

Using my current equation for wandering, It would be impossible to initiate small steps that alternate between each call of the function. Dead end?

Obviously it would require changes to your design. If you're not willing to make those changes then yes, it's a dead end.

In C++, two or more functions can share
the same name as long as their parameter
declarations are different.

• In this situation, the functions that share
the same name are said to be overloaded,
and the process is referred to as function
overloading

@annajazz
Indeed, in C++ we can have the same name for functions but with different arities.
Although your answer is correct, it's not what the OP's looking for. He needs to run concomitant two functions, meaning at the same time. The viable thing he can do is what deceptikon suggested, using threads.

Here's the op request:
How to use the same function twice at the same time?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.