Global property for making appear an object

Hi! i have a cube and a sphere. The sphere is not visible and i want to make it visible with one click in the cube.

I’m trying to use a global property named clicked, so i have one script to initialize it, running in the cube linked to an always sensor.
Initialize script:

import GameLogic as GL
GL.clicked = 0

Running in the cube i also have the 2 sensor for catching the clicks: leftclick and mouseover and the Clickcube script in a python controller:

import GameLogic as GL
import Rasterizer as R
R.showMouse(1)
contcube = GL.getCurrentController()
own = contcube.getOwner()
leftclick = contcube.getSensor(“leftclick”)
mouseover = contcube.getSensor(“mouseover”)
if leftclick.isPositive() and mouseover.isPositive():
GL.clicked = 1

In the sphere i have an always sensor linked to this python controller to make it visible when the global property is set to 1, this script name is Appearsphere:

import GameLogic as GL
import Rasterizer as R
R.showMouse(1)
contesf = GL.getCurrentController()
own = contesf.getOwner()
own.setVisible(0)
if (GL.clicked == 1):
own.setVisible(1)

And that’s all i have.
In the console there’s the error in the Appearsphere script because it has “no attribute clicked” so the global property is not working. Why? I didn’t add it as a property, using the Add Property button, but if i do that it’s not a global property…

This error appears because scripts are run in improper order (Appearsphere script before Initialize script). It can be solved by removing Initialize script and adding it’s logic to Appearsphere script:

if hasattr(GameLogic,'clicked') == 0:
  GameLogic.clicked = 0

Better solustion is to avoid using global variable and use message - cube will send message when is clicked on, and sphere will receive this message and become visible.

Yes, i’ll use messages:
I have the Message Actuator in the cube, linked to the python controller where i try to send the message:

import GameLogic as GL
import Rasterizer as R
R.showMouse(1)
contcube = GL.getCurrentController()
mes = contcube.getActuator("mes")
leftclick = contcube.getSensor("leftclick")
mouseover = contcube.getSensor("mouseover")
if leftclick.isPositive() and mouseover.isPositive():
	print"in the cube"
	GL.addActiveActuator(mes,1)

“in the cube” is printed in the console so it gets there.
In the sphere i added a Property named message which i used to fill the To field in the Message Actuator

import GameLogic as GL
import Rasterizer as R
R.showMouse(1)
contsphere = GL.getCurrentController()
own = contsphere.getOwner()
own.setVisible(0)
messensor = contsphere.getSensor("messensor")
if messensor.isPositive():
	print"got the message"
	own.setVisible(1)

Never prints “got the message” because messensor.isPositive() always = 0
I think that’s because of this line in the cube python:
GL.addActiveActuator(mes,1), but don’t know how to make it work :frowning:

You have problem with logic bricks setup. Try to clear all field in message actuator and also in message sensor. Then it should work.

Another problem is script for sphere. You set sphere to be invisible anytime this script runs. But it runs everytime when any sensor changes its state - so when you release the mouse button or move cursor away, script is started and set sphere back to invisible state.

Now it works!!!1 I cleared all fields in the message sensor and actuator, only used the Subject field, and in the sphere this script:

import GameLogic as GL
import Rasterizer as R
R.showMouse(1)
contesf = GL.getCurrentController()
own = contesf.getOwner()
mensen = contesf.getSensor("mensen")
print"voy a imprimir el mensen"
print mensen.isPositive()
if (mensen.isPositive()) and (own.togle == 1):
	own.setVisible(1)
elif (mensen.isPositive()) and (own.togle == 0):
	own.setVisible(0)
if (own.togle == 0) and (mensen.isPositive()):
	own.togle = 1
elif (own.togle == 1) and (mensen.isPositive()):
	own.togle = 0 
	

This works but i need the sphere to be invisible first and to turn it visible with a click, i switched the setVisible and the togle property but didn’t work
How do i make the sphere initially invisible???

Add a Always sensor for sphere with pulse mode switched off, and connect it to controller with little script:

import GameLogic as GL 
import Rasterizer as R 
R.showMouse(1) 
contsphere = GL.getCurrentController() 
own = contsphere.getOwner()
own.setVisible(0)

It will run only once at startup, shows mouse and hides sphere.

Another option for showing up sphere is to move it to invisible layer and use actuator Edit Object -> Add object to make sphere visible.