Can you 'call' a script function to run on another object directly with Python?

Hello everyone!

I’ve just got a quick question. Is it possible to call a function on another object directly with Python? For instance, if an object has a script called “”, and this script has a function called “enemyDamage()”, can I call that function? I’ve been able to do this in Unity, and it looks something like this:

How it works in Unity (JavaScript):

for functions, you just send a message to the object in Unity, and it will search the script for the function


for individual variables, like health

enemy.GetComponent(enemyScript).health -= 1;

I’ll provide an example below of what I’m trying to do below:

import bge
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()

enemy = scene.objects [“enemy”]
def main():

>>>># The type of thing I’m looking for:


Thanks everyone!

It depends on how you’ve defined enemyDamage(). If it’s a bound method (i.e. it’s a method belonging to a class), you can just call it from the object instance (which is the enemy object itself if the class wraps a KX_GameObject)


If it’s just a function in a module, you can access it by importing the module.

import mymodule

Functions are not bound to the object, nor scripts are.

You can configure a script or an entry point function of a module via Python controller. It does not matter if that is used at another object.

You can import functions from other modules (scripts are no modules!) via import statement as posted by Mobious. This is a Python feature, not a BGE feature.

To achieve the thinks you are looking for why not write a damage handler that processes the damage for you:

def receiveDamage(object, damage):
   armory = getArmory(object)
   effectiveDamage = damage * 100/(100+armory)
   health = getHealth(object)
   remainingHealth = min(0, health - effectiveDamage )
   setHealth(object, remainingHealth )

getArmory, getHealth, setHealth can be implemented in any way you like.
e.g. via property

def getArmory(object):
   return object.get("armory", 0.0)

I hope it helps

Thank you very much! I actually ended up doing what you did from what Mobious told me, but both of your comments helped me and were appreciated! :slight_smile:

However, you can mutate the GameObject to inherit from your own class, and consequently call functions and methods on that object.

Is that like extending the gameObject class to have new functions or properties that it didn’t have originally ?

Essentially, yes.