what is a Tick????

Hi everyone.

Well iI was wandering what a tick process is I mean I have the source code for minicraft that notch gave to everyone and I was reading his code to see if I can grasp some of the techniques he uses when programing and I came about the tick function, and didn’t quite understand it… can anyone illustrate me what a tick is supposed to do??? I don’t need the knowledge of what notch was trying to acomplish I just wan’t to understand what a tick is…

A “tick” is just updating the game logic.

Rendering is updating the stuff shown on screen.

-Nathan

So each time a call a tick function is to update my game logic?
And so I use the Tick function to create my game logic right?

I would look at this; it’ll probably help.

In my own words:

There a few different ways you can have your game cycle through it’s updates. You can have a generic while loop that updates the game logic and renders on the same cycle, like so -

while (running)
{
	updateGameLogic();
	render();
}

This is a horrible way to do it. When you do it like this, it is only rendering when it is ticking, which will make the game feel like crap on a slower computer.

Here is a good way to do it: seperate the rendering and ticking out into different methods, and only let it tick after a certain amount of time.

while (running)
		{	
         long lastTime = System.nanoTime();
			
			while (playing)
			{
				long now = System.nanoTime();
				unprocessed += (now - lastTime) / nsPerTick;
				lastTime = now;
				boolean shouldRender = true;
			
				while (unprocessed >= 1)
				{
					tick();
					unprocessed -= 1;
					shouldRender = true;
				}
				
				Thread.yield();
			
				if (shouldRender)
				{
					frames++;
       				render();
				}
			
				if (System.currentTimeMillis() - lastTimer1 > 1000)
				{
					lastTimer1 += 1000;
					frames = 0;
					ticks = 0;
				}
			}
		}

THAT ^^ is how you should do it. It won’t fix stuttering on machines that can’t render as fast (the fps won’t be as fast) but it will tick as often, since updating the game logic requires few resources.

Hope this helps.

-Nathan