Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Computer game events

Status
Not open for further replies.

jonnyknowsbest

Technical User
Feb 3, 2004
163
GB
I am starting to look into game programming, however, i have one query. To give an example, i have Star Wars Knights of the Old Republic on my Xbox, although i do not press anything on my controller, the NPC's still move and interact with each other. If there is no "trigger" (ie me moving my character) how do the NPC's still move???
Does the program scan for user input every "so many" timer counts, and if there is no response, move the NPC's accordingly??

Sorry if sounds like a stupid question

Kind regards
 
Well, that depends on thier AI. Possibly they see you and react accordingly. The details of how the see you and how they react depend on the AI programming. If they do not see you, they are doing their normal thing which is also controlled by their AI. The AI code is perhaps updated every 55 milliseconds in a timer event of some sort.

Here is an example: Bob is a NPC (non-player character right?) He is going somewhere because in this particular game he has a job. The direction he is going is the same unless the AI function tells him to do something different. Since something can happen at any moment, we call his AI function every 55 milliseconds in a timer event. In real life this would be like remembering what we are doing and yet are able to change what we do if something happens.

Any more questions?

-Bones
 
I've written a game with Direct3D myself, so I can say how I did it(and I'm 99% sure others do it this way too). I used 30 frames a second (I think), and every interval the AI would be updated based on the position of everything else. When there was no Window's message to handle and the AI wasn't ready to be updated yet, I simply called Render().

My message pump looks like this:

Code:
MSG msg;
while(1)
{
	if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
	{
		if(!GetMessage(&msg, NULL, 0, 0))
		{
			Cleanup();
			return msg.wParam;
		}
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	else
		Frame(); // Do a frame if there is no message waiting
}

Frame() doesn't actually do a frame every time, it generally just calls Render() and returns. I have a static variable keeping track of the time since the last AI update, and if it is less than the predefined AI update interval, it updates the AI.

I hope this answers your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top