1
$\begingroup$

Recently, I've started teaching myself Blender Game Engine with video tutorials. Mostly I utilize Logic Bricks, but I'm attempting to branch out into Python scripts.

I have successfully created a working custom mouse overlay using the following code:

import bge

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

over = cont.sensors['over']

own.worldPosition = over.hitPosition

Unfortunately, when the plane I'm using reaches any edge of the game window it jumps back to the center of the viewport. It's very distracting and not the effect I'm going for at all.

What is the proper logic to prevent it from defaulting back to the center of the game window?

$\endgroup$

1 Answer 1

0
$\begingroup$

Check that the mouse over sensor is actually outputting a value:

import bge

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

over = cont.sensors['over']
if over.positive:
    own.worldPosition = over.hitPosition

The problem is caused by what's sometimes called the 'false' logic pulse. A sensor will run the python script twice, once when it is turned on and once when it is turned off (the frequency option makes it turn on every frame). Thus, when the mouse leaves the window, the sensor outputs a 'false' signal (detectable because over.positive is not True), and the values within the sensor are not guaranteed to be sensible. In this case, the default values of the hitPosition are the center of the screen.

$\endgroup$
3
  • $\begingroup$ Yes! This worked exactly. $\endgroup$
    – micjobe
    Jan 23, 2017 at 14:44
  • $\begingroup$ Would you mind explaining how though? I don't understand why an if statement changes the logic and why the original code wasn't checking for an output value. $\endgroup$
    – micjobe
    Jan 23, 2017 at 14:52
  • $\begingroup$ Updated answer. $\endgroup$
    – sdfgeoff
    Jan 23, 2017 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .