Hi
Is it possible to toggle visibility? What I’d like to do is to press a key e.g. spacebar to make an object visible but then make it invisible when I press the space bar again.
thanks
Hi
Is it possible to toggle visibility? What I’d like to do is to press a key e.g. spacebar to make an object visible but then make it invisible when I press the space bar again.
thanks
In module mode you can use the following Python script attached to a Keyboard sensor with Tap on
def visib(cont):
obj = cont.owner
if cont.sensors['Keyboard'].positive:
if obj.visible == False:
obj.visible = True
else:
obj.visible = False
You need to store in a property if the object is already visible or invisible.
This is an example usig logic bricks:
A way to compress Mperonen’s code would be:
def visib(cont):
obj = cont.owner
if cont.sensors['Keyboard'].positive:
obj.visible = not obj.visible
Out of curiosity, would compressing code give a speedup on code execution?
“compressing” is not the best expression of that. SolarLune showed a slightly different design.
If you count the operations it his hard to say which one is faster.
(btw. == False adds an unnecessary operation, visible is a boolean already, so I skipped it)
if obj.visible: # read attribute and check result
obj.visible = True # assign value
else:
obj.visible = False # assign value
so two processing paths = two results:
read attribute and check result +2 opertions
assign value +1
total = 3 operations (the same for the other condition)
obj.visible = not obj.visible # read, negate, assign
one processing path:
read, negate, assign = 3 opertions
both designs use 3 operations in all situations = speed difference depend on the duration of each operation.
Even if there is a difference … you can’t even measure it ;).
Oh that was a great answer! Thanks
When I said “compress”, I meant just that - getting the same result with less code (i.e. the process is compressed). You could say it’s a different design, but I’d say it’s a “thinner” or smaller design. Whether it does anything for speed or not I don’t personally know, though I wouldn’t worry about it, like Monster said. The difference is pretty much the epitome of minimal.
I have managed to toggle the visibility, but I want the object to start from invisible and when the key is pressed it becomes visible.
I do that that through the physics panel and I enable Invisible, but now I have to press the “toggle” button twice for the object to become visible (but only the first time, from then it works like a charm).
I hope that makes sense…
Edit: Also when I apply a MouseOver Sensor AND Right Button (that’s the button I’m using for the toggle) it doesn’t work at all.
ok, I am uploading a video now, you should have your answer in 11 minutes