Trying to write a simple mdoule to toggle visibility

I’ve been trying to write a VERY simple module to toggle the visibility of a cube. Very very simple just want to have it appear/disappear when a key is pressed. I know this can be done with logic bricks alone, but I may want to do this with BGE code in a module. I tried this code below, but it will only make the cube invisible and not toggle it back to visible. I want it to change once per key press. My text is named Text.py and I am calling with a keyboard sensor (with “Tap” enabled) and a python controller set to module mode, and the module name is Text.main. In the console it prints “True” once and then “False” for every other time I press a key.


import bge

def main(cont):
    print(cont.owner.visible)
    
   if cont.owner.visible:
       cont.owner.visible=False
   else:
       cont.owner.visible=True



import bge

def main(cont):
    cont.owner.visible = not cont.owner.visible

Double execution of python controller might be a problem here.


import bge

def toggle(cont):
    if not cont.sensors[0].positive:
        return
    cont.owner.visible = not cont.owner.visible

I have a question while on this topic…If i use a kepress for on and then the press the same key to turn something off like bill did…(like i for inventory)…i use properties and integers like this:


if not 'init' in owner
   owner["click"] = 1
 
if keypress.positive:
     owner["click"] += 1
 
 
if owner["click"] == 1:
   owner.visible = True
if owner["click"] == 2:
   owner.visible = False
if owner["click"] > 2:
   owner["click"] = 1
 

Is this a bad way of doing this?

Yes and no. Sure, it works; but for a Boolean state, toggling a boolean in place is the cleanest method.

Agoose, thanks again! The first code you posted didn’t work, but the second one works fine!

A bit of help here please!
How would I toggle visibility of the objects that are parented to the object that runs the script?
Like childs should do same as their parent.


import bge

#--- BGE callables
...
def toggleWidthChildren(cont):
    if not allPositive(cont.sensors):
        return

    cont.owner.visible = not cont.owner.visible
    setVisibilities(cont.owner.children, cont.owner.visible)

#--- internals
def setVisibilities(gameObjects, visible):
  for gameObject in gameObjects:
    gameObject.visible = visible

def allPositive(sensors):
  for sensor in sensors:
    if not sensor.positive:
      return False
  return True

as “good pratice” , i counsill to put ever one always sensor true…also when it totally usenless
in this way you here sure that the script run EVER.

in this way you can use the imput of sensors avoiding the wrong pulses
because the script run anyway!

in short you cannot using the code of the first post, no way, must be ever some line of control


import bge

def main(cont):
    own = cont.owner
    key = cont.sensors["SPACE"]
    if key.status == 1 : #<<<< --  line of control 
        if own.visible == True:
            own.visible = False
        else:
            own.visible = True


import bge

def main(cont):
    own = cont.owner
    key = cont.sensors["SPACE"]
    if key.status == 1 :  #<<<< --  line of control 
        own.visible = not own.visible


import bge

def toggle(cont):
    if not cont.sensors[0].positive: #<<<---------line of control
        return
    cont.owner.visible = not cont.owner.visible

remember “sensors always” is the best friend of python :wink: