make a python script/module wait for other script/module?

is it possible to have a script/module stop mid code to wait for another script/module to complete?


answer: can’t be done

Hm? Why don’t you just run the code inside the script you want to “wait”.

Also, depends on what you mean by complete…
Do you mean that some property/object/attribute changes and that completes your script? Or is it simply just running a script once?

Usually you achieve this by not running the script when the rest of the set is not yet ready. Check for property and only run your functions if the properties are there. For example check from globalDict (or some other object) if it has been initiated. Then set own initiated to True and allow running your own code.

if not "init" in own:
    if "init" in bge.logic.globalDict:
        own["init"] = True
        own["init"] = False

if own["init"]:

But indeed if you need a lot of mechanics like this it indicates you are coding things from the wrong angle and you perhaps should consider merging some of your scripts.

Yes, it is possible.
No, it will not work.
No, it should not do that.

The Python controller is part of the game loop. The game loop is single threaded. The whole game will wait until the Python controller completed it’s execution. If you make the code “wait” for something, the game will wait too. In other words: The game will stop.

If the event the code is waiting for is part of the game … it will never happen as … the game stopped. So it must be an outside event.
Even than it is not a good idea to wait, because … the game stops and the game experience will be disturbed by lags.

Just now I would say you have a little design issue. Why should one part of the code wait for the other one? What is the purpose of that?

As you see waiting is not an option as the game should remain running.
What you need is a way to evaluate if the code you are waiting for is done or not. So you can use this as input for the other dependent logic.

e.g. The character controller tells the motion controller to move to waypoint X. The motion controller notifies the character controller when its work is done. The character controller performs according actions based on this input.
(Alternatively the character controller can constantly check if the motion controller finished, but this is pretty inefficient compared to the notification method).

I’m using a “flood-fill” algorithm to create squares on a grid. the squares have an “always” sensor that triggers the algorithm once they are created. they spread like this:

I need them to use a stack based system so that they spread like this:

it’s very important that they spread in this stack based way. is there any kind of stack system I could use?

I think I might need to know exactly what you are trying to accomplish but can the “other” module you need to run just be a function?

def main():
    do stuff here


    continue doing stuff

def function():
    other stuff

I’m having trouble trying to wrap my head around what you are attempting.

It’s easiest to register all tiles and then run a manager. The act of registration is a one time process and will save any further evaluation. If you tag objects rather than have then register themselves you will also save processing time on logic bricks

I figured out a work around using sensors controllers and actuators as my stack.

You can include a pattern to spread to tiles around the current tile.

Distance check will probably be too slow in this resolution so you should make a number grid and find the neighbors based on that. For a grid sized x * y the neighbors are (x-1,y) (x+1,y) (x,y+1) (x, y-1). Build a list of your grid in the beginning, store it and get the next tiles to fill (if they aren’t already filled or missing altogether) using recursive function.

Ask if you don’t get some part of this :slight_smile:

As mentioned in your other thread already - I suggest to go away to use the 3D world as your game model.

I suggest to follow the MVC design pattern as this will match your requirements much better.

The Model will reside in Python. This allows you the freedom to choose an efficient design towards your algorithms. For example a 2 dimensional array, or a list/dict of occupied positions. This way you can have very simple and fast access methods to each coordinate you want without fiddling around in 3D space.


myObject = getObjectAt(10, 100)

To make the model state visible in the BGE and to add interaction with the user you create a View and a Controller layer which are specific the BGE. The user input will be processed by the controller part (e.g. setting a startpoint, endpoint, corner point).

The output will be processed by the viewer part (creating cubes/planes, updating the objects with the current state of the model).

The advantages are:

  • You can replace any of those layers without affecting the others (as long as you do not modify the interfaces between them). Example: you do not like the 2D array … replace it with a lookup solution … no change in controller or view necessary.
  • You are free to use the model outside of the BGE (you could even run it in Blender ;)). All you need to do is create a new view and a new controller.

You can read about this method by looking for the MVC model.