script does not work why

Why does not my script not work? I did it right.

import bge

def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner
    sens = cont.sensors['sas']
    actu = cont.actuators['ending']
    if sens.positive:
        cont.activate(actu)
    else:
        cont.deactivate(actu)
main()

http://www.pasteall.org/blend/36411

There’s lots wrong with this blend. But we can fix it:

  • You are using python module mode, but there’s no module named “gamelogic_module.main” as you’ve written in the python controller. Your script is called “gamelogic_logic.simple” which won’t work. Rename it game.py and update the text in the game controller box.
  • You have a sensor called “Radar” but in your script you call it “sas” rename your script to show “radar = cont.sensors[‘radar’]”
  • You have no actuators at all so you can’t call them. Add an actuator and call it “ending” (use the name box on the actuator to set the name). Attach it to the controller.
  • When using module mode you can add cont as a variable of main, such as main(cont): you don’t have to define it in the script.
  • There’s almost never any need to deactivate an actuator. So you don’t need the cont.deactivate part.
  • When using module mode you don’t need to/mustn’t call functions in the main body of the code. If you do it’ll run twice. Maybe you should start with script mode. it’s easier to understand.
  • It might be a good idea to define your sensors and actuators, not to mention your main function with more useful descriptive names…

Now you script looks like this:

import bge

def ender(cont):

    own = cont.owner

    radar = cont.sensors['radar']
    ending = cont.actuators['ending']

    if radar.positive:
        cont.activate(ending)

And here’s the blend:
fixed.blend (404 KB)

I guess the bge does exactly what you told the bge to do.

What are you expecting this code should do?
What effects are you expecting when this code gets executed?
Have you looked at the console?

Here is what it says in the console.

Blender Game Engine Started
Python module can’t be imported - object ‘Cube’, controller ‘Python’:
ImportError: No module named ‘gamelogic_module’
Blender Game Engine Finished


Attachments



I would say … you have “No module named ‘gamelogic_module’”.

Do you think you have a module with that name? Maybe it is a spelling error.

Did you look at the pictures?You need to click on to see them .

Your images do not show any modules.

The images show (at least partly) that you try to call a module named with “gamelogic_module”

You have written a wrong name in the controller brick.

Okay, here is the blend.Take a look at it.

http://www.pasteall.org/blend/36414

Your module ( python file ) name is "gamelogic_logic.simple"Bad name style, should be without the . “gamelogic_logic_simple”

In python Controller brick you are referencing “gamelogic_module.main” which should be formated like module.funtionName, but you dont have a function named “main” and a module named “gamelogic_module”.

I used the script templates that come with blender.So they must be wrong or outdated.

Hi, I edited my post after someone else posted, here I’ve detailed all the problems one by one. and posted a fixed version of the blend file.

Sorry for my noobiness.But why does not the script work?


import bge

def ender(cont):
    own = cont.owner
    radar = cont.sensors['radar']
    ending = cont.actuators['ending']
    if radar.negative:
        cont.activate(ending)

Thakyou for your help. I did not know you could do it that way.I am still a noob programming.

if not randar.positive:

In this specific example, adrainsnetlis is correct. However, I think it’s also a good idea to look at why you thought that would work.
It seems like you’re writing code that you’ve made up, without referencing any documentation. It would be great to have such an ability when writing code, but unfortunately due to the fact that everyone has their own way of writing things, including APIs, I have to refer to documentation when looking for things that I’ve never used before.

In addition, code doesn’t just “not work”, it will (in the case of Python) throw an error message, to indicate something happened that wasn’t supposed to. There are rare occasions when the code is legal but doesn’t do what you expect, because you’ve asked it to do something slightly differently.

So, in this example, to work out why it doesn’t work.

This is the error message in the Python Console. To show the console on Windows, go to Window/Toggle System Console (in Blender). For Linux / MacOSX, launch blender from the terminal, or run the embedded console app.

This error says the following:

Python Script Error - object “Cube”, controller “Python”

Which means that the controller called “Python” on the “Cube” object was the one that failed

File “… .py”, line 7, in ender

Which means that the Python script called t.py was the script that failed, at line 7, which is in a function called “ender”.

AttributeError: “KX_RadarSensor” object has no attribute “negative”.

This means that the Python object* for the Radar sensor does not have an “attribute” (anything after a full-stop character, like sensor.positive, cont.owner, bge.logic) called “negative”. It does, however, have a positive attribute!

*NOT GameObject; In Python, every “thing” is an “object”. In this case, the radar sensor logic brick is, in Python, a KX_RadarSensor object (instance)

Most of the time, these error messages can be looked at by yourself, with no one else to help you, and solved. It will make you a better coder, and get things solved quicker.

The Python documentation is used to tell developers what API functionality is available, what it is called and where to find it. The documentation for the latest release of Blender is found here. You can also find the link by going to Help/Python API Reference (in Blender).
The BGE documentation is found in the Game Engine Modules section of the page. To find out what is available for things like sensors, you must look in the Types section (bge.types module), because all BGE “things” (sensors, objects, meshes) have a Python class/type that has attributes.

E.g the RadarSensor

Some types inherit from other types, which is indicated by the parenthesis after the type name (In this example, note the “(KX_NearSensor)” written after “KX_RadarSensor”. This means that the KX_NearSensor object is a parent of the KX_RadarSensor object. The KX_NearSensor object itself has a parent (and so on). Eventually, because the RadarSensor is a sensor, it inherits from SCA_ISensor, which doesn’t have a negative attribute.

Of course, because Sensor.positive is a Boolean object, you can get the opposite result (if it is negative), by writing “not” in front of the result.

Well, you did rename them, so it’s not a surprise it’s different :wink:

No i did not.

How would i get the cube to end when the radar does not detect anything and also get the cube to stay when the radar detects something?How would i do that in python?That was what i wanted to happen.There is no nand equivalent in blender game engine api.

You can simple use a NAND or NOR controller.