Global variables

How do I make, use and compare global variables/properties in BGE/Python?

http://bgepython.tutorialsforblender3d.com/GameLogic/globalDict

1 Like

^that, and also

bge.anyName = anyValue

And then there’s certain objects that can’t be saved to a dictionary, like an instance of a custom class. I’m quite unprofessional myself so I shelve things just to be done with it.

import shelve

#write
lyeb = shelve.open(filename)
try:
   lyeb['Object'] = classInstance
finally:
   lyeb.close()

#read
lyeb = shelve.open(filename)
try:
   object = lyeb['Object']
finally:
   lyeb.close()

print(object)
>>>className object at hexadecimal memory address

you can store a custom class in a dict just fine, i do it all the time.

and for the global variables just use the globaldict ,it is the simplest.

import bge

dict = bge.logic.globalDict

dict["var"] = "something"

Dangit, you’re right. I tried to save a class with some methods for animations and the dict couldn’t handle the bge specific code so my brain got it mixed up.

Do not use bge or bge.logic as a dict, use the globalDict it’s made for it.

just like @edderkop said.

How do I make comparison sensors for global properties or Actuators that assign or set them?

careful, what we are discouraging is the use of module spaces we didnt create. simply because we dont know whats all in there, and we might run into naming issues.

but if we made our own module, then its a great idea to use that.

import PY.base as base

base.PLAYERCLASS = Player()

class Player(base.CoreObject):

the above is abit out of order, but you get the idea.

That didn’t explain about the sensors or actuators.

I mean, see how you can do it with non-global properties.


Now how do I do the same with global ones? Or will they show up in the list when written the text file with global properties in it?

i was replying to cotaks in that post.

but the globalDict is a python solution, which cant be easily integrated into logic bricks.

what i recommend is using game object properties and using a save load system.

@Daedalus_MDW Tutorial, please?

cotaks has a save load in the resources section.

True, that’s why the GD exists, to keep our data separate from the engine data. And the GD can be saved/loaded with logic bricks as well, no idea if the bge.blabla or bge.logic.blabla get’s saved, in any way try to avoid it.

Yes, but in this case you create your own module, and that one has no crucial data inside it (or any at all) till you add some yourself.

no module level data gets saved when opening a new blend.

just one python script can be set to module mode and there you have your module to put stuff in.

global myvar

will let you find module variables in the current module

import bge

MYVAR = 0
PLAYER = None

class SomeClass:
    def __init__(self, owner):
        self.owner = owner

    def RUN(self):
        global MYVAR
        MYVAR += 1

def START(cont):
    global PLAYER
    owner = cont.owner

    PLAYER = SomeClass(owner)

def RUN(cont):
    global PLAYER
    PLAYER.RUN()

Maybe I should clearify this a bit.

So, I have Crash in LibPlayer.blend, and the instance that it should destroy in LibProps.blend. I want wumpa to fly away when Crash spins. And when he spins, CrashSpin.visible is set true. However CrashSpin.visible isn’t global variable, so it’s not visible to the LibProps.blend.

So any solutions to that?

each object in the bge is a class. one that can be passed anywhere. im assuming CrashSpin is the name of an object. all you need to do is save that class to a variable.

this easily done with

MYVAR = owner # owner is the CrashSpin object.

or

you can use scene.addObject() to add the CrashSpin object when needed, hiding the player object until you CrashSpin.endObject().

Crash’s main mesh already hides when CrashSpin turns visible. But now when Crash DOESN’T spin, Wumpa still collides with the invisible CrashSpin. It should collide when CrashSpin IS visible. You get the point, do you?

PS. would you like to join making this fangame? That way you get all the game’s files which help you realize how it works, and help you with coming up with solutions. Further info here:

id certainly like to have a look at whats going on. if its as bad as what people say, then id port it to my 3rd person template i want to release soon. we can talk more about it in pm or something.

We could also talk about it in project’s Discord

turns out i cant use discord on mobile, ill have to try my pc later.