How to change diffuse_color on real time?

Hello everybody

I’m doing my fist test with blender game engine and I have a question, I’m trying to do the “Simon” memory game (http://en.wikipedia.org/wiki/Simon_(game)) using blender, I have the gameboard and I’m trying to add the logic and the events, what I’m doing is add a couple of sensors (for mouse over and mouse click), a couple of controllers (an AND and a Python controller) and an actuator (type Sound to get the note and linked to the AND controller) but I have a problem with the Python part, I’m trying that when the user click a block it changes his colour, I’m trying doing this:

import bpy
from bge import logic as G
from bge import render as R

owner = G.getCurrentController().owner
owner.diffuse_color = (1, 0, 0)

but that does not work, searching in google I found that is possible change the colour of all the elements with this:


import bpy

for item in bpy.data.materials:
    item.diffuse_color = (1,0,0)

and it works but the change of colour is not visible until I exit to the game (and change all the colours), so, my question is, how can I change the diffuse_color of the element that I clicked? is that possible? I attached an image showing what I have at this moment.

Thank you.
Regards.

Attachments


/uploads/default/original/4X/7/a/6/7a6be7869646a2cdd84599e7f3f3096d0c9bbdee.pngd=1326444037 (thanks for that image monster)

bpy is for non-real-time use only, and must not be used in game, as you’ve found out, it only changes things after the game has finished.
Rather, you must use bge functions eg:

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

own.colour = (0,0,0,1) #RGBAlpha

Thank you very much, but that does not change the colour of the object I tried also with:

 own.diffuse_color = (1, 0, 0)

but that does not work.
I can see that the game is taking the events because I can listen the sound that I set but I can’t see that the object change his colour.

Hello, like sdfgeoff has said “own.diffuse_color = (1, 0, 0)” isn’t correct. You do have to use "own.color (0,0,0,1) as the first value is red, the second is green, third is blue, forth is alpha. You need all 4 numbers in the sequence or it will not work. If you don’t want your object to have an alpha value, just set the last number to 1. These numbers are also floats so you would use any number between 0-1.

For example: own.color (1,0,0,1) would make your object pure red, own.color (0,1,0,1) would make your object pure green and
own.color (1,0.5,0,1) would make your object an orange color.

I hope this helps:)

Thank you very much, I finally solved that adding this code:


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

event = bge.logic.mouse.events[189]
sensor = cont.sensors['MouseOver']

if sensor.positive:
    if event == 1 or event == 2:
        own.color = (1, 0, 0, 1)
    if event == 3:
        own.color = (0.75, 0, 0, 1)


now just need select the correct colour… And play a bit with python to get the “memory” :slight_smile:

Thanks again.

Another thing that I would like to bring up is this line:

event = bge.logic.mouse.events[189]

It’s best to not use the number as a reference, as those numbers can and do change (between blender versions). I happen to know that is the Left Mouse button, so an alternative could be:

event = bge.logic.mouse.events[bge.events.LEFTMOUSE]

and instead of comparing it to 1 or 2 (which could, but probably won’t change) it is better to use:

bge.logic.KX_INPUT_JUST_ACTIVATED

What I would do is go

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

event = bge.logic.mouse.events[bge.events.LEFTMOUSE]
sensor = cont.sensors['MouseOver']
justActive = bge.logic.KX_INPUT_JUST_ACTIVATED
curActive = bge.logic.KX_INPUT_ACTIVE
justReleased = bge.logic.KX_INPUT_JUST_RELEASED


if sensor.positive:
    if event == justActive or event == curActive:
        own.color = (1, 0, 0, 1)
    if event == justReleased:
        own.color = (0.75, 0, 0, 1)

While this may seem to be a lot of bother, just wait for the next update to blender and enjoy re-doing your scripts!
(and I think the if statements are more readable)

Thank you very much sdfgeoff, I just reviewed the API and saw that properties but I didn’t understand how to use it properly but with your code I understand that, and yes I agree, the code that you provide is much better. Thank you for the support.

Best regards.