Wow, awfully long Subject there… Anyways, my situation is I have a few complex functions I’m going to be calling over and over again from different scripts, so I figure it’d be faster and easier to store them in GameLogic rather than define them each time a script is run.
The problem is I can’t seem to use any imported modules in a function stored within GameLogic. A simplified example:
This script is run at startup to add the functions to GameLogic:
from math import sqrt def double(x): return x*2 def root(x): return sqrt(x) GameLogic.double = double GameLogic.root = root
…and this script actually calls the functions:
x = 16.0 print GameLogic.double(x) #--- To be sure the functions are working print GameLogic.root(x)
but running it gives me the following output:
32.0 PYTHON SCRIPT ERROR: Traceback (most recent call last): File "Call", line 6, in ? File "Define", line 5, in root NameError: global name 'sqrt' is not defined
It can’t find the square root function I imported. I’ve tried importing it in the script that calls the function and even tried importing it within the function before “return sqrt(x)”. No dice. Can anyone tell me if it’s possible to use imported modules in functions like this? Thanks!