Improving the UE4 Input system and Blueprint support!

by Nick July. 08, 14 0 Comment

Our current input system for Unreal Engine 4 offers a lot of flexibility and we provide the source code for it so you can modify it any way you want to serve your needs. There is one catch, though – you’ll need to write C++ code to do that. Inevitably, a popular request by our users was to add blueprint support for the input management, while retaining the same functionality. We’re always listening to our clients, so I’d like to explain in details one of the great new enhancements in Coherent UI 2.2 🙂
First of all, we’ll start with the added UV support for raycasting 3D objects. This means that now the actual render data is checked for collision when interacting with the mouse. While this is the most accurate way to tell where on the surface is the pointer, there are some considerations that have to be taken into account with this approach. Here are some examples:

  • If your mesh is very complex this raycast becomes expensive
  • There are various texture sampling modes and the diffuse texture can be in an arbitrary UV channel
  • The collision mesh of the object must have a specific type

There are, of course, various way to tackle those problems and you’ll see how we addressed the above in our implementation.

 

The next feature we added in the blueprints is the ability to set the focus to either Coherent UI or your game. When you set the focus to Coherent UI, the mouse starts interacting with all available Coherent UI Views. When you click on a View, it’s remembered as focused and all keyboard input goes to it until Coherent UI loses focus (or you click to another View and it starts receiving the keyboard input).

 

We also added events for previewing Key/Mouse Down/Up when Coherent UI is focused, since it otherwise consumes the input event and handlers down the chain are not reached.

 

Let’s do a quick crash course and see what’s new! I created a blueprint third person game and set up a sphere which displays a Coherent UI View. Here’s what the scene looks like:

Starting scene

Starting scene

 

We’ll now make the input focus switch between the game and Coherent UI upon pressing the “Tab” key – and we’ll do it with blueprints only! For starters, we need to initialize the Coherent UI Input system. This is done by spawning and instance of the “CoherentUIInputActor” class and initializing it:

Spawning the Coherent UI Input Actor

Spawning the Coherent UI Input Actor

 

Now after we hit play we’ll have an initialized Coherent UI input system. Let’s check out the properties that you can set in the “Initialize” function of the actor:

  • Collision channel – only objects of this physics system type will be checked for being under the cursor. The physics raycast uses an acceleration structure to check for hits and applying a type filter makes it event faster.
  • Address Mode – This is the texture sampling mode used for your object. In most cases it’s going to be “Wrap”, but for some effects you can use one of the others – “Clamp” or “Mirror”. That’s why we’ve exposed this option so you can change it.
  • UV Channel – When using complex materials, the base diffuse texture might not always be in the first UV channel and in those cases you’ll get wrong results. With this option you can set the UV channel that will be used.
  • Raycast quality – Complex meshes have lots of triangles and raycasting them each frame could have a performance impact. This option controls the LOD level of the mesh that will be raycast – you can balance between speed and accuracy.

These options are relevant only for Views on 3D objects and will have no effect for HUD Views.

 

Next, we’ll add a trigger for setting focus to Coherent UI. For demonstration purposes, this trigger will be pressing the “Tab” key, but you can do it through a physics trigger or anything else you like. Here’s the blueprint now:

Focusing Coherent UI with the Tab key

Focusing Coherent UI with the Tab key

 

Note that I used the “Toggle Coherent UI Input Focus” function, which is a convenience function for toggling the focus state without having to track it. You can also set (or unset) it directly and ask the Input Actor if Coherent UI has focus.

 

Now when you press “Tab” the player input stops working, the mouse cursor shows up and you can interact with Coherent UI Views using mouse/keyboard:

Focused Coherent UI View in the game

Focused Coherent UI View in the game

 

Once you have Coherent UI focused, you’ll probably want to be able to move you character at some point. Because of UE4’s design you won’t receive the “Tab” key event in the blueprint if you’re not in “Mouse Control” mode in the sample:

Mouse Control message

Mouse Control message

 

The Coherent UI Input system uses the same method for stopping player controller input so this means we’ll need to do something else to detect when the “Tab” key is pressed while Coherent UI is focused so we can unfocus it. One option for doing that is through the Coherent UI Binding – you can add some JavaScript code in your page that handles the “Tab” key, fires an event and the handler for that event makes Coherent UI lose focus. An alternative (easier) method would be to use the new Input Actor Key and Mouse preview events:

Coherent UI Input Actor Events (context-specific)

Coherent UI Input Actor Events (context-specific)

Getting game focus back from Coherent UI

Getting game focus back from Coherent UI

 

The “Coherent UI Input Key Down” event is bound after the actor is initialized and when Coherent UI is focused and you press a key the “KeyDown_Event” is fired. It provides a variable telling which key was actually pressed so we just add an “Enum equals” node and if the key pressed is “Tab”, we toggle the input focus. Since this event is only fired when Coherent UI is focused, this means we’ll now transfer focus to the game and make a conclusion to this post.

 

That’s how we improved the Coherent UI input system for UE4 and implemented the blueprints for it. We’ll be happy to hear your feedback if you like it or you have other ideas for making it even better.

 

Here’s a bonus – me trying to play a web demo of  “Assassin’s Creed®: Pirates” (http://race.assassinscreedpirates.com/) on a fairly odd surface 🙂

Assassin's Creed®: Pirates

Assassin’s Creed®: Pirates

 

Download Coherent UI for Unreal Engine 4 from Here!

Social Shares

Leave a Comment

Your email address will not be published. Required fields are marked *