Creating Global Variables

Hi everyone,
I’m new to blender and I want to help my son building a very simple “asteroid like game” for a special school project. I need to find the most simple way to declare Global variables that could be read from other objects in the game, like the score of the game to allow differents things to happens. I know I can use python scripts but I’m not familiar with this coding (and my son doesn’t know anything in coding). When I create a “game property” at the left of the game panel for an object, this value is only accessible from the object itself. I want to use this variable anywhere in the game…

thank you !

1 Like

You could use the “copy” mode of the “Property” actuator. Though this becomes problematic when you need to have a large numbers of “global” properties.

In python or using the logic bricks?

In python, if you want multiple objects to communicate, you can use the globalDict:


import bge

dict = bge.logic.globalDict #Get the global dictionary
score = dict.get('score', 0) #Get the current score or the number 0
score += 5 #Add 5 to the score
dict['score'] = score #Store the new score

If you run this script, it will add 5 to the score. (which will start at zero).

If you want a text object to display said score, you can use:


import bge

dict = bge.logic.globalDict
score = dict.get('score', 0)

cont  = bge.logic.getCurrentController() #Get the controller the script is running from
own = cont.owner #Get the object the script is running from
own.text = str(score) #Set what is displayed

The score is now a property of the currently running game, and will always increase, even if you restart the scene (which is great for having things like a score scene). But you may want to reset it back to zero at the start of a game:


import bge

dict = bge.logic.globalDict
dict['score'] = 0

If you want to do coding, the most valuable resource is the BGE API, which can be found here. It’s pretty confusing to navigate though, so I’d recommend learning the basics from tutorialsforblender3d.com. Click on the thing you want to know about, and it’ll have a pretty good demonstration.
Other than that, there are some good BGE tutorials around, some of which you can find in the resources forums.

I will take a look in python scripts ! thanks all for this info.

And if I want to use the logic bricks ? where can I declare such variables? it is possible ? just for a start…

You can also access other objects properties from other objects in python.


import bge

own = bge.logic.getCurrentController().owner
scene = own.scene

#get object named "Player" in the scene
player = scene.objects["Player"]
hp = player["health"]
score = player["score"]

I don’t know the details of exactly what kind of feature you’re making but globalDict is mainly good for transitions between scenes/maps.

I have never been able to figure out how to access logic bricks from python its definitively would be nice to know if you know how to do so.

on the score object, add property score.
add message actuator - and - property.

then on object of choice, if score increases send a message(sensor) containing the info for the score object.

this way you send a message from an object to the score holding object, at the score object you recieve the message, when received you simply ad +1 or something to the score property.

Sorry, I know this isn’t the topic of the thread, but:

I have never been able to figure out how to access logic bricks from python its definitively would be nice to know if you know how to do so.

Quite simply, one uses the ‘controller’ better known as ‘cont’ and from there can access a list of sensor, actuators etc.


import bge

cont =bge.logic.getCurrentController()
sens = cont.sensors
act = cont.actuators

move_act = act['Motion'] #Get the actuator called motion
cont.activate(move_act) #Activate the actuator called motion
cont.deactivate(move_act) #Deactivae the actuator called motion

if sens['Mouse Click'].positive: #Check if the sensor 'Mouse Click' is active
    track_act = act['Track To'] #Get the track to actuator
    track_act.target = 'Cube' #Set the target of the track to actuator
    cont.activate(track_act) #Activate the track to actuator

All of the things you can edit for each actuator/sensor cna be found in the api under bge.types

And if I want to use the logic bricks ? where can I declare such variables? it is possible ? just for a start…

You can’t declare a global variable, but what you can do is create an object to store the variable, and then use messages to change it, and use the copy property actuator for other objects to access it.

Thank you that is very useful to know.

well, using the “messages” seems to answer to the problem. We have a very simple game, so it’s not so complicated this way. The minute we build a more complex game, it’s best to use python scripts.

thank you all !

Good to hear you found a solution.

Good luck with your game