Python problem always sensor

Last night I was making a really cool py script and I came to a point where I couldn’t get any further. Here’s what happens: I can’t use any methods off of the Always sensor. Heres a bit of code to describe this: (just a section, I have everything else in the script I need)

sens1 = cont.getSensor("sens1")
print dir(sens1)

when executed this returns the following in the console:


So I can’t use any methods of sensor. Example:

sens1 = cont.getSensor("sens1")


sens1 = cont.getSensor("sens1")
freq = sens1.getFrequency()
print freq

returns “attribute error.” :frowning: This is very frustrating because I had a really cool script that I’m sure would work if I was able to use the Always sensor methods. Please help!

why? what can the always sensor do you can’t do with properties in your script?

The object that is my player runs a separate PY script that records its velocity, converts it to an absolute value interger and makes it a property of the second object. Now in the second object, I want to do something like this - emitter.vel is the velocity property (int) -

emitter = cont.getOwner()
sensor = cont.getSensor("sensor")
vel = emitter.vel

Again, this returns “attribute error @ setFrequency” even if I type 5 or any other number instead of “vel”

This object has an edit object add object actuator. I want the frequency of adding objects to be equal/proportional to the velocity of my player, the VEL property. The only way I can think of doing this is by changing the frequency of the always sensor in a PY script. Is there another way? Why isnt the script working?


don’t always trust dir() and try to use methods setFrequency() and getFrequency(). They work.

nope, they always return “attribute error.” Even if I change it to something like this


It still says “attribute error.” Whats going on here?

Not sure I’m following everything there, but the frequency of an always sensor is every game pulse. That’s why it’s an always sensor. If you want to slow it down to not every pulse, you’ll have to use some kind of filter. Like every other pulse, every third pulse, whatever, and then call the actuator. You may also consider using a timer and firing the pulse according to a time interval.

Try following script in controller hooked on Always sensor with frequency set to 25:

c = GameLogic.getCurrentController()
s = c.getSensors()[0]
x = s.getFrequency()
if x > 0: x -= 1
print x

On my Blender 2.41 it prints decreasing numbers with raising speed of print.

Yes, works for me too. Interesting. That might come in handy. I love reading these posts because people do things I wouldn’t think about doing. It seems like it would have strange consequences, though. Like, if the frequency was low and you wanted to turn it up, there would be a delay until the sensor fired before you could change it. I wish always had a property so I could turn it off, though.

I found the problem. I was using old documentation (.25 i think) and I was using “getPosFrequency” and “setPosFrequency” instead of “getFrequency.” I wish they would keep the python API the same…its enough to drive one insane…but anyway, its fixed and working perfectly now. Its a really cool script - wait until I finish my FPS and you can see it yourself!

Well you should really keep track of which version you’re using and which documentation you read :slight_smile: 2.25 was like 2 years ago, and it was right before they changed to open source as well. After that it has been stable, except for a few new things here and there that are added.

Good luck with the game, and don’t hesitate to ask any more questions.