Adam's Lair Forum

game development and casual madness
It is currently 2018/12/12, 21:51

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 2018/08/14, 10:17 
Novice Member
Novice Member

Joined: 2017/08/13, 16:10
Posts: 12
Role: Hobbyist
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();
            }
}


Top
 Profile  
 
PostPosted: 2018/08/15, 20:30 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2063
Location: Germany
Role: Professional
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.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group