Setting the ambient color, background colors, and character gravity using Python

In the attached file I have a Python script that is designed to change the worlds background color, ambient color, the mass an object (since I can’t find a way to change the gravity for a single object, rather then the entire scene), and the energy of a sun lamp. The script creates a list of custom (programing) objects, each with their own variables, then checks the z location of the rocket by iterating through the list, and then assigns the values (bg color, mass, ect) based on the rockets z location. The trouble is that none of the changes are applied, despite the console telling me that the conditions for them are triggered (it prints “True”).

# import bge moduleimport bge


class WorldObjData:
    def __init__(self, heigth, color, brightness, gravity):
        self.heigth = heigth
        self.ambColor = color
        
        self.color = [color[0], color[1], color[2], 1]
        
        self.brightness = brightness
        self.gravity = gravity


# get the render module
rend = bge.render


cont = bge.logic.getCurrentController()
own = cont.owner


scene = bge.logic.getCurrentScene()


sun = scene.objects["Sun"]


BU0 = WorldObjData(0.0, [0.2, 0.458, 1], 1, 1)
BU100 = WorldObjData(10.0, [0, 0.322, 1], 0.99, 0.85)
BU200 = WorldObjData(20.0, [0, 0.257, 0.8], 0.98, 0.70)
BU300 = WorldObjData(30.0, [0, 0.193, 0.6], 0.97, 0.55)
BU400 = WorldObjData(70.0, [0, 0.129, 0.4], 0.96, 0.40)
BU500 = WorldObjData(50.0, [0, 0.064, 0.2], 0.95, 0.35)
BU600 = WorldObjData(60.0, [0, 0, 0], 0.94, 0.2)


worldPos = own.worldPosition[2]


own["worPos"] = own.worldPosition[2] # debugging


getHeight = [BU600, BU500, BU400, BU300, BU200, BU100, BU0]


for key in getHeight:
    if worldPos >= key.heigth:
        print("True") #console debugging
        rend.setAmbientColor(key.ambColor)
        own["bgColor"] = key.ambColor[1]
        rend.setBackgroundColor(key.color)
        sun.energy = key.brightness
        own.mass = key.gravity
        


own["sun"] = sun.energy # debugging
own["massR"] = own.mass # debugging

Any ideas on what I’m doing wrong?

Attachments

rocketSim.blend (536 KB)

You’re iterating through the getHeight list from the objects with the largest height value to the one with the smallest value. Since you never break from the loop when you find a height value that is smaller than the current rocket height, you will always iterate through the entire list and repeatedly set the background color. This is why the color is always that of the last item in the list.

The easiest solution would to just add a break statement at the end of your if clause.

Thank you, that worked perfectly. The ironic thing is, I had had the idea of putting a break statement in while I was writing the loop, but somehow I not only forgot to put it in, but I completely forgot about the possibility of using a break statement. It’s like I designed a car, forgot to put in a break pedal, and during testing I was wondering why it wasn’t stopping.