messages in python

I’m trying to make a small city building game. I’ve made it so you click to add a building but how would I make it so you have to have enough money before you can? A keyboard sensor adds one to the property “money”. I’ve got this code in a python controller connected to an always sensor and a message actuator.


#get property

#get actuator

#set subject


the other object is an empty and a cube with a left click, mouse over, and message sensor set to 3. It has an and controller and an add object actuator.
If money=3 and I click on the cube it should add a building but it doesn’t, Why?
I hope this was clear, I’ll post a .blend if you need it.
Thanks in advance.

Don’t use messages. Access your money property in Python, and type “if money > 3:”
before the line to add the building. You could do it without Python too, using an expression controller.

will that work if the property is on a different object.

You can link an actuator on the second object to the Python controller of the first object (shift-select both objects in 3D view) and then get the owner of the actuator, and the property of the owner.

I know I can do that but there are going to be a lot of objects to add buildings and I don’t want to link that many together.

It would just be the object with the “money” property. You wouldn’t need to do it for each individual building.

O.K. I have an object that has the money property and empties to add the buildings, I would have to link the object with the money property to every empty, wouldn’t I?

Better yet, make your money property an attribute of GameLogic.

#--Init the variable somewhere = 0

#--And whenever you need to use it...
gl = GameLogic #Since I don't like to type as much :D

if > 5:
#Do stuff

I’ll try that, thanks Moguri.

Thank you Moguri, that works perfectly.

It works but since I have at the beginning, every time the script runs it resets to zero. I have solved this with properties but I don’t now how to make it work with


if over.isPositive() and click.isPositive():

print money

click is a left click sensor and over is a mouse over sensor.
how can I get this to work right

You can use hasattr() to check if you’ve already defined

gl = GameLogic

if not hasattr(gl, "money"): = 0
    money =


Also, you can just use in every spot that you would want to use money…

were in the script would I put this?

Where you want to define the variable:

gl = GameLogic

if not hasattr(gl, "money"): = 0
    money =

cont = gl.getCurrentController()

click = cont.getSensor("click")
over = cont.getSensor("over")

if over.isPositive() and click.isPositive():
    money += 1

print money