PDA

View Full Version : A Question About GameLoops



steadi
1st December 2012, 20:06
Hello There Guys,

I am currently trying to create a game using Qt and it's rather nice graphics view item. I have the game pretty much setup but I would like to add in a game loop to make the npc's wonder around and such.

My current code is:


while (1)
{
update();
}


but this just crashes the game. I know a while loop works when low-level programming but it does not seem to work in this instance. Can somebody point me in the right direction on how to create a game loop or a better way around using the while loop in Qt?

Thanks in Advance,
Matt :P

amleto
1st December 2012, 20:22
QThread

(and don't sub class it). You could use a QTimer instead of while(true). Don't use while(1) - it's just not classy and stinks of 'C'. You can use while(true) with no problems other than hogging cpu. If this causes a crash, then you have a problem elsewhere...

steadi
1st December 2012, 20:47
I have tried while(true) and using a QThread but it all seems to yield the same result. I have checked over the rest of my code and nothing is out of place or anything that should cause it to crash - any suggestions?

amleto
1st December 2012, 22:50
read my sig.

wysota
2nd December 2012, 00:35
Hello There Guys,

I am currently trying to create a game using Qt and it's rather nice graphics view item. I have the game pretty much setup but I would like to add in a game loop to make the npc's wonder around and such.

My current code is:


while (1)
{
update();
}


but this just crashes the game. I know a while loop works when low-level programming but it does not seem to work in this instance. Can somebody point me in the right direction on how to create a game loop or a better way around using the while loop in Qt?
Instead of a while loop use a timer with timeout interval matching the game speed you want to obtain. Also please take care of avoiding naming your methods the same as already existing methods in Qt.