Let me begin by saying I'm okay in python in general (and a half-decent programmer in some other languages), but I am very very new to python in Blender.
What I want to do
I have multiple objects all right on top of each other - say, a cube (1), a sphere (2), and a cone (3). When the game begins, only the cube is visible. The player can cycle through the objects by using the M and N keys, as in, when you cycle forward the cube becomes invisible and the sphere becomes visible. The player hits M to cycle forward, N to cycle backwards. When you hit the cone (the third object) you can't go forward anymore. Similarly, when you hit the cube (1st object) you can't go backwards anymore.
If you're curious, the application is I'm making a scientific visualization tool where you can step forward and backward through time while looking at datasets.
My hypothetical approach
I believe (but may be wrong) that I want to have some integer variable V that begins at 1; when you hit M it counts up; when you hit N it counts down; a statement checks if it goes below 1 or above 3 and corrects it back to 1 or 3 if so.
So here's a sort of code skeleton, which is incomplete and doesn't work yet, that gets at what I imagine the solution to look like:
V = 1 #initialize the variable V as 1 at game start
#Here would be some code to use the keyboard tap controllers M and N to
#increment/decrement the value of V, INCLUDING a check
#that if V < 1 you set it back to 1, and if V > 3 you set it to 3
#I think it would look something like this
#except I know I'm missing something:
if cont.sensors['m_key'].positive:
V += 1
elif cont.sensors['n_key'].positive:
V -= 1
if V < 1:
V = 1
if V > 3:
V = 3
#Now we check the value of V and set visibilities accordingly
#(which I also don't know how to do)
if V == 1:
#set cube visible
#set sphere invisible
#set cone invisible
elif V == 2:
#set cube invisible
#set sphere visible
#set cone invisible
elif V == 3:
#set cube invisible
#set sphere invisible
#set cone visible
The tricky parts (for me)
All I know how to do so far in Python is scripts that only affect the parent object (the controller's owner) like so
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
if cont.sensors['space_key'].positive:
own.visible = not own.visible
This script, connected to a keyboard sensor called space_key with spacebar in tap mode, will toggle the visibility of the object in which the keyboard sensor resides. That doesn't help me handle multiple objects though.
I also don't know how to initialize V as 1 just at game start, and have it count up and down. Just for fun I tried this code fragment: import bge cont = bge.logic.getCurrentController() own = cont.owner
V = 1
print(V)
if cont.sensors['m_key'].positive:
V += 1
print(V)
elif cont.sensors['n_key'].positive:
V -= 1
print(V)
and the output looked like "121" when I hit m once, and "101" when I hit n once
So I guess my main questions are:
- How do I change the visibility of multiple objects (perhaps using the object name) from one script?
- Where should this script reside, along with the M and N controllers? Should it be attached to the cube, or to some empty object somewhere? Or is it going to have to be multiple scripts?
- If it has to be multiple scripts, how does the variable V get initialized properly, and how does it get globally recognized by all the different scripts?