2.5 Custom Mouse Cursor

How can I use a custom mouse cursor in-game with Blender 2.5?
Blendenzo’s tut doesn’t work with 2.5 :no: :frowning:

What doesn’t work about it? I’m guessing for one the script is using the old api and needs to be updated.

Here is the code:

import Rasterizer as R
cont = GameLogic.getCurrentController()
own = cont.getOwner()
mPosi = cont.getSensor("MousePosi")

### Start mouse position at center of game screen
if own.init == 1:
    R.setMousePosition(R.getWindowWidth()/2, R.getWindowHeight()/2)
    own.init = 0

### Move cursor to mouse position
cursorPosi = mPosi.getRaySource()
own.setPosition(cursorPosi)

I don’t know much about scripting, but here is the error that shows in the console window:

Attachments


Yup, it’s using the old api, I didn’t test the following, but I think it should work.

import Rasterizer as R
cont = GameLogic.getCurrentController()
own = cont.owner
mPosi = cont.sensors["MousePosi"]

### Start mouse position at center of game screen
if own['init'] == 1:
    R.setMousePosition(R.getWindowWidth()/2, R.getWindowHeight()/2)
    own['init'] = 0

### Move cursor to mouse position
cursorPosi = mPosi.raySource
own.worldPosition = cursorPosi

Edit: Moguri beat me to it

That code is using the old api, these are some of the chages needed.

own = cont.getOwner() –> own = cont.owner
cont.getSensor(“MousePosi”) –> cont.sensors[“MousePosi”]
own.init –> own[“init”] (stupid)
mPosi.getRaySource() –> mPosi.raySource
own.setPosition(cursorPosi) –> own.position = cursorPosi

try this code

import Rasterizer as R
cont = GameLogic.getCurrentController()
own = cont.owner
mPosi = cont.sensors["MousePosi"]

### Start mouse position at center of game screen
if own["init"] == 1:
    R.setMousePosition(R.getWindowWidth()/2, R.getWindowHeight()/2)
    own["init"] = 0

### Move cursor to mouse position
cursorPosi = mPosi.raySource
own.position = cursorPosi

Both of those codes give me errors as well. :frowning:
Here is the error I am getting now:

Attachments


It would appear that Py3k does floating point arithmetic by default rather than integer division like previous versions. Try this:

import Rasterizer as R
cont = GameLogic.getCurrentController()
own = cont.owner
mPosi = cont.sensors["MousePosi"]

### Start mouse position at center of game screen
if own['init'] == 1:
    R.setMousePosition(R.getWindowWidth()//2, R.getWindowHeight()//2)
    own['init'] = 0

### Move cursor to mouse position
cursorPosi = mPosi.raySource
own.worldPosition = cursorPosi

It works! Thank you very much. :slight_smile:

It is not working for me, in blender 2.57b

>>> import Rasterizer as R
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ImportError: No module named Rasterizer

>>> cont = GameLogic.getCurrentController()
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'GameLogic' is not defined

>>> own = cont.owner
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'cont' is not defined

>>> mPosi = cont.sensors["MousePosi"]
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'cont' is not defined

>>> 
>>> ### Start mouse position at center of game screen
>>> if own['init'] == 1:
...     R.setMousePosition(R.getWindowWidth()//2, R.getWindowHeight()//2)
...     own['init'] = 0
... 
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'own' is not defined

>>> ### Move cursor to mouse position
>>> cursorPosi = mPosi.raySource
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'mPosi' is not defined

>>> own.worldPosition = cursorPosi

Yeah, I’m not sure why certain times GameLogic is a valid module, and at other times, it’s not. Here’s a 2.5 version that uses the 2.5 modules.


from bge import render
from bge import logic

cont = logic.getCurrentController() 
own = cont.owner 
mPosi = cont.sensors["MousePosi"]  ### Start mouse position at center of game screen

if own['init'] == 1:     
     render.setMousePosition(render.getWindowWidth()//2, render.getWindowHeight()//2)     
     own['init'] = 0  ### Move cursor to mouse position 
cursorPosi = mPosi.raySource 
own.worldPosition = cursorPosi

EDIT: @mills - I didn’t read the errors fully before - you’re probably running the script with the Run Script command. Put that script into a text file, and then plug it into a Python controller - attach a mouse sensor named ‘MousePosi’ to it. I assume the mouse sensor should be set to Mouse Over Any or something to that effect. Play around with those settings. Also, have an object property named ‘init’ set to 1 in an object.

It is working perfect!
It seems 2.57b didn’t like “import rasterizer”

Thanks a lot!