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.

cont=GameLogic.getCurrentController()
own=cont.getOwner()

#get property
money=own.money

#get actuator
sendMoney=cont.getActuator("sendMoney")

#set subject
sendMoney.setSubject("money")

GameLogic.addActiveActuator(sendMoney,1)

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
GameLogic.money = 0

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

if gl.money > 5:
#Do stuff

I’ll try that, thanks Moguri.

Thank you Moguri, that works perfectly.

It works but since I have money=gl.money=0 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 gl.money.

gl=GameLogic
money=gl.money=0
cont=gl.getCurrentController()
click=cont.getSensor("click")
over=cont.getSensor("over")

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

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 GameLogic.money:


gl = GameLogic

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

  

Also, you can just use gl.money 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"):
    gl.money = 0
else:
    money = gl.money

cont = gl.getCurrentController()

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

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

print money