An all key sensor connected to a script connected to some other objects´ actuators

So I want to make a script that checks if a specific button is pressed and activates the actuator on an other object. So e.g. If I press E then it activates the actuator on E Empty. If I press V then it activates the actuator on the V Empty and so on.How can I do that? Can someone give me an example file from which I can learn this?

or if i could reach the objects from the script without connecting them would work too

I think something like this is what you’re looking for:

ActuatorMotion.blend (450 KB)

Q and E move the left and right cubes respectively.

Simply send messages :wink:

If you need an example look at WALL-E (in my signature). It uses this method to decouple the user input from the controls.

Can’t really navigate through it.

So I just have to name the actuators and not the object itself?

scene = bge.logic.getCurrentScene()

What does that line do? As I see it just sets a variable that won’t be used.

Also I want to trigger the script only once per key presses so holding the button down would just make it run once and if I turn on TAP or turn the Pulse to false it continues to run by itself :confused:

What the hell… No matter what I try it doesn’t work as I want… I don’t have experience in python but I do have some programming background and I just don’t know why my code doesn’t work.

import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()

keyboard = cont.sensors['Keyboard']

if keyboard.getKeyStatus(bge.events.NKEY) == 1:
    cont.activate(cont.actuators['N RIGHT'])
elif keyboard.getKeyStatus(bge.events.NKEY) == 3:
    cont.deactivate(cont.actuators['N RIGHT'])

No matter how I configure the sensor the actuator runs from 3 to infinite times…

if keyboard.getKeyStatus(bge.events.NKEY) == 1:
elif keyboard.getKeyStatus(bge.events.NKEY) == 3:

What does that ==1 and ==3 at the end even mean?..

0 = not active
1 = just pressed
2 = active
3 = released

http://www.tutorialsforblender3d.com/BGE_Python/Sensors/Keyboard_Sensor_Menu.html
http://www.tutorialsforblender3d.com/BGE_Python/Sensors/Keyboard/KeyboardSensor_events.html
http://www.tutorialsforblender3d.com/BGE_Python/Game_Keys/GameKeys_Menu.html

The code works perfectly for me… keyboard.blend (440 KB)

Just a thought. Are you using blender 2.49? The older versions of blender use python2 wheras the new ones use python3 and so scripts for new blender are incompatible with old ones. If you are using blender 2.49, I can recommend getting the latest version (2.68 I think).

I have 2.68a

It works this way for me too but it triggers it until the key is held down. If I turn on the TAP on the sensor and press it once it goes on forever.
I just need it for an instant, to trigger the actuator only once per key press.

Use this script:

import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()

keyboard = cont.sensors[‘Keyboard’]

if keyboard.getKeyStatus(bge.events.NKEY) == 1:
cont.activate(cont.actuators[‘N RIGHT’])
else:
cont.deactivate(cont.actuators[‘N RIGHT’])

This way it wont even trigger the actuator.

What I’m trying to achieve:

if (keypressed == NKEY)
    then
        if (N==False && Le1==False) then trigger actuator once in Object1
    else
        if (E==True && Le1==True) then trigger actuator once in Object2

if (keypressed == EKEY && E==False && Le1==False) then trigger actuator once in Object3

And so on...

I time ago, i try to activate a actuator of other object using a script of other object, using the scene.objects, and not worked, the console not haver errors, but the script not make anything. only works if the actuator is connected to the controller(in logic), the best way is use messages(monster say this)

what are you trying to do? which makes the actuator having those objects you want to activate? you are trying to make it move?

I’m sure it can be done with the same script, without activate the actuator of the other object, sorry english :smiley:

Yeah I’m trying to move them and to change the property of the variables in the main object that starts the script.

You don’t seem to understand how actuators work. When you activate an actuator, it remains active until it is deactivated. If you only want an actuator to remain active for one frame, you need to explicitly deactivate the actuator on the next frame. Carlo’s script does this correctly (though he left out the indentation under the if statements). However, you must also enable True level triggering on the keyboard sensor to ensure the script is called even when the key is still being held down. Otherwise the script is only called when the key is pressed or released.

And yes, you can get rid of the scene variable. I left that in there on accident.

Its easy :smiley:

you need to use the:

bge.logic.getCurrentScene()

So, for get a object use this:
import bge
scene = bge.logic.getCurrentScene()
object = scene.objects[“Name of objects”]

And for make the object make a thing(like make the object move), you need see this: http://www.tutorialsforblender3d.com/BGE_Python/Game_Logic/GameObject_Menu.html

And for get a property of this object:
import bge
scene = bge.logic.getCurrentScene()
object = scene.objects[“Name of objects”]
object[“a Property of the object”] = “Any value”

Sorry english :smiley:

You can make to the scene.objects everything you can do with the cont.owner.

I made this example for learning :smiley:
keyboard objects and properties.blend (93.2 KB)

That’s all nice and good but how do I make it trigger only once and how can I move it’s location and not give it linear velocity?

also the keyboard bug in 2.68 is something to note…

Check, this logic based motion controller out…

it uses a property to control animations, and also you can use

if “property” controlling animation does not equal zero, keypress does nothing,

so this is basically a “state” even though I am not using a state controller.

and here is a more simple logic based motion control (no actions) keypress Rot

Attachments

ReRigForAlphonsB.blend (525 KB)KeypressRot.blend (445 KB)