[英]Why is variable not global in BGE Python
I'm trying to make a simple game in Blender Game Engine using Python controller. 我正在尝试使用Python控制器在Blender Game Engine中制作一个简单的游戏。
I have a Python controller attached to an Always sensor on a pulse mode and a game property called 'first' on a Sphere, which is also the controlled object. 我在脉冲模式下将Python控制器附加到Always传感器上,在球体上也将游戏属性称为“ first”,这也是受控对象。
I want to run a few lines of code only for the first time the game runs, just so it defines the variables and it doesn't overwrite them the next time it runs. 我只想在游戏第一次运行时运行几行代码,只是它定义了变量,并且下次运行时不会覆盖它们。
When I run the code below I get an error, that the ForBack
and LeftRight
variables are not defined. 当我在下面运行代码时,出现错误, ForBack
和LeftRight
变量。 However I can access them from within the if statement. 但是我可以从if语句中访问它们。
Am I missing something obvious or what? 我缺少明显的东西吗? Also let me know if I could compact this code. 还请告诉我是否可以压缩此代码。
import bge
from bge import logic
cont = bge.logic.getCurrentController()
player = cont.owner
keyboard = bge.logic.keyboard
scene = logic.getCurrentScene()
objs = scene.objects
ACTIVE = bge.logic.KX_INPUT_ACTIVE
first = objs['Sphere']['first']
if(first == True):
ForBack = 0.0
LeftRight = 0.0
print('1')
print(ForBack)
objs['Sphere']['first'] = False
if (keyboard.events[bge.events.WKEY] == ACTIVE and ForBack < 50):
ForBack += 5
#forward
if (keyboard.events[bge.events.SKEY] == ACTIVE and ForBack > -50):
ForBack -= 5
#backward
if (keyboard.events[bge.events.AKEY] == ACTIVE and LeftRight < 50):
LeftRight += 5
#left
if (keyboard.events[bge.events.DKEY] == ACTIVE and LeftRight > -50):
LeftRight -=5
#right
player.applyTorque((ForBack, LeftRight, 0), False)
They are only defined if your first if statement evaluates to True
. 仅当您的第一个if语句的计算结果为True
才定义它们。 You access every time in all your if statements which are evaluated each time and in player.applyTorque((ForBack, LeftRight, 0), False)
. 您每次都在每次评估的所有if语句中以及player.applyTorque((ForBack, LeftRight, 0), False)
。
You should set the initial value for both outside the first if statement. 您应该在第一个if语句之外为这两者设置初始值。
ForBack = 0.0
LeftRight = 0.0
if first:
........
What is happening is pretty clear: 发生的事情很清楚:
if(first == True): # evaluates to false first time
ForBack = 0.0
LeftRight = 0.0
print('1')
print(ForBack)
objs['Sphere']['first'] = False
# then you get here and ForBack is not defined
if (keyboard.events[bge.events.WKEY] == ACTIVE and ForBack < 50):
^^^^^^
If you only want the others evaluate if the initial if statement is True you need to nest the if's inside the initial if. 如果只希望其他人评估初始if语句是否为True,则需要将if嵌套在初始if中。
if first == True:
ForBack = 0.0
LeftRight = 0.0
print('1')
print(ForBack)
objs['Sphere']['first'] = False
if (keyboard.events[bge.events.WKEY] == ACTIVE and ForBack < 50):
ForBack += 5
#forward
if (keyboard.events[bge.events.SKEY] == ACTIVE and ForBack > -50):
ForBack -= 5
#backward
if (keyboard.events[bge.events.AKEY] == ACTIVE and LeftRight < 50):
LeftRight += 5
#left
if (keyboard.events[bge.events.DKEY] == ACTIVE and LeftRight > -50):
LeftRight -=5
#right
player.applyTorque((ForBack, LeftRight, 0), False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.