Jump to content

[Monogame/XNA] Input lagg when adding enemy objects to list


lurv

Recommended Posts

Game so you can see what i mean: https://puu.sh/sThyV/2cf3ad931a.zip

At Time 40,42,44,46 it adds enemies.

Keep continuously moving during that time and you'll see how it stops.

 

Basically, I have a list of enemy objects and timed events using gameTime.TotalGameTime.TotalSeconds. Every 0.1 seconds it calls upon a method to check for enemy spawns, and if we're at second 2.5 we add an enemy to the enemy list.

Now what happens is that keyboard input lags for a second, for example, if i'm moving continuously to the left, my character will stop for a short period (like 0.1 secs i think) before resuming moving.

I'm using gameTime to check for keyboard input every millisecond, but i've tried not using it too and it still happens. It's also weird because I have code that creates bullet objects every 0.5 seconds if i hold down Z the same way it does with enemies (through adding the object to a list), but that doesn't make it lagg the input

if (elapsedMs > oldElapsedMs + 0.001) //Millisecond update, for movement
        {
            oldElapsedMs = elapsedMs;

            //Keys to be held down.
            newState = Keyboard.GetState();
            Keys[] pressed_Key = Keyboard.GetState().GetPressedKeys();
            for (int i = 0; i < pressed_Key.Length; i++)
            {
                switch (pressed_Key[i])
                {
                    case Keys.Escape:
                        this.Exit();
                        break;
                    case Keys.Left:
                        player.tryMoveLeft();
                        break;
                    case Keys.Right:
                        player.tryMoveRight();
                        break;
                    case Keys.Up:
                        player.tryMoveUp();
                        break;
                    case Keys.Down:
                        player.tryMoveDown();
                        break;

                    default:
                        break;
                }
            }
        }

Spawn check code:

//0.1sec Update, spawning code
        if (elapsedSpawnTime > oldElapsedSpawnTime + 0.1)
        {
            oldElapsedSpawnTime = elapsedSpawnTime;
            spawnTimer += 1; //Every 0.1 sec add one to spawnTimer. The spawn timer int is what stage.Spawn() uses to check for spawns.
            stage.Spawn();


        }

my spawning method in my stage class:

public void Spawn()
    {
        switch (game.spawnTimer) //Each int is 0.1sec
        {
            //10 spawnTimer = 1 Second

            case 40:
            case 42:
            case 44:
            case 46:
                game.enemyList.Add(new Enemy(game.enemySprite, 500, 500, 1, true));
                break;

            default:
                break;
        }
    }

 

Link to comment
Share on other sites

your check for input keys is inside your if statement for time checking, move it out of that. Secondly, I feel like there may be a more effecient way to check for keys down than iterating through an array every frame AND having a switch statement for each of those keys. But the real problem is that that switch statement is in your timed event.

Edit: So basically move your entire input check method outside of this statement:

if (elapsedMs > oldElapsedMs + 0.001) //Millisecond update, for movement {

In addition, I have a few recommendations to clean up this code, but I have to bounce for the time being, if you catch me in the shoutbox later, remind me to make a post on it.

Link to comment
Share on other sites

I've tried moving it out of the millisecond check, the problem till persists :(

The thing with the millisecond check is that I'm simulating deltatime so that it moves you every millisecond instead of every time the program updates since the program updating is faster for some computers than others

Link to comment
Share on other sites

6 minutes ago, lurv said:

I've tried moving it out of the millisecond check, the problem till persists :(

The thing with the millisecond check is that I'm simulating deltatime so that it moves you every millisecond instead of every time the program updates since the program updating is faster for some computers than others


you /SHOULD/ be checking for user input every frame/tick not on a timed event.

Basically right now you're saying
If (timeElapsed > interval) then move.

Link to comment
Share on other sites

Are you sure? Practically everyone has been telling me to make deltatime to make sure the program ticks isn't what checks for movement since the speed varies from pc to pc

Anyway, i moved it out of the if statement. Unfortunately the input stop still happens when i add new enemy objects :(

It doesn't stop when i add new bullet objects though which is weird

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...