Main problem

import bge
cont = bge.logic.getCurrentController() 
obj = cont.owner

sens = cont.sensors["Mole"]
actuator = cont.actuators["Enemy"]

if sens.positive:
        cont.activate('Enemy')
else:
        cont.deactivate('Enemy')
main()


Why is main not defined? I am using the same code with a steering actuator instead of a motion actuator.

You need to define main first.

Def main():
    <i>do stuff</i>

main()

Or if main is defined in some other module:


from myModule import main

main()

Your script does not represent a function, so you are asking the script to loop a function that does not exist , you tell him to call somthing that does not exists. Def main () at the start , and main () at the end to run the code between def main() and main()

File "st cube.py", line 6, in &lt;module&gt;
NameError: name 'cont' is not defined
Python script error - object 'Cube.002', controller 'And':
Traceback (most recent call last):
import bge
def main():
cont = bge.logic.getCurrentController() 
obj = cont.owner

sens = cont.sensors["Mole"]
actuator = cont.actuators["Enemy"]

if sens.positive:
        cont.activate('Enemy')
else:
        cont.deactivate('Enemy')
main()


How do you fix this name error?

you didnt import logic i think. Try this


import bge
from bge import logic
cont = logic.getCurrentController() 
obj = cont.owner

sens = cont.sensors["Mole"]
actuator = cont.actuators["Enemy"]

if sens.positive:
        cont.activate(Enemy)
else:
        cont.deactivate(Enemy)



It looks like you forgot to indent the body of code following def main():
Remember, the body of a function must always be indented.

In general avoid the name “main” it is a meaningless word and does not describe anything. Better use a descriptive word.

If you running your code as script, you do not need a separate function. When reading you need to jump to the bottom of the code to see that you have to jump back to top. This is really irritating especially there is no visible reason why you have to do so.

If you running your code as module, you do not need to call BGE callable functions from module level.

Have a look at the BGE Guide to Python coding.