• ### Announcements

#### Archived

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

# Input and combat loop

## Recommended Posts

Black_Star    122
My game has a while combat loop. The player and the monster both attack once every time the loop is run. I use the sleep() function to pause the combat loop. No input is possible in it and I want to change it. here is code:


while(cCrea.hp!=0 && cChar.hp!=0)   //combatloop, continue until player or monster is dead

{
Sleep(1100);
cout<<endl;
waarden.calc=rand() % 19+1;                //generate a number between 1 and 20, 20 is always a hit, 1 is always a miss.

switch(waarden.calc)
{
case 1:  break;
case 20: playerdamage();          //calculate damage player has done to monster

break;
default: calcplayertohit();   //calculate if player hits the monster

break;
}

if(cCrea.hp==0) {break;}                //is monster dead, abort loop

Sleep(1100);                         // wait 800 miliseconds

waarden.calc=rand() % 19+1;
switch(waarden.calc)
{
case 1:  break;
case 20: creaturedamage();         //calculate damage monster has done to player

break;
default: calccreaturetohit();      //calculate if monster hits the player

break;
}
monsterstatus();
cout<<endl;
cout<<"hp: "<<cChar.hp<<" mp: "<<cChar.mp<<" >";

}

Now I want to be able to give the user the possibility to give input during combat so that he can flee, look at monster, use a skill or a spell ect. Does anyone know a way to do this? If the player doesnt want to input something the loop has to continue.

##### Share on other sites
Landsknecht    234
What is the purpose of the sleep call? Just to slow the loop down a bit?

I would kill the sleep call altogether and just use a timer call like GetTickCount or something. Use a variable to store the timer value and just use that to figure out if enough time has gone by. Here is some psuedo-code...

    timeValue = GetTickCountloop{   tmpTime = GetTickCount;   if((tmpTime - timeValue) > 800) // Or whatever delay you want   {      timeValue = tmpTime;      DoCombatCrap();   }}//end loop

That way, the loop will continue on and on and not stall out like using the sleep. If you wanted to you could put some logic in the beggining to grab keypresses or such so you could run, use items, etc...

Hope that makes some sense
Landsknecht

[edited by - landsknecht on March 19, 2002 5:12:33 AM]

##### Share on other sites
Black_Star    122
Ive got GetTickCount(); working but how should I get the input. It pauses when I use cin.

##### Share on other sites
ktuluorion    393
quote:
Original post by Black_Star
Ive got GetTickCount(); working but how should I get the input. It pauses when I use cin.

I''m not going to give you a lengthy description on well-documented things, but I CAN point you in the correct direction: Research DirectInput. It will do what you want, and is actually quite easy in comparison to other DirectX components.

You really shouldn''t use standard C functions in most cases for game programming.

Another reason you may want to look into DI is you may want to have support for other types of controllers, such as joystick or mouse.

Good luck!

Mike

##### Share on other sites
Landsknecht    234
Also, something else to think about... GetTickCount is actually pretty cheesy. The resolution sucks. I pointed it out but it is very easy to understand. If you wanted to do it right, you should proly use a high performance timer and wrap it in a class. But that may be more than you wanna deal with.

As far as the input goes, Direct Input is a snap. But if you want a hack method - look into the GetAsynchKeyState macro. That does not pause and it simply does an immediate check to see if a key is up or down.

Hope that helps.
Landsknecht