Does My Game Need a Main() loop?

I haven’t been using a main loop and it hasn’t been a problem so far. But I’m seeing some other people who are using the main loop in python.

Also, second question - is there any reason I shouldn’t use mostly modules instead of scripts. Because right now almost everything is a module and I haven’t had any problems with that yet.

I create my projects like you are doing right now and haven’t had any problems either. There may be advantages that we’re about to hear from someone else, but there certainly aren’t any problems with what we’re currently doing.

1 Like

Modules are gud.

1 Like

I only use main if say I have an empty object running a python controller: myScript.main and that main takes care of everything like world generation. Otherwise, if I have something like a bullet, I might do bullet.hit . There’s also these things called components which I haven’t used before but I guess they would work good for that situation (running small individual functions of a greater class).

regarding functions vs scripts, I rarely use the script by itself. You could do script if you are running a script that handles all of world generation but i still prefer to do that through the module main because the controller gets passed in automatically so I don’t have to define it or other things. It also just looks neater. I usually only use just the script option if my script has less than say 5 lines like if I am just disabling mipMapping or something like that which doesn’t require cont or own or scene

1 Like

You should not use a ‘main’ loop, but name it for the function it is.

They don’t know that ‘Main’ is just an example to name/define a function.

Modules > script

Script:

  • is just that a script to be used by 1 object, use it on multiple objects and it will fail.

Module:

  • is just that a function with code that get’s executed when that function is called, be it 1 object or on 1000 objects.
  • Modules only their names get loaded/readed at run time, meaning module is lighter/better performance then script.
  • Module got controller already build in, meaning no gpl issues if you only need the cont or owner or scene (no need to import bge module for that) the first argument you set will be assigned to the controller (if no data is received for it)
  • you can simply pass data from 1 function to another function.
  • you can make one script with 100 functions and call them individually from within the python brick and from any object you like.
  • you can simply import modules from one script into an other.

And everything that i forgot to mention.

1 Like

I don’t code games but some operators for blender that I code in some of my add-ons have by accident gained a main loop.
I like to make my add-ons in a modular fashion, some modules handle motion of entities and other handle collision logic and other just have some nasty math functions and everything is controlled by a single secondary loop embed in a main loop by a generator object in a modal operator, so I guess main loops aren’t bad.

1 Like

I guess with “Main() loop” you mean the game loop. (The loop that presents a single frame when running the game).

The BGE already has a game loop. This is the reason why you can simply start the default scene and it works. You do not need to start or maintain the game loop.

Have a look at Prof. Monster's BGE Guide to the GameLoop for details how that works.

You “customize” the game loop by adding logic bricks. While this runs inside the game loop, you describe the behavior within a single logic frame.

You might see people writing functions with the name “main”. There is no restriction nor a convention to use this name. It does not describe the game loop. It is not a loop at all. It is a function that is called within the game loop. In my opinion it is not even a good name for several reasons. One is that is matches the conventions of other systems with a complete different meaning. This can easily lead to confusion. (I guess this is why you ask this question.)

Some people do not like the logic bricks and try to reduce to use them as less as possible. So they use one Python controller (which will be called at each single frame) as “entry point” to run their own logic system from there. It is still not the game loop, but is used for nearly all custom logic.

1 Like

Also I don’t like to use a “main” function because unlike Java, C++, etc, the main function isn’t required. Since you can run any function you want, I always name my functions related to the task they do. For example. player.move or airplane.fly