I have a python script that defines all the scenes, and other scripts import it. Since scenes are added after the scripts, I get a load of errors telling me that the scenes are not defined on startup. This is because the scripts are executed, but the scenes are not yet added nor defined. In fact, I get about 4 errors before the scenes are defined. I have a python script that I want to run before anything else. It adds and defines all the scenes. Is there anyway to execute a script before anything else is executed? Thanks.
While Micheal_Z's answer is directly answering your question I think your description shows you have a different issue.
- You can't run anything before the first scene is loaded.
- You can't process logic on a scene that is not loaded (yet).
What is your "script" (you import it , so it is a module) doing?
From your explanation I guess it contains the names of scenes you want to load. This makes sense as the BGE does not provide names of loadable scenes (just of loaded scenes).
You do not really need that, as you know beforehand what scenes to load (and when). Typically you would do this with a scene actuator (one for each scene).
When you do it dynamically with Python, you need these names. I guess this is your current situation.
I suggest to implement a scene loader that loads the scenes in one or several steps.
To solve your processing errors I suggest to avoid trying to access scenes that are not loaded yet. Either by checking if the scene exist of by running the logic AFTER the scenes where loaded.
Binding Logic to scene
This is very simple ... place the logic into the according scene. It will never run without the scene as it is tightly couple to the scene. Btw. it makes sense to bundle the logic of a scene to exactly that scene.
The checking (if you really want to decouple scenes from their logic) is simple too. Do a check if the scene (still/already) exist:
or scene = getScene("Scene") if scene:
(Yes, you need to implement sceneExists()/getScene() by yourself)
I hope it helps.
There is a checkbox in the Python controller which seems to be suitable for your needs. It's tooltip says "Mark controller for execution before all non-marked controllers (good for startup scripts)". See screenshot:
Since you did not mention that you already tried this - you may want to check this option on the controller carrying your scene-defining script.