Is Global List variables doable in bge ? like arrays ( lists[ ])

I need a global list ( variable[] ) in a game that I need to access thru python.

I know how to read and update a property variable of any object in a scene, but I need to retain a list (an array [ ])

Is that doable?

Thank you

It’s doable. What exact kind of list do you need?

Also see the global dictionary, you can plug whatever you like into it and access it from anywhere

You can store a list in a property too ;).

Why do you think you need a “global”?
Why do you think you need a list?

I looked into the global dictionary and that works fine (See below). Thank you very much!

fyi- I am just used to using arrays in other programming languages, and thus Lists work for me within Python. I seemed to be restricted however because “add game properties” in the logic bricks only had “Timer, float, integer” etc and I don’t know how to initialize it as a list ( name[])

Here’s what i found online about global dictionary and it works…

#assign values to a global list
bge.logic.globalDict[“player1”]= [“tony”,“arnold”]
##saved global dictionary
bge.logic.saveGlobalDict()

load a saved global dictionary

bge.logic.loadGlobalDict()

get the saved player 1 stats from the just loaded global dictionary

player1_Stats = bge.logic.globalDict[“player1”]

#the following prints out “tony”
print (player1_Stats[0])

In python you don’t need to initalize things. So even if it was a string gwame property you could turn it into a list with a simple assignment.
You can also create properties at runtime with obj[‘newpropname’]

You also don’t need to save load it every frame, saving it dumps it to a file on the hard drive.

Well, just to be clear on a few points,

  1. The BGE doesn’t support all Python data types, just a few (and an additional Timer type that doesn’t really have an equivalent in Python).

  2. You do need to initialized variables with values before you attempt to read them, or else the interpreter will throw an error. You don’t need to initialize variables in the sense that any variable can be set from within Python without using the BGE types (though, again, there’s no simple equivalent for the BGE’s Timer data type). Here’s an example:



from bge import logic

cont = logic.getCurrentController()

obj = cont.owner

if 'init' not in obj:

    obj['init'] = 1  # Initialize the "init" variable if it doesn't exist already

    obj['items'] = {}  # Create a Python dict and store it in the object with the variable name "items"

print(obj['items'])


I guess it is time for a deeper look onto datastructures and objrcts.

I suggest the first couple of BGE guides in my signature. It will help you to understand the BGE (it does not train you Python - there are other resources to do that).

Please use [noparse]


[/noparse] tags.
Please do not repeat code in comments. If you think your code needs explanations write it in a cleaner way.