gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Two Functions at Once?

#64205 - dexter0 - Tue Dec 20, 2005 6:08 am

Is there anyway to have my code call a function and while that function is running, call another function and have them both run at the same time?

#64207 - tepples - Tue Dec 20, 2005 6:16 am

What, specifically, do the two functions do?

Do you mean coroutines, where two functions run, passing data back and forth?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#64208 - dexter0 - Tue Dec 20, 2005 6:22 am

The two functions are controlling two different sprites on the screen. Each function controls one of the two sprites.

#64211 - poslundc - Tue Dec 20, 2005 6:47 am

Why would they have to run at the same time?

Dan.

#64229 - Fatnickc - Tue Dec 20, 2005 12:43 pm

You can have the effect that they are running at the same time quite easily.
Simply replace all whiles with ifs and the like, then use this:
Code:

while(a<b)
{
    function1();
    function2();
}

#64264 - sajiimori - Tue Dec 20, 2005 7:19 pm

I use coroutines extensively, allocating a stack for each of them. That's a pretty high price to pay in terms of memory, but I can always "bake" them into finite state machines if it's getting too expensive.

Edit: Then again, it was probably more of a beginner question, which would make coroutines a little out of the ballpark...

#64307 - tepples - Wed Dec 21, 2005 5:14 am

In that case, Fatnickc's answer is the right one.

Imagine each game object taking a turn. Each turn it stays still or moves a pixel or two. Then imagine the turns going so fast that each sprite gets a turn 60 times a second. All objects move a bit, and then the program draws them in their new positions, rinse and repeat until Game Over. This is what a typical game loop looks like.
Code:
while(game_state != STATE_GAMEOVER)
{
  move_player();
  move_player_out_of_walls();
  move_enemies();
  move_enemies_out_of_walls();
  move_projectiles();
  game_state = handle_object_collisions();
  draw_all_objects();
  VBlankIntrWait();
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#68488 - dusda - Wed Jan 25, 2006 12:08 am

tepples wrote:
In that case, Fatnickc's answer is the right one.

Imagine each game object taking a turn. Each turn it stays still or moves a pixel or two. Then imagine the turns going so fast that each sprite gets a turn 60 times a second. All objects move a bit, and then the program draws them in their new positions, rinse and repeat until Game Over. This is what a typical game loop looks like.
Code:
while(game_state != STATE_GAMEOVER)
{
  move_player();
  move_player_out_of_walls();
  move_enemies();
  move_enemies_out_of_walls();
  move_projectiles();
  game_state = handle_object_collisions();
  draw_all_objects();
  VBlankIntrWait();
}


That is a very good explanation of how games work :D.