• 11
• 9
• 10
• 9
• 11

# Lag

This topic is 999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I made a simple little game where you have to guess a card, and the problem i have is that after i press the button to chose the card, it takes about 2 seconds or even more to update the card and show if i was wrong or right. I am using LibGDX and here is my code:


package gameScreens;

import main.mainGame;

public class playScreen implements Screen {
private OrthographicCamera cam;
private SpriteBatch batch;
private BitmapFont font;
private ShapeRenderer shape;
private int choosenNumber = 0;
private int mod = 0;
private int computerNumber = 0;
private int lastCompNumber = 0;
private String currentMod;

public playScreen(){
cam = new OrthographicCamera();
cam.setToOrtho(false, mainGame.getWidth(), mainGame.getHeight());
batch = new SpriteBatch();
font = new BitmapFont();
shape = new ShapeRenderer();
}

@Override
public void show() {

}

public void modPast(){
if(computerNumber == 0 ){
computerNumber = MathUtils.random(1,5);
while(lastCompNumber == computerNumber)computerNumber = MathUtils.random(1,5);
System.out.println(computerNumber);
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)){
choosenNumber = 1;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){
choosenNumber = 2;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_3)){
choosenNumber = 3;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_4)){
choosenNumber = 4;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_5)){
choosenNumber = 5;
}
if(mod == 1){
if(choosenNumber > 0){
if(computerNumber == choosenNumber){

System.out.println("Win!");
lastCompNumber = computerNumber;
computerNumber = 0;
choosenNumber = 0;
}
else if(computerNumber != choosenNumber){
System.out.println("Lost!");
lastCompNumber = computerNumber;
computerNumber = 0;
choosenNumber = 0;
}
}
}

}

public void modFuture(){

if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)){
choosenNumber = 1;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){
choosenNumber = 2;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_3)){
choosenNumber = 3;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_4)){
choosenNumber = 4;
}
if(choosenNumber == 0 && Gdx.input.isKeyJustPressed(Input.Keys.NUM_5)){
choosenNumber = 5;
}
if(choosenNumber > 0){
computerNumber = MathUtils.random(1,5);
while(lastCompNumber == computerNumber)computerNumber = MathUtils.random(1,5);
System.out.println(computerNumber);
}
if(mod == 0){
if(choosenNumber > 0){
if(computerNumber == choosenNumber){

System.out.println("Win!");
lastCompNumber = computerNumber;
computerNumber = 0;
choosenNumber = 0;
}
else if(computerNumber != choosenNumber){
System.out.println("Lost!");
lastCompNumber = computerNumber;
computerNumber = 0;
choosenNumber = 0;
}
}

}

}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

cam.update();
batch.setProjectionMatrix(cam.combined);

shape.begin(ShapeType.Filled);
//  shape.setColor(Color.RED);
// shape.rect(200, 10, 600, 200);
//  shape.rect(200 - 70, 50, 140, 250);
//  shape.rect(350 - 70, 50, 140, 250);
//   shape.rect(500 - 70, 50, 140, 250);
//    shape.rect(650 - 70, 50, 140, 250);
//    shape.rect(800 - 70, 50, 140, 250);
//   shape.rect(200 - 70, 50, 140, 250);
//   shape.setColor(Color.BLUE);
//    shape.rect(mainGame.getWidth()/2 - (170/2), mainGame.getHeight()/2 + 20, 170, 270);
shape.end();

batch.begin();
font.setColor(Color.BLACK);
font.draw(batch, "1", 200, 50);
font.draw(batch, "2", 350, 50);
font.draw(batch, "3", 500, 50);
font.draw(batch, "4", 650, 50);
font.draw(batch, "5", 800, 50);
font.draw(batch, "6", mainGame.getWidth()/2, mainGame.getHeight()/2);
font.draw(batch, "Mod: " + currentMod, 10, mainGame.getHeight() - 10);

switch(lastCompNumber){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
/*	font.draw(batch, "Use SPACEBAR to change mods.", mainGame.getWidth()/2, 60, 0, Align.center, false);
font.draw(batch, show, mainGame.getWidth()/2, 100, 0, Align.center, false);

if(mod == 0){
font.draw(batch, "Mod: Future", mainGame.getWidth()/2, 40, 0, Align.center, false);
font.draw(batch, "The computer will let you pick an image from the 5 that are available, then \n the computer will pick a random one and compare it to yours.", mainGame.getWidth()/2, 150, 0, Align.center, false);
}
if(mod == 1){
font.draw(batch, "Mod: Past", mainGame.getWidth()/2, 40, 0, Align.center, false);
font.draw(batch, "The computer will first chose a random image from 5 \n available, then its your turn to pick one of them.", mainGame.getWidth()/2, 150, 0, Align.center, false);
}*/

batch.end();

if(mod == 0){
currentMod = "Future";
modFuture();

}else if(mod == 1){
currentMod = "Past";
modPast();

}
if(mod == 0 && Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
mod = 1;
}else if(mod == 1 && Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
mod = 0;
}

}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}

}



I also wanna point out that it might be the computer that i use atm, but i cannot test it with my personal computer because its summer and i spend the summer at my grandparents. I am using a pretty crappy computer i can say. I suppose game development can wait until i get back home, but i wished i could practice while away too. :/

Also, can i avoid getting crappy fps by using just textures instead of sprites?

Edited by KenWill

##### Share on other sites

This sounds like an excellent opportunity to work on your skills with a debugger. Try putting a breakpoint in the code that does your update, then run the game. Trigger an update, and step through the code. You may be surprised to find where it's spending all of its time.

Well, i have no idea how to work with the debug yet (amazing skills right here) but i'll do some research on that and try it out. Thanks. :3

##### Share on other sites

This sounds like an excellent opportunity to work on your skills with a debugger. Try putting a breakpoint in the code that does your update, then run the game. Trigger an update, and step through the code. You may be surprised to find where it's spending all of its time.

So lets say i put a breakpoint just after the first set of sprites is drawn to the screen, how do i know how long it took to load them or anything like that?

Reading my code again i have seen some problems but only small ones (like checking what "mod" its active in the mod method that would only run if the mod its active, but i don't think that would give me such a bug. I think its about the way i load and render all those sprites on the screen or something...i don't know but i'll try to figure it out.

Also haven't tried to see if its about processing or just a bug in the code. I shall try to watch my fps at the same time, and see what comes up.

Edited by KenWill

##### Share on other sites
This is an even better job for a profiler than a debugger.

My usual advice to newer coders is to make use of both a debugger and a profiler as much a part of your studies as writing the code itself.

(I never use Java, but maybe this one will suite your needs?)

##### Share on other sites

2 seconds doesn't sound to me like a load time issue, not for the size and number of sprites you're working with. It sounds more like a problem with the way the code is doing its work.

I should have been clearer originally: what happens if you set your breakpoint and then use the Step functions of your debugger to move slowly and deliberately through the code? A few things to look for:

• Loops that you have to click through way more times than you would expect
• Code that takes a long time to "Step over" compared to other code around it
• Anything that feels like it pauses or hiccups when you step past it
This isn't a highly scientific approach, mind you, but it should at least help narrow down what's going on. If my initial guess is wrong and this is more of an accumulation of small issues, a profiler might be in order.

I have another problem...when i introduce a breakpoint the game will not respond upon starting.

EDIT:

So, um, seems like without the sprites the game works smoothly but when i render the sprites the fps just goes down and all the code its affected, not just one bit. I...don't know why this is happening. I mean this computer isn't that good but i can run games with nice graphics with it. I use to play on it strategy games like Total War Medieval 2, Darkest Hour and other games like Red Orchestra: Ostfront 41-45 so i don't think loading 10 sprites its such a heavy task for this computer...I really wanna test this code when i get home to see if it runs the same on my good computer. .-.

Edited by KenWill

##### Share on other sites

HELP!

No one is actually going to help me? >.>

##### Share on other sites

I have another problem...when i introduce a breakpoint the game will not respond upon starting.

It is unclear what you mean by that. I suspect your IDE is currently waiting for your further input since it reached the breakpoint and halted the program before anything was visible but with such limited information it is difficult to be sure.

Apart from that, the advices above regarding debugger and profiler still stand. Even without those a strategic placement of suitable System.out.println()s could be used to detect where exactly the slowdown happens.

##### Share on other sites

You are expecting some behavior (that the program flow goes a certain way, that it processes certain data in certain way, that it takes an expected amount of time).

But since you are not getting the expected behavior, somethings obviously not going like you expect.

So, you need to figure out where specifically things are going wrong. Theres a few things you can do:

• Read through the code to make sure theres no clear mistakes in logic
• Make sure you understand how the tools you use work and check you arent using them wrong (eg that youre using the libGDX functions correctly and know what they do, how they do it, and approximately how expensive they are in terms of time and memory)
• Measure the program flow/execution time/data being processed to make sure everything is like you expect (utilize debuggers, profilers, or simple print statements to get this information)

Others have suggested using the debugger to step through the code after hitting a breakpoint (this means you keep pressing a button and the IDE will execute the code one step at a time - measuring the execution flow and to some degree the time each step takes to run) and using a profiler (this will tell you how much time is spent in each function, perhaps allowing you to identify the location of the 2 second slowdown).

Learn how to do those things, since theyll be extremely helpful in the future.

If you dont want to learn those tools right now, you can find a function to print the current time, and use that to measure the time spent in a block of code (measure current time before and after block of code, print the difference). If you have a good idea of where the issue might be, it should not take long to identify the exact code at fault.

If you get stuck try changing approach.

Edited by Waterlimon