×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    18

    Saving Game State & Pausing Inactive Game

    Hi every body,

    I have developed a game for nokia s60v5 (Symbian ^1). Now I want to accomplish following tasks:

    1. Save the state of the game as the user exits it & then resume the game from that stat when he runs it again.

    2. If the game is minimized (e.g when there is a phone call during game play or the user presses menu button) the game should automatically be paused. i.e. all the timers should stop.

    (I have used a pause function in the game logic but it is not automatic. User has to press the pause button to pause the game)

    I have used QML for the graphics & JavaScript for the logic.
    How do I accomplish the above mentioned tasks??

  2. #2
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Saving Game State & Pausing Inactive Game

    Hello!

    1. To save the gamestate in a simple QML + JS game you can take a look at the QML Database API, this however requires some understanding of SQL. For more complex games you might have to create your own QML Extension to use the Qt C++ File API (wiki article here).

    2. To detect when the application goes inactive you can use
    Code:
    property bool active: Qt.application.active
    onActiveChanged: {
        console.debug("Active changed to " + active);
    }
    This way when active is true application is active, when it's false you can call the pause function to pause the application.

    Br,
    Villep

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    18

    Re: Saving Game State & Pausing Inactive Game

    1. Thanks Villep, well I've no understanding of SQL at all but I'll manage it myself.
    2. Unfortunately, these lines dont work.
    there is an error: "TypeError: Result of expression 'Qt.application' [undefined] is not an object."

    Any idea?

  4. #4
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Saving Game State & Pausing Inactive Game

    Which version of Qt are you using? If you are using older Qt which has QtQuick 1.0 then you need to do some additional work to get the active state because Qt.application was introduced in QtQuick 1.1. Basically you create your own EventFilter that you install to the application. How to detect application activation / deactivation can be found here.

    All you need to do after you create your EventFilter class is add a signal to EventFilter which you emit when active / deactive occurs.

    In the header
    Code:
    signals:
        void activeChanged(bool active);
    In the cpp
    Code:
    bool EventFilter::eventFilter(QObject *obj, QEvent *event)
    {
        if (event->type() == QEvent::ApplicationDeactivate) {
            emit activeChanged(false);
            return true;
        }
        if (event->type() == QEvent::ApplicationActivate) {
            emit activeChanged(true);
            return true;
        }
        return QObject::eventFilter(obj, event);
    }
    Then in your main you need to set the eventFilter as a context property to the QML side. Make sure you have installed the eventfilter to the application as mentioned in the wiki article.
    Code:
    //You need the context of your QDeclarativeView
    QDeclarativeContext *root = view.rootContext();
    root->setContextProperty("eventFilter", &eventFilter);
    Finally in your QML now you can simply do

    Code:
    Connections {
        target: eventFilter
        onActiveChanged: {
            console.debug("Active changed to" + active);
        }
    }
    Br,
    Villep

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    18

    Re: Saving Game State & Pausing Inactive Game

    Thanks again Villep,
    I followed the viki article & then added your code to the project. But this time it returns error on the line:
    Code:
    root->setContextProperty("eventFilter", &eventFilter);
    it says: error: invalid use of incomplete type 'struct QDeclarativeContext'

    Can you tell whats gone wrong?

    Here are the eventfilter.h, eventfilter.cpp and main.cpp files:

    main.cpp:
    Code:
    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "eventfilter.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        EventFilter eventFilter;
        app.installEventFilter(&eventFilter);
    
        QmlApplicationViewer viewer;
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
        viewer.setMainQmlFile(QLatin1String("qml/mySnake2/Game.qml"));
        viewer.showExpanded();
    
        QDeclarativeContext *root = viewer.rootContext();
        root->setContextProperty("eventFilter", &eventFilter);
    
        return app.exec();
    }
    eventfilter.h:
    Code:
    #ifndef EVENTFILTER_H
    #define EVENTFILTER_H
    
    
    #include <QObject>
    
    class EventFilter : public QObject
    {
        Q_OBJECT
    public:
        explicit EventFilter(QObject *parent = 0);
    
    signals:
        void activeChanged(bool active);
    
    public slots:
    
    };
    
    #endif // EVENTFILTER_H
    eventfilter.cpp :
    Code:
    #include "eventfilter.h"
    
    bool EventFilter::eventFilter(QObject *obj, QEvent *event)
    {
        if (event->type() == QEvent::ApplicationDeactivate) {
            emit activeChanged(false);
            return true;
        }
        if (event->type() == QEvent::ApplicationActivate) {
            emit activeChanged(true);
            return true;
        }
        return QObject::eventFilter(obj, event);
    }

  6. #6
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Saving Game State & Pausing Inactive Game

    Hello!

    That error is coming from missing an #include <QDeclarativeContext> from your main.cpp.

    Also in eventfilter.h you should add #include <QEvent> and then add
    Code:
    bool eventFilter(QObject *obj, QEvent *event);
    In the public section of the class, below explicit EventFilter(QObject *parent = 0);

    And finally in eventfilter.cpp add
    Code:
    EventFilter::EventFilter(QObject *parent) :
        QObject(parent)
    {
    }
    Br,
    Villep

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    18

    Re: Saving Game State & Pausing Inactive Game

    Thanks Villep, Its working!

Similar Threads

  1. Replies: 3
    Last Post: 2008-05-05, 20:42
  2. saving game status after press the red end button
    By ayanir in forum Mobile Java General
    Replies: 0
    Last Post: 2006-01-16, 16:07
  3. Softkeys & game pausing.
    By smb101 in forum Mobile Java General
    Replies: 5
    Last Post: 2004-05-11, 12:37
  4. Pausing a game when phone rings
    By anrikun in forum Mobile Java General
    Replies: 5
    Last Post: 2003-10-07, 06:18
  5. Pausing Game(very urgent)
    By kiranputhran in forum Mobile Java General
    Replies: 5
    Last Post: 2003-03-30, 19:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×