Skip to main content
gamedev.net
🔒 Locked

[.net] make it multi-threaded?

Started by nordwindranger Feb 16, 2007 at 5:24 PM 4 replies 850+ views
Original Post
nordwindranger
nordwindranger
I'm relatively new to game programming with c# and managed direct x (been doing it for about 1/2 a year), and I've gotten to the point in my project where I'm giving some serious thought to putting in some pretty decent strategic level AI. Before now all of the ai subroutines (and pretty much everything else non graphics related) has been called in the update method of the render loop. However a lot of the ai stuff doesn't need to be called every iteration of the render loop. So the question is do I need another thread to handle all of the ai (and non graphics/updating stuff), or do I keep calling everything from the update method and just put a timer on it? I've been having a oddly hard time finding anything on this subject, and all of my game programming books stop short of talking about this. thanks (p.s. I'm not writing this with multicore processors in mind.. if that matters)
dalep
dalep
I don't reckon that sort of thing would work well in its own thread. In order to make AI decisions, this thread would have to be able to look at the state of the game data and to do that safely, you're going to have to lock things all over the place. MainTread and AIThread would be fighting over stuff constantly, it would probably perform worse than just doing it all in one thread.

If you can process your AI every Update without it hurting your framerate/performance, then why not? And if it does, then maybe you could break it up into separate tasks and put those into some sort of circular queue and just process one of them each update. So if there are 5 tasks, then each gets processed every 5th update. A system like that could be very flexible and fine-tune-able.
avianRR
avianRR
Quote:
Original post by nordwindranger
Before now all of the ai subroutines (and pretty much everything else non graphics related) has been called in the update method of the render loop. However a lot of the ai stuff doesn't need to be called every iteration of the render loop.


If it dosn't need to be updated every iteration then don't!
Try this...

public int LastTick; // add to class
public int AI_Rate; // add to class - set to number of milliseconds between AI updates.

// add to update function.
int NowTick = System.Enviroment.TickCount;
if((NowTick - LastTick) > AI_Rate){
LastTick = NowTick;
// do AI stuff here
}

I normaly try to give hints to how to write something and let the person figure it out as a learning experience. You get lucky because I'm in a really good moode today.

As a side note. You could change the value of AI_Rate to make the decisions happen more often as part of a difficulty setting. You can beat a computer really easy if it only makes one decision every 200ms but what about every 1ms? hehe.... difficulty setting="Yer Gonna DIE!"
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
jfclavette
jfclavette
Quote:
Original post by dalep
I don't reckon that sort of thing would work well in its own thread. In order to make AI decisions, this thread would have to be able to look at the state of the game data and to do that safely, you're going to have to lock things all over the place.

MainTread and AIThread would be fighting over stuff constantly, it would probably perform worse than just doing it all in one thread.


AIThread shouldn't have to modify anything. That reduces the need for locking a whole lot, and trought careful design / depending on the type of game and the information needed, you might very well be able to eliminate them almost completely. I'm currently working on a game-like simulation where each agent has its own thread, and it hasn't gotten too messy yet.

Different strategies are possible. If the mutable game state info that the AI needs is small, refreshing a copy at set intervals and have the AI work off it could work. It doesn't matter in the grand scheme of things if AI is working on 30ms old datasets.

I'm not saying it's the ideal solution in your case, but I wouldn't shy away from giving it a try.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
dalep
dalep
It would have to modify *something* - there's little point in having an AI if it doesn't effect the state of the model in some manner ;)

But you're right. Its a judgement call.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.