Declaring Variables

Hi,
I can’t figure out how to declare variables without assigning them a value.

If i try this:

import bge
cont = bge.logic.getCurrentController()

key = cont.sensors[“Keyboard”]

if key.positive == True:
test += 1

It gives me the error: name “test” is not defined

If i try this:

import bge
cont = bge.logic.getCurrentController()

key = cont.sensors[“Keyboard”]

test = 0
if key.positive == True:
test += 1

It assigns the value 0 to the variable everytime the script is run (every frame)

Then i tried this:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

key = cont.sensors[“Keyboard”]

if “init” not in own:
test= 0

if key.positive == True:
test += 1

But it gives me the same error.

So, is there a way to declare a variable without assigning to that a value?
Thanks a lot

So, you want to keep a variable in existance between frames?
You’r last example is half way there:


import bge
cont = bge.logic.getCurrentController()
own = cont.owner

key = cont.sensors['Keyboard']

if not 'count' in own:
    own['count'] = 0 

if key.positive:
    own['count']+=1

yes, i knew this method, but if my variable is not an integer but an array or an object?

Same method applies. You can initiate it in the gameobject by using


if not 'x' in object:
    object['x'] = datatype

or, an extension of this:


import bge

class instanceClass:
    def __init__(self):
	    self.x = 1

def main(cont):
	own = cont.owner
    if not 'instance' in own:
	    own['instance'] = instanceClass()
	
	#after the code above, if there isn't a registered class already
	#it will be created on the gameobject.
	
	#now just use the handle (own['instance']) and modify its attribute (in this case x)
	
	print(own['instance'].x) #get the value of x in the instance
	own['instance'].x = 2 #set the value of x in the instance

Wow, thanks a lot agoose :smiley:

So, you want to keep a variable in existance between frames?
You’r last example is half way there:

Is there a similar way to do this with globalDict’s

Yes, there is.


import bge

cont = bge.logic.getCurrentController()
own = cont.owner

if not "my_key" in bge.logic.globalDict:
    bge.logic.globalDict['my_key'] = 0

if cont.sensors[0].positive:
    bge.logic.globalDict['my_key'] += 1


GlobalDict is a dictionary, just like the way you store variables on an object. You can treat them the same :slight_smile:

Just your our clarification:

A variable lives as long as any reference to the variable exist.
If you reference the variable in a function it will live as long as the function is executed. When leaving the function the variable will die.

The script mode is like a function. After leaving the script all variables die. A variable can survive if you keep a reference somewhere else (module, property etc.). The variable will die with the death of the last reference to it.

Example:


foo = ["demo","list"] # birth of foo
print (foo, id(foo) )
foo.pop()
foo.pop() # remove existing values
foo.extend(["list","with","new","values"]) # add new values
print (foo, id(foo) )
foo = ["other","list"] # foo is dead, long lives foo
print (foo, id(foo) )
# the second foo dies here too