Cursor position always 0

Hello, I am developing a game and I have a problem, the cursor is always 0, 0, and it does not matter what it does since it will always be in 0, 0.

How can i fix this?

Post some code and details of which platform, MonoGame version and such…

EDIT

And welcome to the forums :slight_smile:

How do you get cursor’s coordinates? On which platform?

Monogame 3.6 on Windows
To get the coordinates I only use the following.

MouseState mouseState = Mouse.GetState(); protected override void Update(GameTime gameTime) { Console.WriteLine($"{mouseState.X.ToString()} , {mouseState.Y.ToString()}"); }

You have to add

mouseState = Mouse.GetState();

at the top of Update() too !

And you can use a null declaration in your class (a member declaration is not updated until you put it in the method where it should be used !):
MouseState mouseState = null;

Final code:

MouseState mouseState = null
protected override void Update(GameTime gameTime)
{
mouseState = Mouse.GetState();

Console.WriteLine($"{mouseState.X.ToString()} , {mouseState.Y.ToString()}");
}
1 Like

Error! MouseState mouseState = null;
You can not convert NULL to ‘MouseState’ because it is a value type that does not accept NULL values.

But if I use MouseState mouseState;
No error occurs but is still in (0, 0)

Yeah, MouseState is a struct. But like @Alkher says you need to get the mouse state every frame (so inside your update method). It won’t automatically update or anything.

I’m currently not at home and no monogame available, but i’m sure you know how a variable declaration works, struct or not.

My MouseState.Position was always 0 because I used it inside the Draw instead of the Update method.