Python question

I’m working on a script and I was wondering if a idea of mine would work. The idea is for the script to run through and assign the variables a value only once, then after that any value assigned to them will be kept.

# property is a sensor detecting if a certain property is equal to 0

# checks to see if w is being pressed

# assign will hopefully change the property to 1

if property.isPositive:
     a = 0
     b = 0
     c = 0
if w.isPositive:
     b = 5

assign.propName = "prop"
assign.Value = 1

so in this example would b still equal five even after the script re-loops, or would b just equal nothing? Also something tells me that I butchered using the property functions write and that the property wouldn’t change to one.

Also the script that’s in this post is just something quick that I made up to get my idea across.

The script does not reloop, but gets executed everytime the controller gets activated.
That means a,b, and c are script local variables. They die after exiting from the script. (the same to every other variable in your script.

When executing the script the next time new variables a,b,c are created. They do not have any relationship with any previous script excution.

If you want to keep data outside of the current script run, you have to store the data in variables that are not local to the script.

That can be:

  • an object property

  • cont.owner[“prop”] = value

  • value = cont.owner[“prop”]

  • an attribute to any other object (GameLogic, scene etc.)

  • GameLogic.myAttribute = value

  • value = GameLogic.myAttribute

  • an already existing attribute at an object

  • GameLogic.globalDict[“key”] = value

  • value = GameLogic.globalDict[“key”]

  • maybe more
    All this variables exist as long the object they belong to exist. E.g. a scene exist as long the scene is running. When restarting the scene, the current one dies and a new one is created.

I hope this helps