Bad script still works XD

cont = GameLogic.getCurrentController()
own = cont.owner

Space = cont.sensors[“Space”]

PR = own[“C”]

if Space.positive:
A = (PR + 1)

own[“C”] = A

print PR

I have a cube with a Space sensor attached to that python script.
The cube has a property called C. Every time I press space, it adds 1 to C.

The script works just like I want it to, but the console says this:

Python script error from controller “cont#CONTR#1”
Traceback (Most recent call last):
File"", line 20, in <module>
NameError: name ‘A’ is not defined

Help a bad coder out please :stuck_out_tongue:

import GameLogic as GL ## Imports GameLogic into a smaller form
Cont = GL.getCurrentController()
Own = cont.owner

Space = Cont.sensors["Space"]

PR = Own["C"] ## Object Property

A = False ## Predefine 'A' just in case the if statement doesn't set A

if Space.positive:
         A = PR + 1 ## You have to use either Tab or 4 Spaces to identify what code is going to be used in this 'if' statement 
Own["C"] = A

print PR

Please use the code tags rather than the quote tags, the code tags preserve the indentation of the code:

Its a bit hard to tell without the indentation, but I think the own["C"] = A line must be indented to match the A = PR + 1 line. Because the variable A is only defined within the if condition, it won't exist outside of the if condition.

Nice XD Thanks a lot dudes :smiley: And yes I’ll definitely remember to use code tags next time.

John Out

EDIT: Hmmm… predefining A as false does not work for me… hmmmm :confused: Also is this in the wrong section? If it is, how do I move it?

EDIT1: I think I figured out the correct way.

cont = GameLogic.getCurrentController()
own = cont.owner

Space = cont.sensors["Space"]

if Space.positive:
    own["C"] = own["C"] - 1

Works like a charm! XD

To save yourself some time when incrementing/decrementing variables, instead of writing the variable name out twice, you can use:

Decrement the C property by 1

own[“C”] -= 1

Increment the C property by 1

own[“C”] += 1


Thanks dude :smiley: That’s a nice trick :smiley: +1 to knowing the limits of Blender, you watching this Social? :evilgrin: I’m gonna make it :stuck_out_tongue:

PS: @Bullfrog: I tested your code and it works :smiley: Just some minor spelling mistakes :stuck_out_tongue: