NPC management tutorial?[Solved]

Started by
3 comments, last by polarboy 14 years, 3 months ago
Hi, guys, I'm spending my little break between school thinking about my game again. I'm trying to start on NPC management. But I'm not sure how I would approach this. To me, the simplest way to approach this to have a thread for each NPC and perform calculations there, but I don't think that would be a good idea for memory reasons. (Especially if you have hundreds of NPCs like a lot of KOEI games) But I just can't seem to find any other way to do it, I mean, if we use a loop to loop through the NPC list and determine what to do. For example NPC->do_move(); So the NPC executes one line in the script My understanding is we can't just go to the next guy and come back later to execute the next line in the script, unless of course we register each line as a function and send it back to C/C++ (-_-||) I'm thinking all sorts of crazy things like a table, FSM and etc, I just couldn't really figure out how to do it. I guess my question is: What's the normal way to do it? Oh yes, I forgot, the original intent was: Is there any tutorial on how this is achieved? I haven't been able to find much in terms of implementation. Thanks [Edited by - polarboy on December 31, 2009 4:08:53 PM]
Advertisement
What kind of "calculations" are you trying to do? Making a thread for each NPC is certainly the wrong approach. The approach usually taken is to loop through your NPCs/entities every timestep and run some sort of update method. The logic that you've written for that NPC would only do the work for that one given timestep. Actions occurring over multiple timesteps (most actions) would be state driven.

For example, if you NPC is moving to point X, you'd probably mark that the NPC is moving towards that point, and then each update you'd check whether it has arrived, and if not then move Y units towards X. Once you reach that point, you'd clear that state, or start with a new action.

I'm not exactly sure what you meant by your script, but it sounds like you meant that your script would execute one line, then yield execution back, and then the next time you run your script it would pick up where you left off. You can do that sort of thing with co-routines, but I wouldn't advise it.
Quote:Original post by Rycross
What kind of "calculations" are you trying to do? Making a thread for each NPC is certainly the wrong approach. The approach usually taken is to loop through your NPCs/entities every timestep and run some sort of update method.

I'm not exactly sure what you meant by your script, but it sounds like you meant that your script would execute one line, then yield execution back, and then the next time you run your script it would pick up where you left off. You can do that sort of thing with co-routines, but typically what is done is to build some sort of simple state machine and then make your NPC behavior event driven.


Wow, I wasn't expecting a reply so quickly, thanks for the quick reply.

My calculations are things like, the new position over certain amount of time and stuff like that.

Yes, I was thinking about execute one line and come back to it. I can go look up co-routines now, haha, didn't even know there was such a thing.

I thought of state machines, but I'm not sure how you would implement one.
Like I was thinking states such as "moving", "fighting", "idling", but I had difficulties with it...can't remember what o.O
I'll give it some more thought, maybe it does work, haha

Thanks a lot
Quote:Original post by polarboy
I thought of state machines, but I'm not sure how you would implement one.
Like I was thinking states such as "moving", "fighting", "idling", but I had difficulties with it...can't remember what o.O
I'll give it some more thought, maybe it does work, haha


I actually decided to re-write part of my post while you were responding, because I didn't really feel that it was clear. Let me re-quote that:

Quote:Original post by Rycross
For example, if you NPC is moving to point X, you'd probably mark that the NPC is moving towards that point, and then each update you'd check whether it has arrived, and if not then move Y units towards X. Once you reach that point, you'd clear that state, or start with a new action.


Does that give you an idea? Of course, your NPC may be moving towards X while shooting a gun, so you may have to be able to mark your NPC as "moving to X" and "fighting," but its not too hard to just mark your NPC as doing both.
Quote:Original post by Rycross
Quote:Original post by polarboy
I thought of state machines, but I'm not sure how you would implement one.
Like I was thinking states such as "moving", "fighting", "idling", but I had difficulties with it...can't remember what o.O
I'll give it some more thought, maybe it does work, haha


I actually decided to re-write part of my post while you were responding, because I didn't really feel that it was clear. Let me re-quote that:

Quote:Original post by Rycross
For example, if you NPC is moving to point X, you'd probably mark that the NPC is moving towards that point, and then each update you'd check whether it has arrived, and if not then move Y units towards X. Once you reach that point, you'd clear that state, or start with a new action.


Does that give you an idea? Of course, your NPC may be moving towards X while shooting a gun, so you may have to be able to mark your NPC as "moving to X" and "fighting," but its not too hard to just mark your NPC as doing both.


Yeah, I think I got it, it makes sense. I'm just gonna sit there for a while and plan everything out

This topic is closed to new replies.

Advertisement