Change Module of Python controller in Python?

Hello, is it possible to change the module of a Python controller (see picture), with python?


I want to change the module in python when i spawn the object.

Yes, with the cont.script attribute:


def timeslow(cont):
    cont.script = __name__  + '.other_function'

def other_function(cont):
    print("Here")

I really like this trick and use quite a few places…

nice thanks!

I’m not familiar with the module system…is there a place where I can get a quick rundown of it…just a simple intro tutorial…I really have no clue what it is :slight_smile:

if you have a python script like this, lets say the Script is called Player.py


import bge

def somefunction():#do stuff


def otherfunction():#do other stuff


def main():#do smt



you can use the module thing to directly call a function and not run the whole script

Python controller: Player.otherfunction

or Player.main

I’ll just add a comment to Leralasss’s post that it is slightly more complex. In script mode, the script is run separately for each frame and for each object. In module mode, it is run once, and then the function is specifically called each frame/object.
This means you can have persistent variables in the module. For example, if I have the following module:


speed = 0.05

def move(cont):
    cont.owner.worldPosition.x += speed

def increase_speed(cont):
    global speed
    speed += 0.01

If some objects are always running “move” then a completely separate object can run “increase_speed” and it will change the speed of every object using the move function.