Sharing Variables Between Scripts and Bricks

I don’t understand how to make global variables that I can access across multiple scripts. What I want to do is mainly use logic bricks for code, but then to use scripts in order to perform statistical formulas for my gameplay.

Example: I want my character’s global stats such as “Player Agility” to be copied into a formula within a script so that the global variable “Player Agility” will act as a modifier of several other variables stored within the formulas in the script. Such that a player with higher Agility may end up with bonus in other stats based on math.

So that the script would say something like:

Attack Damage = “GetWeaponData”/“Get Player Strength” * “Player Agility”

Then you could send “Attack Damage” to some other script in order to deal damage to monsters.

How would I do that?

Thanks for help.

You can define global variables as attributes of the GameLogic module.
Try for example:

script 1:

import GameLogic as GL
GL.var1, GL.var2 = 1., 2.

#script 2:

import GameLogic as GL
print(GL.var1, GL.var2)

I’m not sure whether this is the most efficient method but it works for me.


You can use globaldict also.

from bge import logic as gl

to read

from bge import logic as gl

You can even port it between .blends also.

thanks for trying to help… but i don’t get it. Probably never will. Been waiting forever for flowgraphs so :spin:

i mean what’s foo i don’t get it. I just don’t it.

foo is a just a dummy word. it is a common word among many hello word programs.
substitute foo with a name you wish to refer later.

Heres how to use your Attack Damage variable as a cross-script, global variable.

import bge
Attack_Dmg = "GetWeaponData"/"Get Player Strength" * "Player Agility" 
bge.logic.globalDict["AttackDmg"] = Attack_Dmg

Then in another script to read in Attack_Dmg

import bge
Attack_Dmg = bge.logic.GlobalDict["AttackDmg"]