How to have a global vector that can be changed in python scriot

I have a global vector that needs to be updated in my Python script for the game engine.

I have tried using the init procedure but I get a error that vector used without being initialized.
if ‘init’ not in own:
own[‘init’] = 1
vect = Vector((1.0,0.0.0.))

vect = vect*transform <— get error here

I got around this problem by declaring 2 game properties one for the X value and one for the Y value since am operating in a 2-D system. But there’s gotta be a better way to do this.

You are missing a comma in the Vector declaration

vect = Vector((1.0,0.0.0.)) =&gt; vect = Vector((1.0, 0.0, 0.0))

what do you want to create?

aligning to a vector?
own.alignAxisToVect(vector, upaxis, rotationspeed)

your object actually needs to have the game property “init”.

or you could not need to with:

if "init" in owner:

your object actually needs to have the game property “init”.

of course not.

he is doing it the right way, if ‘init’ not in own. Then he sets the init property. so what he is doing is correct.
script does not find init so it executes the if statement, then init gets created and next frame it will pass the statement.

the “init” property makes not much sense.

There are better options:
A) call your initialization separately (with a separate Python controller). This isolates initialization code from game business code.


print("I'm an initialization script and initialize your game logic. I have a lot of things to initialize. Call me before doing business. But call me just once!")


print("I'm the business code. Call anytime after calling calling the initialization script.")

  • look if the property you are looking for is present (when you need to create or assign a new object when not present)


if not "vector" in owner:
    vector = owner["vector"]
else:
    vector = mathutils.Vector((0.0, 1.0, 2.0))
    owner["vector"] = vector

  • or you already have a fallback value ready (no need to create it each time you call this code):

from myVectors import VECTOR_Z

vector = owner.get("vector", VECTOR_Z)

It depends on the situation (as usual).

Wie geht’s Monster
I think I like your second idea. What I’m trying to do is, I have an orientation vector for my character, I want initialize it base on its initial position and then afterwards continually updated it based on input from a joystick. Ii would assume that i need to perform this transformation in else loop of your example
if not “vector” in owner:
vector = owner[“vector”]
vector = [0.0,1.0,0.0]
else:
vector = transform(vector)