• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

Subscribing to the FlightSaved event doesn't work?

Messages
5
Country
unitedstates
i was playing around with the managed system event SDK sample. i added one additional event to subscribe to. it's the FlightSaved event. i added the following line in the section where events are subscribed to:

Code:
simconnect.SubscribeToSystemEvent(EVENTS.SAVEFLT, "FlightSaved");

the sample project runs fine and receives notifications for the other events, but not for FlightSaved. i tried saving a flight from the free flight screen, and while flying. the event just never seems to be sent to the simconnect client. is this a known issue? has anyone successfully subscribed to FlightSaved? i'm running FSX SP2 and SDK SP2.

here's the full code:

Code:
    public partial class Form1 : Form
    {

        // User-defined win32 event
        const int WM_USER_SIMCONNECT = 0x0402;

        // SimConnect object
        SimConnect simconnect = null;

        // System state switches
        int FS_System_4    = 0;
        int FS_System_Sim  = 0;

        enum REQUESTS
        {
            REQUEST_4S,
            REQUEST_SIMSTATE,
        };

        enum EVENTS
        {
            SIMSTART,
            SIMSTOP,
            FOURSECS,
            SAVEFLT,
        };
       
        public Form1()
        {
            InitializeComponent();

            setButtons(true, false, false);
        }
        // Simconnect client will send a win32 message when there is 
        // a packet to process. ReceiveMessage must be called to
        // trigger the events. This model keeps simconnect processing on the main thread.

        protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_USER_SIMCONNECT)
            {
                if (simconnect != null)
                {
                    simconnect.ReceiveMessage();
                }
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }

        private void setButtons(bool bConnect, bool bGet, bool bDisconnect)
        {
            buttonConnect.Enabled = bConnect;
            buttonRequest4.Enabled = bGet;
            buttonSimStart.Enabled = bGet;
            buttonDisconnect.Enabled = bDisconnect;
        }

        private void closeConnection()
        {
            if (simconnect != null)
            {
                // Unsubscribe from all the system events
                simconnect.UnsubscribeFromSystemEvent(EVENTS.FOURSECS);
                simconnect.UnsubscribeFromSystemEvent(EVENTS.SIMSTART);
                simconnect.UnsubscribeFromSystemEvent(EVENTS.SIMSTOP);
                simconnect.UnsubscribeFromSystemEvent(EVENTS.SAVEFLT);

                // Dispose serves the same purpose as SimConnect_Close()
                simconnect.Dispose();
                simconnect = null;
                displayText("Connection closed");
            }
        }

        // Set up all the SimConnect related event handlers
        private void initSystemEvent()
        {
            try
            {
                // listen to connect and quit msgs
                simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
                simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);

                // listen to exceptions
                simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);
                
                // listen to events
                simconnect.OnRecvEvent +=new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent);
 
                // Subscribe to system events
                simconnect.SubscribeToSystemEvent(EVENTS.FOURSECS, "4sec");
                simconnect.SubscribeToSystemEvent(EVENTS.SIMSTART, "SimStart");
                simconnect.SubscribeToSystemEvent(EVENTS.SIMSTOP, "SimStop");
                simconnect.SubscribeToSystemEvent(EVENTS.SAVEFLT, "FlightSaved");

                // Initially turn the events off
                simconnect.SetSystemEventState(EVENTS.FOURSECS, SIMCONNECT_STATE.OFF);
                simconnect.SetSystemEventState(EVENTS.SIMSTART, SIMCONNECT_STATE.OFF);
                simconnect.SetSystemEventState(EVENTS.SIMSTOP, SIMCONNECT_STATE.OFF);
                simconnect.SetSystemEventState(EVENTS.SAVEFLT, SIMCONNECT_STATE.OFF);

            }
            catch (COMException ex)
            {
                displayText(ex.Message);
            }
        }

        void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
        {
            displayText("Connected to FSX");
        }

        // The case where the user closes FSX
        void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
        {
            displayText("FSX has exited");
            closeConnection();
        }

        // The case where the user closes the client
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            buttonDisconnect_Click(sender, null);
        }

        void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            displayText("Exception received: " + data.dwException);
        }

        void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent)
        {
            switch (recEvent.uEventID)
            {
                case (uint) EVENTS.SIMSTART:

                    displayText("Sim running");
                    break;

                case (uint) EVENTS.SIMSTOP:

                    displayText("Sim stopped");
                    break;

                case (uint) EVENTS.FOURSECS:

                    displayText("4s tick");
                    break;

                case (uint) EVENTS.SAVEFLT:

                    displayText("Flight saved: " + ((SIMCONNECT_RECV_EVENT_FILENAME)recEvent).szFileName);
                    break;
            }
        }

         private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (simconnect == null)
            {
                try
                {
                    // the constructor is similar to SimConnect_Open in the native API
                    simconnect = new SimConnect("Managed System Event", this.Handle, WM_USER_SIMCONNECT, null, 0);

                    setButtons(false, true, true);

                    initSystemEvent();

                }
                catch (COMException ex)
                {
                    displayText("Unable to connect to FSX " + ex.Message);
                }
            }
            else
            {
                displayText("Error - try again");
                closeConnection();

                setButtons(true, false, false);
            }
        }

        private void buttonDisconnect_Click(object sender, EventArgs e)
        {           
            // If they are on, turn off the system event subscriptions
            if (FS_System_4 == 1)
                buttonRequest4_Click(sender, null);
            if (FS_System_Sim == 1)
                buttonSimStart_Click(sender, null);

            closeConnection();
            setButtons(true, false, false);
        }

        private void buttonRequest4_Click(object sender, EventArgs e)
        {
            // Toggle switch
            FS_System_4 = 1 - FS_System_4;

            if (FS_System_4 == 1)
            {
                simconnect.SetSystemEventState(EVENTS.FOURSECS, SIMCONNECT_STATE.ON);

                buttonRequest4.Text = "Stop 4 sec event";
            } else
            {
                simconnect.SetSystemEventState(EVENTS.FOURSECS, SIMCONNECT_STATE.OFF);

                buttonRequest4.Text = "Request 4 sec event";
            }    
        }

        private void buttonSimStart_Click(object sender, EventArgs e)
        {
            // Toggle switch
            FS_System_Sim = 1 - FS_System_Sim;

            if (FS_System_Sim == 1)
            {
                simconnect.SetSystemEventState(EVENTS.SIMSTART, SIMCONNECT_STATE.ON);
                simconnect.SetSystemEventState(EVENTS.SIMSTOP, SIMCONNECT_STATE.ON);
                simconnect.SetSystemEventState(EVENTS.SAVEFLT, SIMCONNECT_STATE.ON);

                buttonSimStart.Text = "Stop sim events";
            }
            else
            {
                simconnect.SetSystemEventState(EVENTS.SIMSTART, SIMCONNECT_STATE.OFF);
                simconnect.SetSystemEventState(EVENTS.SIMSTOP, SIMCONNECT_STATE.OFF);
                simconnect.SetSystemEventState(EVENTS.SAVEFLT, SIMCONNECT_STATE.OFF);

                buttonSimStart.Text = "Request sim events";
            }
        }

        // Response number
        int response = 1;

        // Output text - display a maximum of 10 lines
        string output = "\n\n\n\n\n\n\n\n\n\n";

        void displayText(string s)
        {
            // remove first string from output
            output = output.Substring(output.IndexOf("\n") + 1);

            // add the new string
            output += "\n" + response++ + ":" + s;

            // display it
            richResponse.Text = output;
        }
    }
 
You need to hook a different event callback. For any event that returns a filename string, you need to hook the OnRecvEventFilename event:

Code:
simconnect.OnRecvEventFilename += new SimConnect.ReceiveEventFilenameEventHandler(simconnect_OnRecvEventFilename);
 
Back
Top