Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a problem with my RPG.

I want enemies to wander randomly. So I put:

enx_speed = random.randint(-2, 2) 
eny_speed = random.randint(-2, 2) 

in the main loop (while true:). However, that loops every 20 milliseconds (it's what I use to redraw, find key presses, etc).

How can I have two infinite loops running at the same time with different loop times?

share|improve this question
    
I see your main loop, but where is this other loop you are talking about? Why do you need two loops? –  UnderscoreZero May 2 '13 at 20:39
    
Theoretically you could use multi-threading to have multiple infinite loops run in parallel, but in this situation you really should not do this. Multithreaded programming is difficult and full of pitfalls. Unless you have to solve performance issues, you should stay away from it. I recommend the answer by Fizzik. –  Philipp May 3 '13 at 8:35
add comment

3 Answers 3

Don't know python, but here is a basic arbitrary-language example.

You should only have one game loop that updates all other entities. You can determine how much time has passed between each running of the loop and pass that information on to the entities who can use this information. For Example:

main() {
    lastSystemTime = currentSystemTime;
    while (true) {
         //Time passed since last loop in milliseconds
         int delta = currentSystemTime - lastSystemTime;
         lastSystemTime = currentSystemTime;

         //Loop through all interested parties and update them with time passed
         for all sub-entities {
             entity.update(delta);
         }
    }
}

Then in the entity

constant int WAIT_TIME = 200; //200 milliseconds to wait after actions performed
int timer = 0; //How much time to wait before performing actions

update(int delta) {
    if (timer <= 0) {
        //Perform the actions, then set the wait timer.
        doStuff();
        timer += WAIT_TIME; // Add the wait time to the timer
    }
    else {
        timer -= delta; //Decrease current time to wait by how much time has passed
    }
}
share|improve this answer
add comment

You only need one infinite loop running very fast to do what you're after. Inside the loop you calculate time elapsed since last run. If it's greater than X, then you call function1. If it's greater than Y, then you call function2.

share|improve this answer
add comment
up vote -1 down vote accepted

I managed to solve this myself and the method I used was probably not the best but it was really really easy.

First I define "enloop" outside of the main loop.

enloop = 0 

Then inside the loop every time the loop runs:

if enloop < 10:
    enloop = enloop + 1
else:
enloop = 0

And later also in the loop:

if enloop == 10:
    enx_speed = random.randint(-2, 2) 
    eny_speed = random.randint(-2, 2)

Thanks for responding and let me know if this is wrong somehow.

share|improve this answer
1  
It's only wrong in that the frequency of execution will be determined by CPU speed. Like many old DOS games. Fizzik's answer above gives an example of a proper delta-time based solution that will perform consistently across all* system specs. * colloquial "all", exceptions may still apply. Even though they shouldn't. –  LLL79 Dec 6 '13 at 16:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.