Search code examples
screeps

Why can't I have a main loop


I've always written programs such that I have a main loop:

initialiseStuff();
while(1){
    doRepetitiveStuff();
}

But it seems that screeps will give me a "console not responding" error if I attempt this.

Why does it do this? I'm guessing that the game waits for the end of your script before doing something perhaps?

How can I achieve what I want (do some methods, set variables, only once) when the entire main is repeated every tick?


Solution

  • How can I achieve what I want (do some methods, set variables, only once) when the entire main is repeated every tick?

    Basically, you can't execute something only once. The game engine runs that infinite loop on its own, you can't get out of it.

    It is important to understand, that Screeps is a multiplayer online game. Your and other players' scripts are being executed server-side. That means you could simply break the game engine process with such infinite loops as in your example.

    Also, due to distributed and scalable server architecture, your script could run on different machines along different game ticks. Thus, it doesn't make any sense to make an initial script setup, as it won't be shared between multiple separated machines' runtime environment.

    See more details on how the game loop works in this article: Understanding game loop, time and ticks.