[SOLVED] Can you call while loops in the game loop?

Started by Grilled, March 13, 2015, 23:44:18

Previous topic - Next topic

Grilled

Hello!

I wanted to know if there's a way to call a while loop in the game loop? Or call a method in which a while loop is in. Whenever I do so my game just stops responding.

Thanks  :-*

abcdef

There is nothing stopping you calling a while loop in a while loop. But until your inner while finishes your outer while loop will behave like it is suspended.

Maybe you need to spawn a new thread and then run the while loop in the new thread

Cornix

I would highly recommend learning more of the core concepts of programming before trying to get into OpenGL. A question like this really belongs to a first semester student programming for the first time. And LWJGL is certainly not something that is easy to learn and use.

Grilled

Quote from: Cornix on March 14, 2015, 10:30:52
I would highly recommend learning more of the core concepts of programming before trying to get into OpenGL. A question like this really belongs to a first semester student programming for the first time. And LWJGL is certainly not something that is easy to learn and use.

I take an AP class in computer science first of all. I think you misread my question. I'm asking if its possible to call a while loop inside the GAME LOOP. Obviously the game loop is a while loop but it acts differently than a regular while loop. I've actually made very good progress in the development of my first game considering there aren't very many tutorials on lwjgl other than a few on youtube and the javadoc. This was just something I wanted to know because anytime I call a while loop say:
while(Player.x<2){ x++; }
(this is just an example) in my game loop, the program doesn't respond.

Kai

I guess your question is about what happens to the rendering and the responsiveness of the window if you occupy the main thread with things such as a busy while loop or other complex computations.

The answer is, that your window does not render anything and also neither responds to user input nor to window messages sent by the OS in between calls to Display.update() (or GLFW.glfwPollEvents and GLFW.glfwSwapBuffers, if you are using LWJGL 3).

Basically, all window message handling (as well as rendering becoming visible) only occurs when Display.update() or the mentioned GLFW methods are invoked.

In principle you can do anything in the main rendering loop but the longer each frame takes, the less responsive your window becomes and the lower your framerate are.

Neoptolemus

If the game becomes unresponsive, then it sounds to me like that while loop is either taking a long time to run, or it never meets the conditions required to end. If your program is single threaded then that means all code outside the loop will cease being executed until its complete.

If you can't optimise the loop, then I suggest splitting the loop into a separate thread, so the renderer can continue operating regardless of what the while loop is doing. The issue then of course is that while the renderer will continue updating as normal, your game loop will then be slowed down or frozen by the loop.

What are you trying to do in the while loop?

Grilled

Quote from: Neoptolemus on March 15, 2015, 06:56:39
If the game becomes unresponsive, then it sounds to me like that while loop is either taking a long time to run, or it never meets the conditions required to end. If your program is single threaded then that means all code outside the loop will cease being executed until its complete.

If you can't optimise the loop, then I suggest splitting the loop into a separate thread, so the renderer can continue operating regardless of what the while loop is doing. The issue then of course is that while the renderer will continue updating as normal, your game loop will then be slowed down or frozen by the loop.

What are you trying to do in the while loop?

Thanks.

I want to be able to press space and a bullet will be drawn and kept drawn (and moving based on the players orientation) for a certain amount of time. I figured I could do this using a while loop.

SHC

I think you didn't understand how games move the objects. The movement won't happen all at once, it will be done in steps of a speed per frame. Define a speed for your bullet, and every frame add that speed to the position of the bullet and draw the bullet at it's position, it will look like the bullet is moving.

Grilled

Quote from: SHC on March 16, 2015, 08:23:30
I think you didn't understand how games move the objects. The movement won't happen all at once, it will be done in steps of a speed per frame. Define a speed for your bullet, and every frame add that speed to the position of the bullet and draw the bullet at it's position, it will look like the bullet is moving.

Right now I have it so that when the space bar is pressed the bullet is drawn and moves from the players postion based on how long the space bar is held for. But I want it to stay drawn and moving regardless of how long the spacebar is held for.