# How can I execute a function from a separate script?

I have two objects and two python scripts: each object has a python script. Both python scripts contain a basic function that is different from the other script's function. This is all simple. However, since I have the two functions in separate python scripts, I cannot execute them as I do not know how to do so.

I have tried using the messaging mechanic from the Game Engine itself, but that is too slow, as it takes at least one tick. I would like a faster way, if possible.

How can I do this?

You can just import the second file from the first and then call it's functions.

Imagine you have two scripts, foo and bar, stored in foo.py and bar.py, respectively; the script bar has a function which you want to call from the script foo. All you need to dois import bar at the start of foo and then call it's function, as in this example:

#File bar.py
def test():
print("Hello, World!")

#File foo.py
import bar

bar.test() #Outputs "Hello, World!" to the console

• Be careful, you can only import modules (and packages), not scripts. Modules can be executed as scripts. Calling the same module as script and as modules can have unwanted side-effects. I strongly suggest to use module files as modules only. Never directly execute it via Python controller in a Script mode. Either use Module mode or use a separate pure script to import the module. – Monster Dec 21 '15 at 5:58