mouse over and sensor pulse questions

I have a few questions:

  1. if I use a lot of sensors with pulse mode running all the time, am I going to use up too many resources?(what I’m using the pulses for)

[INDENT=2]
Answer to 2 made this question irrelevant.
[/INDENT]
[INDENT=2]
[/INDENT]
2. are “if” statements the proper way to change the color of something when I mouse over it?

[TABLE="width: 500"]

[INDENT]
what I used:

import bge

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

mouseover = cont.sensors["mouse over"]
notmouseover = cont.sensors["not mouse over"]
keyboardup = cont.sensors["up"]

if notmouseover.triggered:
        print ("false")
        cont.owner.color = [1,1,1,0]
    
if mouseover.triggered:
        print ("true")
        cont.owner.color = [1,255,1,0]

This is the proper way to do it:

import bge                              #use every time
import mathutils

cont = bge.logic.getCurrentController() #use every time
own = cont.owner                        #use every time

mouseover = cont.sensors["mouse over"]




if mouseover.positive:
        cont.owner.color = [1,1,1,0]
    
else:
        cont.owner.color = [0,1,0,0]

[/TABLE]

  1. where do I enter/attach python scripts that I want to run every time I start my game?
    Answer: the camera is a good object to attach all your python scripts that you want to run when your game starts up

[/INDENT]

-Garrett

Moderation:

moved from Python Support
because it belongs to BGE

see thread

Sure why not.

Rather than checking the attribute “triggered”, better check the attribute “positive” :). For details have a look at the guides in my signature

On adding a object (e.g. at scene start)
Always [no pulse] -> Python controller

On every single frame (as long as the object exist):
Always [true pulse] -> Python controller

This way may allow you to use 1 sensor instead of two


import bge

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

mouseover = cont.sensors["mouse over"]
keyboardup = cont.sensors["up"]

        
if mouseover.positive:
        print ("true")
        cont.owner.color = [1,255,1,0]

else:    ##not triggered
        print ("false")
        cont.owner.color = [1,1,1,0]
       

or


if not mouseover.positive:
         print ("false")
         cont.owner.color = [1,1,1,0]
else:    ## triggered
         print ("true")
         cont.owner.color = [1,255,1,0]

   

I would set your mouse sensor to tap also…just check the console to see how many times its triggering to see how you need to set things up. You could probably even do something like this To make it only execute if the colors not changed already


if not mouseover.positive:
         print ("false")
         if not cont.owner.color == [1,1,1,0]:         
                 cont.owner.color = [1,1,1,0]
else:    ## triggered
         print ("true")
         if not cont.owner.color == [1,255,1,0]:
                 cont.owner.color = [1,255,1,0]


   

EDIT: After trying the code I notice that you should not use tap the way I have the code wrote. But the latter code does work

Object.color RGBA values are entered ranging from 0.0 to 1.0 so I don’t see how any of those scripts would work. The last number is alpha which needs to be other than 0.0 if you want your object to be visible at all.

Should be:


cont.owner.color = [0.0, 1.0, 0.0, 1.0]  # green
cont.owner.color = [1.0, 1.0, 1.0, 1.0]  # white
cont.owner.color = [0.0, 0.0, 0.0, 1.0]  # black
cont.owner.color = [0.0, 0.0, 0.0, 0.5]  # black, semi-transparent

EDIT: You can also just type 1 and 0 rather than 1.0 and 0.0 but technically you’ll want to indicate these are float not int.

derp :s
thank you

I tried using the “positive” attribute originally, but I must have done something wrong. using “positive” fixed my problem altogether. thank you :slight_smile:

On adding a object (e.g. at scene start)
Always [no pulse] -> Python controller

On every single frame (as long as the object exist):
Always [true pulse] -> Python controller

I meant, where do I attach the python code too. I read somewhere that using the camera for all your “always” python stuff was a good idea. I figured there would be a special place in the BGE that I would assign the python script. using the camera should work for what I am doing.

this worked:

if mouseover.positive:
        print ("true")
        cont.owner.color = [1,255,1,0]

else:    ##not triggered
        print ("false")
        cont.owner.color = [1,1,1,0]

I realized late last night that you use 0-1 not 0-255. I’m glad that if I had not figured that out, your post would have corrected my mistake. thank you.

The camera is a really worse place. It is a camera and should do camera things only.

In doubt create a new empty. Give it a meaningful name that you know what it is doing. In your case it seems you change the color when the mouse cursor is over an object. I suggest to call it: “ColorChangeHover”.

As it does not have a mesh it will not eat rendertime.

This way you can easily move this logic around as one piece. You can even share it with other files. You can enable, disable the logic just by moving the object to an hidden layer and back (or by switching layers). You can even have multiple different implementations.

HINT: when you move the mouse from one object to another without hover over background, you will see the controller gets not triggered. You can enable this with KX_MouseFocusSensor.usePulseFocus. Unfortunately there is no switch in the GUI. So you need Python to enable this configuration.