How do I set mouse boundaries?

The real trick here is that there currently isn’t a way to set the mouse cursor position from Blueprint built into UE4.

In general, the process is fairly simple:

  • Determine the X and Y limits for your mouse cursor; you’ll need a minimum and maximum value for each. If you have a static camera, you can set values according to the room you’re in, or if they’re all the same size (like the original Zelda, or Binding of Isaac), you can use the same values all the time. If your camera moves, you’re going to have to update the values based on that movement.
  • Determine the current position of the mouse. This one is easy, you can do that by getting a reference to the player controller, and calling GetMousePosition(), you’ll get two floats - one for X and one for Y. Bear in mind that mouse coordinates are expressed in screen space, as X and Y coordinates on your screen, not within the game world. You can use DeprojectMousePositionToWorld from the PlayerController to get the mouse position in world coordinates, or you can convert your room position limits to screen coordinates.
  • Compare the current mouse position to your limits. Set branches for if X is greater than MaxX or less than MinX, and if Y is greater than MaxY or less then MinY.
  • If your mouse position is outside the limits, set the mouse position to be within the limites. (i.e., if X is greater than MaxX, set the X value to MaxX)

The last step is the real trick here. Within C++ you can get a reference to the Viewport from your player controller, and it has a method called “SetMouse”; but it’s not currently exposed to BP. You can either edit the engine to expose this method, or otherwise write your own object to give you this method. Or you can install Rama’s BP Library plugin that gives you the SetMouse functionality.

(39) Rama's Extra Blueprint Nodes for You as a Plugin, No C++ Required! - Blueprint - Unreal Engine Forums!

I think I’m going to go through the native SetMouse method and see if there’s any reason Epic hasn’t exposed it to BP, and if not I’ll just do it myself and see if they accept the modification.