count seconds in BGE python script when mouse_over

Hello everybody!

I´m trying to start a pop up window in the BGE when the mouse is rest 2 seconds over an object.

With that every object in my scene can get a pop up window that shows up when the mouse rests 2 seconds on the object and desappers when the mouse moves on.

To get a pop up window when the mouse is over the object is not a problem but I dont get it to count seconds in the BGE.

Can anybody tell me how I can define a variable (for example “time”) that starts counting when the mouse is over the object?

I hope someone can help me! Thanks to all of you how read that and trying to help me!

You can attach data directly to the object that is being processed. Here is some untested pseudo-code to give you an idea.



from time import time

# Get the current controller and the object.
controller = bge.logic.getCurrentController()
owner = controller.owner

# Get the mouse-over sensor
mouseOver = controller.sensors['mouseOver']

# The first time though 'owner' does not have a 'hoverStart' because
# we have not added it.  So initialize it now.
if 'hoverStart' not in owner:
  owner['hoverStart'] = None

if mouseOver.positive:
  # The mouse is over the object, see if we should start tracking the time.
  if not owner['hoverStart']:
    owner['hoverStart'] = time()

  # See if the mouse has been over the object for more than 2 seconds
  if time() - owner['hoverStart'] > 2:
    # DISPLAY YOUR POPUP 
    
else:
  # The mouse is not over the object.  Clear the time the mouse started to hover.
  owner['hoverStart'] = None


Basically, you need to remember when the mouse started to hover over an object. You can do that by remembering the time when the mouse first started to hover over the object. When the mouse stops hovering, you forget the time so it restarts.

Hey,
I’m trying it that way but I’ve got a problem … for testing it I don’t use a pop up … when the mouse is over for more then 2 Sec. blender should print “2 sec.” … the problem is that this happend in exactly that moment the mouse is over … it seems like it dosn’t count the time … do you have any idea why this happend?

I solved the problem … I use “time.sleep(2)” now and with that I’ve got it … thanks a lot for your help!

I believe you will find that your script will now freeze your entire game for 2 seconds.

I looked at this a bit closer and (on OSX 64-bit) it seems that BGE is not storing the time value correctly. If I do something like this:


tmp = time()
own['hoverStart'] = tmp
print("tmp: %f, hoverStart: %f" % (tmp, own['hoverStart']) )

Then I get two different values. It seems that when the float is passed into the KX_GameObject, it is losing some resolution.

Does anybody know why that might happen?