Get keyboard input when held during level load

Is there any way to retrieve keyboard input for an axis for which the key is held down during level load?

I have a situation where I load a level, and due to the nature of the game the player is likely to be holding down “accelerate” throughout. The input axis has bindings for gamepad shoulder axis and keyboard. This works fine for the gamepad - the player accelerates before and after the level load as expected. But if the accelerate key is held down then the input axis reads zero in the new level and the player doesn’t accelerate. The axis input doesn’t change until you release the key and press it again.

I’ve investigated this behaviour in an isolated test project. In this case I have two levels, and I simply print a debug message if the key is held down (using the player controller’s “is input key down” or other methods don’t seem to make a difference). After a few seconds I transition to a new level, which does the same thing. I hold the relevant input key down the whole time. When starting the game in level 1 there is a delay of half a second before the debug message shows the key is down. It then stays down until the level load. Then when level 2 is loaded the debug message shows the key to not be down even though it is, and it never goes down unless I release and re-press it.

So questions: Is this expected behaviour? Is there a proper way to get the behaviour I want i.e. keyboard input to always be correct despite level transitions? Or is there an advised workaround, perhaps saving the previous keyboard state in the game instance and re-applying it after level load?

Hope you can help.

I don’t know if this is intended behavior. Frankly I have my doubts. However, I do have an alternative suggestion. Instead of polling they key state, you could hook into a keyboard/mouse event, say C. It should look like the following image:

225480-keyboardeventc.png

If you’ve used Action events before, this should look familiar to you. Here, you can then react to the button being pushed down and store that in a custom player state object. Once released, you do the same. Accordingly you test that stored variable, then.

The thing about PlayerState is, it persists between sessions, and accordingly also between levels. At least as far as I’m aware. I have yet to work with it tho, so no guarantee there. (See [this question here][2].)

So in theory, that stored variable should be still true once you load into the new level, then once you release that button, you should probably receive the event… worth a try, I’d say! :stuck_out_tongue:

In my investigation attempts I looked at the simple bool “is key down” test on the player controller, but in the actual game I’ve been using the axis event (and also tried polling the axis, same issue) and updating variables based on that. I think that should behave the same for gamepad or keyboard, but obviously keyboard is causing a problem. Annoyingly it’s not that the event doesn’t fire, rather that the event fires but with the wrong value.

The idea of caching the input from the event in an object that persists across level load is what I was getting at when I mentioned the game instance, but it hadn’t occurred to me to split the keyboard input off into a separate binary event, rather than another input on the same axis. I’ll have a play with that. Thanks.

Did you fix it? Im having the very same issue

No, I never found a fix I’m afraid, but in the end my design for the relevant part of my game changed so it became much less of an issue. Sorry, hope you can sort it out - would love to know a workaround if you find one.