Multiple game loops?

Blitz3D Forums/Blitz3D Beginners Area/Multiple game loops?

CGV(Posted 2003) [#1]
Hi to everyone, this is my first post.

I got Blitz+ about a month ago and have been studying the sample programs and all and then I jumped right in and started writing a game but, because I didn't have any real concept of what the game would be beyond it's general genre of puzzle\logic\line game, I've run into an issue with my general program design.

I'm using a finite state machine to control my main loop but when certain events occur, instead of handling them with code within the main loop, I jump to functions with their own independant timing loops and all so I've essentially got several main loops in my game. These functions take anywhere from a fraction of a second to about 3 seconds to complete so my main loop is frozen while this is going on.

Obviously this would be unacceptable in an event driven Windows app, but do I need to worry about this in a full screen (Blitz2D type) program, or can you pretty much do whatever you like in full screen programs?

Thanks for any replies
-=CGV=-


ford escort(Posted 2003) [#2]
if i understand correctly you have some functions that don't return each frame to the main loop freezing it


you can use a kind of flag to keep track of the active function then jump each frame to this function until it's complete

for exemple when you have to jump to a function, set your flag to something

actualy your code may look iike that

mainloop
code
code
code
if condition:function1():endif
code
code
code
/mainloop

function1()
loop
code
code
code
/loop until condition
return



------------------

you can make it look like that
mainloop
code
code
code
if condition:flag=1:endif
select flag
case 1
function1()
end select
code
code
code
/mainloop

function1()
code
code
code
if condition
flag=0
endif
return

so the program still execute your main loop and jump every frame to your procedure until the same condition occurs ...

i admit that it's a difficult and looking weird way of code stuff :)


Ross C(Posted 2003) [#3]
Hey, welcome. As above plus your function should only realy update things once per loop. Could you give me an idea of what your function is that is taking up to 3 seconds to perform?


CGV(Posted 2003) [#4]
Hi, thanks for the replies

I know how to write my code so it all runs in a single loop, I just want to know if could get away without doing so.

The game is sort of a 'Bejeweled' clone.
When you line up 3 or more chips, they get removed from the board which is where I jump to a function which handles the removal. Because it's all animated it takes about 1 second to perform.

I also gave the game a bomb chip. If you remove a line of chips containing a bomb, then the whole row and column where the bomb was located explodes taking out all those chips. The function which handles this takes about 3 seconds to perform all the animation.

When each of these functions gets called it becomes the main loop taking over the job of clearing the backbuffer, redrawing it and flipping it, so instead of running for one frame and then returning, it keeps running until the animation is complete and then returns. My main loop doesn't really do anything besides update the mouse cursor and wait for the users input.

I guess it's the type of game that makes this possible since after you make a move you just sit and watch the results, then you make your next move and so on. If it were an arcade game requiring continuous user input it would never work doing it this way.

It works, I just wonder if the Blitz compiler could end up getting confused if I put in one too many loops, although there's never more than one loop running inside another.

I guess what I was fishing for was an absolute:
"NO!!!, you'll crash everyone's computer and bring down the whole internet doing it that way!!!"

:)
Regards,


Ross C(Posted 2003) [#5]
Nah, your alright if it's an event that the user has no input to. The compliler should be fine with that, no reason not to. :)


WolRon(Posted 2003) [#6]
I heard a while back that you can nest loops somewhere in the thousands if I remember right. I think the only limit is the size of the stack your program is using. For the most part, you should never have to worry about loops within loops.


CGV(Posted 2003) [#7]
UPDATE:

Well, it turns out you can't have more than one loop unless your game is really simple.

I tried adding a scoring feature today (the points you scored float their way up the screen) and there was no way to make it work due to my multiple loops so now I have to rewrite everything to run within a single loop.

Live and learn.

Regards,


WolRon(Posted 2003) [#8]
Not true.

You can make your score floating function (hint, hint) as a FUNCTION.

And just call it from either loop if necessary.


CGV(Posted 2003) [#9]
Yes, it is a function but I've got each loop running at a different speed as each animation has it's own requirements so the floating score keeps changing speed depending on which loop I'm in.

I'm sure it can be done but it needlessly complicates things. I only ended up with multiple loops because I sat down and just started coding with no plan or design.

Regards,