[Qt-interest] question about QStateMachine setTargetStates() - how deterministic is it?

Alan Ezust alan.ezust at gmail.com
Mon Apr 12 18:02:16 CEST 2010


I'm trying to understand how QStateMachine works, and whether I can
use a spontaneous transition to multiple possible states as a
"randomizer" for a rock-scissors-paper game.

I notice that there is some randomness to choosing t's next state when
t is fired the first time,
but on repeated firings, t keeps bringing me to the same state it
picked the first time.

Is that a bug?

here is a code fragment - Player and Weapon are both derived from QState.


[using Qt 4.7.0-tp]


#include "player.h"
#include "weapon.h"

Player::Player(QString name, QState* parent)
: QState(parent),  m_score(0)
{
    setObjectName(name);

    // create child states - only one can be entered at a time.
    rockState = new Weapon(Weapon::ROCK, this);
    scissorsState = new Weapon(Weapon::SCISSORS, this);
    paperState = new Weapon(Weapon::PAPER, this);
    randomState = new QState(this);
    randomState->setObjectName("RandomState");


    /* create spontaneous transitions from random to real weapons
       Alas, it seems to always goes to the same state, rather than choosing a
       different target state each time.  How to randomize? */
    QList<QAbstractState*> weapons;
    weapons <<  scissorsState << rockState << paperState;
    QAbstractTransition *t = randomState->addTransition(randomState);
    t->setTargetStates(weapons);

    setInitialState(randomState);

}



More information about the Qt-interest-old mailing list