Question about python variables

Hi all, this is going to sound like a rather stupid question.
I have always been confused in python with variables.
If you are running a script on True Pulse, and define a variable such as bob

bob = “name”

if parts of the script change the variable bob, but you call bob = “name” at the start of the script, when the script is called again (via a pulse) is the variable (bob) reset to the starting definer?

e.g


bob = "name"

if 1==1:
     bob = "me"

Yes. Execution will start at the top of the script each time it is called. So if bob is declared and initialised to “name” at the top of the script each time the script is called bob will equal “name” and any changes to the value of bob that happen during the scripts execution will be lost when the script is called again.

Python variables declared in a script are local to that script and I think they’re destroyed when the script ends. If you want to keep changes made to a value use properties. Also with properties there are methods you can use so they’re only initialised once so changes can be saved rather than them being reset on each call.

Hope this helps! :smiley:

If it is a script the variable is reset to “name” every time You run.

If You use a module, You have to define a function to call in that module. The stuff ‘outside’ the function is only run once, when the module loads (before the first time You call the function). So if You define a variable in the module and change it in the function it will remember the change the next time the function run.

NOTE: You have to declare the variable global in the function to change the ‘outside’ variable.

You can also create properties in scripts by using obj[‘property’] = value. That will create an object property if it doesn’t exist, and will set it to the value specified.

I think here is a big misunderstanding:

When the controller finished executing the script all python code dies.
This including any data that is not referenced elsewhere (e.g. in a property or as attribute on GameLogic).

When a controller runs the same script again new variables, new functions and new classes will be created.

Specific to the above code:
The variable “bob” simply does not exist before the statement bob=“name” regardless how often the script is executed.
If you execute the code a second time a new variable “bob” will be created when executing bob=“name”.

You can assume the code acts like being the code block of a function:


def script():
  script code here

Everything you create in “script code here” is local and will get destroyed when leaving the function (except there is a reference at an object the survives.

I hope it helps for your understanding

Thanks everyone, yes i do know object variables, global variables etc. However, i was always unsure how true pulse called a script (whether it destroyed and recalled the session, or if it re-read the script (saving variables) from the top.

Scripts are run from the top every time then discarded when they finish.

Modules are compiled when the game begins and can improve script performance. As said above, everything outside functions in a module gets executed only once, so you can define initial values outside the functions to set them up at the start of the game. The rest of the module’s functions are just run when they are called from a Module controller.

If I have complex stuff to set up at the start of a script (that is always the same on each script run), I place it at the top of a module. This way, the complex stuff is only done once. The module might only have one function “main” with a couple of lines of Python. Only this couple of lines are executed each time the controller is triggered instead of repeating the same complex calculations every time.

olá pessoal, sou novo aqui, mais gostaria de saber como faço pra adicionar eixos em um script de carro comum, um carro possui 4 rodas 2 eixos como faço pra adicionar mais 2 rodas mais um eixo? grato a quem responder.

Ok, eu não falo português então isso pode parecer estranho.
Este não é o lugar certo para postar sua pergunta. Criar uma lista de discussão.

mas não é uma discussão sobre python? por que que iria criar uma nova sala de discussão? eu não falo muito bem inglês por isso uso navegador com tradução e isso não é problema nenhum, acredito que o idioma não é uma barreira a muito tempo. e vocês são mais atenciosos do que os blenderianos Brasileiros as perguntas demoram semanas pra serem respondidas.

ah, bem, se você usa o Google Translate, e iniciar uma discussão chamado “veículo questão Wrapper” que irá receber a devida atenção.

One thing… Scripts/modules are called both on positive and negative pulse.

Script are text blobs that get executed (I think You can actually change the script from in game).

Modules get imported just like any module.