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 “enemyScript.py”, 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
enemy.SendMessage(“enemyDamage”);
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:
>>>>enemy.GetScript(enemyScript).enemyDamage()
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)
enemy.enemyDamage()
If it’s just a function in a module, you can access it by importing the module.
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: