I’m trying to create a script that I can just attach to just one object (the camera in this instance), that will temporarily change the color of any object that you mouse over. I can get it to change the color of objects, but not to change them back to their original color after the mouse is no longer over the object.
old, non working code I tried:
import bge
import mathutils
import GameLogic
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
mouseoverany = cont.sensors["mouse over any"]
obj = mouseoverany.hitObject
if obj is not None and mouseoverany.positive: #while the mouse is not over anything,
#"hitObject" returns "None".
origin = obj.color[:] #I cant assign the value "None" to origin,
obj.color = [0,1,0,0] #so I added "is not None" to the "if" statement
elif 'origin' in locals(): #this part should change the object back to its original color.
#I use "origin in locals()" because if origin is not defined,
obj.color = origin[:] #there will be an error.
print ("origin in locals") #
else:
print ("origin not in locals")
[SOLVED]
I was able to get my code to work with global variables. I created a script with an “always” sensor that set up the global variables.
import bge
import mathutils
import GameLogic
cont = bge.logic.getCurrentController() #use every time
own = cont.owner #use every time
scene = bge.logic.getCurrentScene()
GameLogic.oldobj = scene.objects["plane"] #the variable "oldobj" must be set to a dummy object first
GameLogic.newobj = [0]
GameLogic.oldcolor = [0]
then I was able to set up a simpler “if” statement in the main script to make everything work properly.
import bge
import mathutils
import GameLogic
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
mouseoverany = cont.sensors["mouse over any"]
if mouseoverany.positive:
GameLogic.newobj = mouseoverany.hitObject
GameLogic.oldcolor = GameLogic.oldobj.color[:]
GameLogic.newobj.color = [0,1,0,0]
else:
GameLogic.oldobj = GameLogic.newobj
GameLogic.oldobj.color = GameLogic.oldcolor[:]