Question about the Game loop

Started by Grilled, February 08, 2015, 05:11:57

Previous topic - Next topic

Grilled

Hello once again!

I have a question about the Game loop ( while(!isCloseRequested) );

Ok so basically I have an enemy draw method as shown here:
public void drawEnemy(){
		
		enemyBrain();
		
  
		
		glPushMatrix();
		glTranslated(x, y,0);
		glBegin(GL_QUADS);
		glColor3f(0.9f, 0.3f, 0.2f);
		glVertex2d(-8,0);
		glVertex2d(8,0);
		glVertex2d(8,16);
        glVertex2d(-8, 16);
	
	    glEnd();
	    
		glPopMatrix();

	
		glLoadIdentity();
			
			

	}


If I call that method in my game loop, it only creates one enemy.
Now if i were to call that method in another method, say:
public static void Update() {
		timeSinceLastSpawn += Delta();
		
		if(timeSinceLastSpawn > spawnTime) {
			spawnEnemies();
			timeSinceLastSpawn = 0;
		for(Enemy enemy: enemies){
			enemy.Update();
			enemy.drawEnemy(); //method specified above
			
			}
		}
	}


It creates multiple enemies in random locations( as specified in the creation of the new enemy object  ).
Would you happen to know why?

Thanks! :-*

abcdef

Erm because you told it to? A draw command command is a draw command, doesn't matter where you call it from

broumbroum

Quote from: Grilled on February 08, 2015, 05:11:57
...
Ok so basically I have an enemy draw method as shown here:
public void drawEnemy(){
		
		enemyBrain();

		glPushMatrix();
		glTranslated(x, y,0);
		glBegin(GL_QUADS);
	...vertices
	    glEnd();
	    
		glPopMatrix();

	        glLoadIdentity();
			
			

	}

...;	for(Enemy enemy: enemies){
			enemy.Update();
			enemy.drawEnemy(); //method specified above
			}
	}


It creates multiple enemies in random locations

Hi Grilled (bear ^^), I'm about to say your code is not really showing properly what can help me to understand your problem. Why ?
Here is something to check and correct : to translate the "world mesh", with a global variable x,y is not a good idea, it'll bug in anytime, you should pass it through the method arguments drawnemy(x,y);
I think your Enemies comes from a class that may behave like if they were so designed for. E.g. they have been positioning at a certain location in the "world mesh" and need to move (in Update()). Please tell me more about the enemy.Update() function.. For sure there is the mistake in...

Grilled

The enemy's update function just checks if the new Enemy is the first enemy in the wave and if it is then it will set boolean first to false. Otherwise it adds the x to the time delta and multiplies that by the velocity of the Enemy which is a final float of 0.5.

Shown here:

public static void Update() {	
	 	if (first)
	 		first=false;
	 	else
	 		 x+=Delta()*VEL;
	}

broumbroum

I think there's a bug in this update() fnt from the Enemy class. Your instance relies on a Delta() fnt of time, both to respawn Enemies and move them... That may be unaccurate if you are not sure WHEN and WHERE it is called.You said the Update() function wasn't in the game loop, but in another thread, was it?
Here I show you what I mean, it's shorter :
public static void Update() {
            if(enemies.size() == 0) // it'll run once if its empty
                   spawn(enemies); // populate your List and location randomly
            synchronized(enemies) {// it'll run many times
            for(Enemy enemy: enemies){
                 enemy.x+=Delta()*enemy.VEL; // We all access from the member variables
                 draw(enemy);
           }
     }
 }

It's a pattern of designing with real time-thread synchronization, too difficult for explaining it here. I suggest you to simplify your Update() method in a way that it relies on time for movement (like delta * VEL) only and try something easy for respawn,...

Look what if there were just a random number of enemies in one game instance, but appearing on the same time at different places ?  8)

Grilled

Thank you very much for your replies!

I tried the code mentioned but when I ran it, the enemy didn't even spawn.

You said something about the enemy's movement relying on the time Delta but it in fact does not. The only method in the Enemy class that even calls delta() is the update method, and if i were to take that out, nothing really changes. :-[

Grilled

Soon after I wrote that I actually figured out the problem. Basically the for-each loop I had would infinitely add enemies to my list. So I made a numEnemies state variable and passed spawnEnemies() numEnemies as a parameter and had a for loop act off of the number of enemies specified, which in turn allowed me to effectively control the amount of new Enemy(float x, float y)'s spawned.

Thank you for Your help! :o