nand script error

What is wrong with my script?I am getting this error.

Python script error - object 'Cube', controller 'Python':
Traceback (most recent call last):
  File "For.py", line 15, in <module>
AttributeError: 'SCA_NANDController' object has no attribute 'sensor'
Blender Game Engine Finished

Here is my script and a picture of my logic brick setup?

import bge

 
python_controller = bge.logic.getCurrentController()

cube = python_controller.owner

nand_controller = cube.controllers["Nand_10"]

sen_list = nand_controller.sensors
sen = nand_controller.sensor["Forward"]
sen = nand_controller.sensor["Backward"]  
sen_activated = sen.negative


Controller and GameObject sensors are accessed from the sensors attribute. However, you shouldn’t check the state of sensors of other controllers.

It seams you fixed your code already. Controllers indeed have no attribute “sensor”. They have an attribute “sensors” (plural). This is a mistake I often do as well as I’m focused too much on a single sensor.

Beside of that … what agoose77 wrote. Your controller should not care other controllers. It is out of context.

You could use it for debugging or utilities (e.g. to find out what controller runs what Python code). But I suggest to avoid that in production environment.

So you mean get rid of sen_list = nand_controller.sensors. What do you mean actually?Is it an error in the blender game engine or is it my error?

No, the error you showed to us meant you had something like this:


... nand_controller.sensor

This would result in


AttributeError: 'SCA_NANDController' object has no attribute 'sensor'

The code you posted does not have this syntax (and it does not have 15 lines of code). Therefore it looks correct.

Edit: The other is more a design issue that most-likely results in hard-to-identify problems later…

Actually there are eight lines exluding the blankline.Here is the error i am getting.

File "For.py", line 7, in <module>
AttributeError: 'SCA_NANDController' object has no attribute 'sensor'
Blender Game Engine Finished

As I wrote above, it’s not .sensor, it’s .sensors.

lol. I was looking at that line:


sen_list = nand_controller.sensors

and missed that one


sen = nand_controller.sensor["Backward"]  

why are you storing the sensors in a variable without using it? :wink:

I got the code from Blender 3D and Game Engine Tutorials.I modified it.How would you do it correctly?

ad an “s” :wink:

Code is correct if it does what you want. But I do not know what you want. Therefore you get only answers to your syntax problem.

I want the cube to move when i am not pressing the x keyboard key and move when i press x keyboard key.That is why i am using the nand controller in the python code.

You did not configure <x> ;).

Did you mean: activate motion when forward and backward are both not positive?

In that case better use the NOR controller.

[table=“class: outer_border, align: center”]

[th]forward[/th]
[th]backward[/th]
[th]OR[/th]
[th]NOR (Not OR)[/th]
[th]AND[/th]
[th]NAND (Not AND)[/th]

not positive
not positive
deactivate
activate
deactivate
activate


not positive
positive
activate
deactivate
deactivate
activate


positive
not positive
activate
deactivate
deactivate
activate


positive
positive
activate
deactivate
activate
deactivate

[/table]
(This happens when at least one of the sensors triggers, otherwise there are no signals towards the actuators)