In this lesson we'll be getting every objects in the scene, check the
object type and exclude object that are not needed.
A great example of this is the
audio spectrum by BluePrintRandom.
Import the game engine module, define current controller and owner object
(I won't be repeating after every lesson :/).
We get a list of objects with for loop with scene.objects
like so.
try:
import Range
except:
import bge as Range
cont = Range.logic.getCurrentController()
own = cont.owner
scene = Range.logic.getCurrentScene()
def main():
for obj in scene.objects:
print(obj)
Turn off true level triggering, for now we'll just need to know this once only.
Run the engine and in the console you'll be having results like so.
Now to see what our object's type is, we'll use python's
f-strings
which makes
string formatting easier unlike C like syntaxes with
%s,
%u,
%c, etc.
On 10th line we do like print(f"{obj}: {type(obj)}")
replacing print(obj). Now your console must be
looking like this.
You might've learned how check whether a certain varible is an int,
bool, string, etc. So here we check it with
python's built-in type() function.
Now say you want to get a list of lights and change all of their colors to red, we can do it
like so.
def main():
for obj in scene.objects:
#print(f"{obj}: {type(obj)}")
if type(obj) == Range.types.KX_LightObject: # <bge.types.KX_LightObject> in BGE/UPBGE
obj.color = (1,0,0)
else:pass
So, when you run the engine, you may find your environment slightly red which means it works! But you just need to turn off "Environment Lighting" just for now to see the whole scene in red.
That's all for today's lesson and here, download the project file.