Help with code

Hi, I’m busy trying to make a script for my game that allows the player to type in stuff so that it changes the value of a property. For instance so that they might be able to customise their name in an RPG etc.

I’ve made this script to try and change the value of the property. When I run it the keys are recognised properly but they fail to change the value of the property. :expressionless:


 import GameLogic as GL
k = GL.getCurrentController()
keyb = k.getSensor("Keyboard")
key = keyb.getCurrentlyPressedKeys()
kee = str(key)
string = k.getOwner().Text
set = k.getActuator("Set")
p = ""

if keyb.isPositive():
	type = int(kee[2] + kee[3]) 
	if type<=33:
		code = kee[2] + kee[3] + kee[4]
		if code == "100":
			p = "d"
		if code == "101":
			p = "e"
		if code == "102":
			p = "f"
		if code == "103":
			p = "g"
		if code == "104":
			p = "h"
		if code == "105":
			p = "i"
		if code == "106":
			p = "j"
		if code == "107":
			p = "k"
		if code == "108":
			p = "l"
		if code == "109":
			p = "m"
		if code == "110":
			p = "n"
		if code == "111":
			p = "o"
		if code == "112":
			p = "p"
		if code == "113":
			p = "q"
		if code == "114":
			p = "r"
		if code == "115":
			p = "s"
		if code == "116":
			p = "t"
		if code == "117":
			p = "u"
		if code == "118":
			p = "v"
		if code == "119":
			p = "w"
		if code == "120":
			p = "x"
		if code == "121":
			p = "y"
		if code == "122":
			p = "z"
		if code == "32":
			p = " "
		
	else:
		code = kee[2] + kee[3]
		if code == "97":
			p = "a"

		if code == "98":
			p = "b"
				
		if code == "99":
			p = "c"
	m = str(p)
	word = (string + m)
	set.setValue(word)
	GL.addActiveActuator(set,1)


Basically I took the value of the currently pressed key. (which looks something like [[101], [1]]) and used the first number to correlate with a key. I then used the pressed key to change the value of the string but somehow that didn’t work. The string’s balue changes to the “Unexpected Caharcters after expression” error with ‘[’ as the unexpected character. Please help a n00b in need. Thanks. :smiley:

did you search? oh well. maby a post i did will help… Somone made an example blend on the bottom of the page: https://blenderartists.org/forum/viewtopic.php?t=30314&highlight=

Maybe I’m missing something, but part of what your doing there looks very round-about. A property can be changed by owner.property = “something”
You don’t need an actuator. You should only need owner.text = word, if it’s property is named text.

Thanks fireside! That was all I needed. It works perfectly now. As my name implies I’m quite a n00b so I didn’t realise you could change a property with Python (I tried but I did it all wrong).

Here are the last few lines of the working script.


m = str(p)
	word = (string + m)
	K.Text = word

Glad I could help. I got a chuckle out of your name. We’re all newbs at something or else we’re not learning anything. I’ve coded off and on, but am pretty new to Blender. This is the best game development tool I’ve ever come across or heard of by far.