Is there a way to run modules using the name of the object the controller is attached? Using own() gives an error because own is a kx_gameobject own.name does not work either because its a string object. I ran out of ideas.
Here’s a sample module that would be called from an object named “Cube”.
import bge
import sys
def Cube():
print('in cube')
def main(cont):
own = cont.owner
module = sys.modules[globals()['__name__']]
func = getattr(module, own.name)
func()
Its a little more complicated than I thought it would be. But it works great! Exactly what I needed!
Thanks!
I needed this in my Game Menu for the buttons, so I wouldn’t have to attach a logic brick module to each button. So if I click the button “Options” it will run the module for that button.
Is this the best way to do this?
That sounds like a good way to go about it. I’ve done something similar except I used string properties and a function dictionary.
You could also use exec, but be aware of the security limits
Thanks for help. I think I’ll stick with the first idea since it does everything I need it to do.