Archived

This topic is now archived and is closed to further replies.

Seriema

NPCn: lists or threads?

Recommended Posts

Hi! I''m working on a RPG on my spare time, been a while (6 months)since I touched it though (because of school). I remember when facing the fact that we''re going to have NPCn walking around doing stuff. The first thought was to put all NPCn in a list and have some function called when iterating the list on each update loop. But now we learned threads in school. And woow! Each NPC could be in a own thread, so I don''t have to care about it! Sure somebody must''ve done this before me! But is it common? Do games that are in the market today do that? Or does the thread-switching take to much valuable processing time (FPS sinks?)? "No lies of sugar can sweeten the sournes of reality" }+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
Having each NPC in its own thread seems very very wasteful. Keep in mind, every time information needs to be passed between threads, you need to use synchronization of some sort (locking with mutexes or whatever) and that''s expensive (threads in themselves aren''t really expensive - the synchronization mechanisms are).

Threads are not storage algorithms ("place each in its own thread"), not are they efficient enough to just use in bulk. When planning multithreading, just write down a list of basic tasks. Either these tasks should have roughly equivalent processing priorities or they should go to sleep often, as implementing specific elaborate timing and scheduling will immediately kill your performance.

One thread for all NPCs (call it the AI thread) seems a much better starting point to me. Personally, I usually end up with one GUI thread, one game thread (aka main thread), one AI thread and one comms thread - that way each thing that can generate bursts of activity is isolated from the others. I also generally make any transfers between the threads asynchronous.

Hope that made some sense.

(My projects and ramblings...)

Share this post


Link to post
Share on other sites