Adam's Lair Forum
https://forum.adamslair.net:80/

Keypressed between scenes
https://forum.adamslair.net:80/viewtopic.php?f=4&t=1241
Page 1 of 1

Author:  tufduck [ 2018/08/14, 10:17 ]
Post subject:  Keypressed between scenes

Have a strange issue here... I have two scenes, a TitleScreen and the GameScreen. The title screen is the startup scene, and to switch to the actual game scene I just check for a keypress, and then do the switch.

Now, the weird thing.. The key I press in the title scene is "propagated" or "transferred" to the game scene, meaning that if I press let's say Spacebar, then when the game scene loads and my keyboard manager is activated, it instantly triggers a Spacebar keypress and does whatever action I have associated with Spacebar in the game. I don't want that. How is that even possible, that one scene can detect keypresses from another scene??

Here is some code:

TitleScene
Code:
public void OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
                DualityApp.Keyboard.KeyDown += Keyboard_KeyDown;
            }
        }

private void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs e)
        {
            Scene.SwitchTo(GameScene);
        }



GameScene
Code:
public class KeyboardManager : Component, ICmpInitializable, ICmpUpdatable
    {
        public GameManager GameManagerRef { get; set; }

        public void OnInit(InitContext context)
        {
            if (context == InitContext.Activate)
            {
               
            }
        }
     
        public void OnShutdown(ShutdownContext context)
        {
            if (context == ShutdownContext.Deactivate)
            {
            }
        }

        public void OnUpdate()
        {
           
            #region Space, Escape, TAB

            // Space = Next phase
            if (DualityApp.Keyboard.KeyHit(Key.Space) && !Helpers.ShiftKeyHeld())
            {
                GameManagerRef.NextPhase();
            }
}

Author:  Adam [ 2018/08/15, 20:30 ]
Post subject:  Re: Keypressed between scenes

Input events are fired before the main scene update, so the scene switch will happen immediately. Right after the switch, the frames scene update kicks in, calling your update method and checking for the key hit. Since both happens within the same simulation frame, the hit is still there, and your code triggers from the same event that switched the scene before.

Probably the easiest solution would be to not use the KeyDown event for the switch, but doing a KeyHit check there as well. This way, the scene switch will be scheduled for end-of-frame, and the earliest possible update of the new scenes components will be after the next input update.

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/