Variables in python

Can I accsess a variable from any object from any script? Something like myval =

Yeah. You have to get the object first with
scene = GameLogic.getCurrentScene()
obj = scene.getObjectList()[“OBCube”]

Remember to put the OB in there, that’s how Blender names things.
Then you get the property just like you said

myval =

Perfect. Thank you. I was hoping it would be that simple.

And are the keys labeled as standard ASCII codes?

I forgot to get the scene in that code and edited.

What do you mean by keys? Oh, keyboard keys.

No. You can import a key name thing called

import GameKeys

Then the keys are named “AKEY” etc.

Edit: sorry, just reread. I think they are labled as ASCII values, but it’s in a list or something. You can just have it tell you by
print sensor.getCurrentlyPressedKeys()
I think that’s it. Then check the value.

Now i checked the value of the getcurrentlypressedkey and printed it. I have a response like [123, 1] This second number changes even if its the same key. What does this mean?

(thanks for the edit)

It’s probably a true/false thing, or positive/negative.
To just get the first number you use getCurrentlyPressedKeys()[0]


Get a list of currently pressed keys that have either been pressed, or just released

list of key status. [[keycode, status]]

Thank you for taking the time to answer my somewhat…simple questions fireside. :smiley:

Got another one: :slight_smile:

When I try to get the number from the array it tells me it is an unscriptable object:

key = sensor.getCurrentlyPressedKeys()[0]


key = sensor.getCurrentlyPressedKeys()[1]

Both let me know it is unscriptable.

Type error unscriptable object

ooooo I think I see whats going on here

its returning several keys at once inside another array

[[127,0] , [168, 3]] and so on…

So I need to seperate them accordingly.

Still getting the same error though… :-?


I just tried it, yeah. Unsubscriptable object.
Not sure how to do that right off.

Oh. It was trying to print when there was no object in the list.

You have to use

list = sensor.getCurrentlyPressedKeys()
if list != None:
  print list[0][0]

There are two functions to get the key states from a keyboard sensor. getPressedKeys() and getCurrentlyPressedKeys() they are similar, but there’s a subtle difference.

getPressedKeys() returns keys that have been pressed or released on that frame. A key status of 1 means it was pressed, and 3 means it was released.

getCurrentlyPressedKeys() returns all keys that are being pressed. A key status of 1 means it was pressed that frame, and 2 means it was held from a previous frame.