Color Sensor


in BGE I want to have a Sensor which detects whether there is an object at a specific spot and what color this object is. I would start with a near or radar sensor because the spot where I want to detect is always the same. But I’ve got no idea how to get the color. There can appear different objects in front of the sensor. I also want to delete these objects at this spot. but I don’t know how to select them to connect ist in the Game-Logic-Editor.

Gretings wicki

This would require python. You can set up the Logic Bricks by connecting all the sensors (+ an Always sensor) to a python controller (set to module mode), an all the actuators to that python controller. Make sure you name them correctly.

Starting code:

from bge import logic

def main(cont):
    own = cont.owner

You can get sensors with:

sensorName = cont.sensors["NameHere"] #put the sensor's name between the quotes, but leave the quotes

And actuators with:

actuatorName = cont.actuators["NameHere"]

Next, you can check whether the sensor is triggered or not with:

if sensorName.positive:
    #stuff will go here, mind the indentation

And activate actuators like this:


The object’s color, and this is the object color, not material, is defined with:


And it is a list of 4 things, RGBA.

So you can check if an object is red like this:

if own.color == [1.0, 0.0, 0.0, 1.0]:
    #do something

Combined, you could have something like this:
from bge import logic

def main(cont):
    own = cont.owner

    near = cont.sensors["Near"]
    move = cont.actuators["Move"]
    turn= cont.actuators["Turn"]

    if near.positive:
        if own.color == [1.0, 0.0, 0.0, 1.0]: #if object is red
            cont.activate(move) #move the object (actuator)
        elif own.color == [0.0, 1.0, 0.0, 1.0]: #if object is green

In the module controller, put “script.main” (without quotes)

Hopefully this helps :yes:

Okay, this is really great. I get the ogic in it but I’m still confused with a few things.
It is about a place where different objects can be placed. This would be the place where I would place the near or radar sensor. It’s only there to check whether there is an object. If there is an abject, I would like to get the colour it has. But its a different object. I don’t know the name or something else.

Sorry for my poor english, I’m not a native speaker.

Greetings and thank you in advance

The BGE provides sensors to detect objects (collision, ray, mouse over, near, radar).

Some of them can be configured to detect objects of a certain kind (filter). This is the presence of a property or the presence of a certain material.

As you wrote you want to detect an object at a known spot (area) you can use any of the sensors. You also mention that several objects can be in sensor range. This is a bit strange as which one would be the object you are looking for?

Nevertheless, when the sensor detected one or more objects you can evaluate the result via python controller.

Basically you loop through the detected objects (ray and mouse over detect one object only!).

Nemescraft already showed you what you can do. I want to isolate the tasks a bit more:

set it up as script

import bge

controller = bge.logic.getCurrentController()

sensor = controller.sensors["sensor name here"]

for hitObject in sensor.hitObjectList:
    print("Found object",, "with object color", hitObject.color, ".")
    print("No object detected.")

Be aware:

  • open the console window before starting the game session
  • this snippet prints the object color. Object color can be overidden by object material
  • this snippet expects you to entere a sensor name at “sensor name here”, otherwise you get an error
  • the controller needs to be triggered to execute the code (the sensor needs to detect at least once)
  • this snippet does not activate any actuator, nor does it anything with the detected objects

you don’t need to get the color , just give all you green objects a game property and name it green then you simply test for that property
and the the same for all the other colors.

thats not very useful. what if the objects change color? only so many colors can be expressed as words.

be warned, colors are stored as vector classes so == checks wont work as expected. you would need to loop through the indices.

its usfull if the colors are static and in many games they are, plus its pretty much the only way if you use logic bricks only.