Mouse buttons and python variables

Hi i’ve got another python problem. My left mouse button click causes a small script to run which assigns some variables. Later in my script I try to use these variables but I get an error saying they’re not defined. Global didn’t help, though I didn’t expect it to because neither of these are functions, just simple if statements. I wonder if the variables are only assigned while the lmb is pressed but I don’t really know what’s going on.
Here’s what my code looks like:


c = property # a boalean property starts as False 
if leftmouseButtonClicked:
    a = variable 1
    b = variable 2
    c = # boalean changed to True
if c:
    if a = variable:
        do stuff

I know the first bit is working, but in the second part my error says a is not defined. But the second part doesn’t run till after the first bit has, anyone wan’t to shed some light on it. It’s got me kinda stumped. I guess it’s because a is only defined on the run through of the script when the mouse is pressed and after this the variable is lost but I don’t know a good way to set it to stay apart from saving it which I’m no keen to do.

try this:


a = None
b = None
c = property # a boalean property starts as False 
if leftmouseButtonClicked:
    a = variable 1
    b = variable 2
    c = # boalean changed to True
if c:
    if a == variable:
        do stuff

You need to define the variables a and b outside of both if blocks in order for them to be available to both if blocks. Also, you meant == in “if a = variable:”

True i did mean ==, the problem with deffining it outside the block is that then it is reset every time where as what I need is for it to be set, then do a sequence of stuff one bit at a time then it can set C to false again.

Then you need to assign the variable to the globalDict or the object. eg:

# Storing in globalDict:
import GameLogic

if leftmouseButtonClicked:
    GameLogic.globalDict['a'] = variable 1
    GameLogic.globalDict['b'] = variable 2
    GameLogic.globalDict['c'] = True

if GameLogic.globalDict['c']:
    ...

or

# Storing on a object (useful if you are using multiple instances of the script on different objects)
cont = GameLogic.getCurrentController()
own = cont.owner

if leftmouseButtonClicked:
    own['a'] = variable 1
    own['b'] = variable 2
    own['c'] = True

if own['c']:
    ....

(Tip, if you want to toggle a bool you can use: bool = not bool)