Skip to main content
gamedev.net
🔒 Locked

simultaneous moves?

Started by tiwummbihaou 4 replies 500+ views
Original Post
tiwummbihaou
tiwummbihaou
hi all, i have a game. where there exists a player and a non player character whose behaviours are managed by Artificial Intelligence. However the AI() function works when the player does not do any movement. Is it because I put it in the idle function? Or what can I do to make AI() work while the player is also doing something? Thnx
tiwummbihaou
tiwummbihaou
Yes,
I also want to play the AI even the player is standing still.
We can say it''s a real time simulation kind of a game.
Should I use threads?
Is not there another way?
thnx
jbadams
jbadams
You should have a ''gameloop'', which just loops constantly while the game is running. Within this loop you''ll want to do everything the game needs, including checking for input, updating the player information, rendering to screen, etc. Note that things dont actually occur simultaneously, but that your loop (should) run fast enough that it isnt noticable.

For example, in VB, I have a gameloop that looks sort of like this:


Dim BoolRunning As Boolean
BoolRunning = True

Do While BoolRunning = True
Handlekeys ''handle user input
AI.update ''update AI state
DDraw.Render ''render scene to screen
DoEvents ''let windows have a turn
Loop


Thats taken from one of my first attempts at a game in VB, but it seems to do the trick just fine.
- Jason Astle-Adams
tiwummbihaou
tiwummbihaou
Sorry but I could not understand how a loop can help the subject?
Do you mean I won''t need to use threads in order to make things like real time?
RuneLancer
RuneLancer
A game loop allows you to perform multiple things in a frame. This is assuming that you''re programming under Windows, where using a certain loop is vital to handling messages properly. Every frame, you''d tell your game to do something. For instance, you could have...

while(true)
{
if(message) handleMessage(message);
else
{
doAI();
checkStates();
handleInput();
updateSprites();
updateDisplay();
}
}
where you''d use whatever language you''re using''s respective message-checking functions (most likely the Win32 API''s) and logic-handling/function calling reserved keywords/syntax/whathaveyou.

Are you coding this in DOS? That''s the only environment that comes to mind where you could get away with not having a game loop. You could easily halt the program execution while waiting for player input, for instance, but with Windows, you can''t just jam a program... I''d like to see how you handle this part of your game, frankly.

Topic Locked

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

Sign in to reply to this topic.