#include <kgameproperty.h>
Inheritance diagram for KGameProperty< type >:


Public Member Functions | |
| KGameProperty (int id, KGamePropertyHandler *owner) | |
| Constructs a KGameProperty object. | |
| KGameProperty () | |
| This constructor does nothing. | |
| virtual | ~KGameProperty () |
| void | setValue (type v) |
| Set the value depending on the current policy (see setConsistent). | |
| bool | send (type v) |
| This function sends a new value over network. | |
| bool | setLocal (type v) |
| This function sets the value of the property directly, i.e. | |
| void | changeValue (type v) |
| This function does both, change the local value and change the network value. | |
| virtual void | save (QDataStream &stream) |
| Saves the object to a stream. | |
| const type & | value () const |
| virtual void | load (QDataStream &s) |
| Reads from a stream and assigns the read value to this object. | |
| const type & | operator= (const type &t) |
| This calls setValue to change the value of the property. | |
| const type & | operator= (const KGameProperty &property) |
| This copies the data of property to the KGameProperty object. | |
| operator type () const | |
| Yeah, you can do it! | |
| virtual const type_info * | typeinfo () |
Note: The entire API documentation is obsolete!
The class KGameProperty can store any form of data and will transmit it via network whenver you call send. This makes network transparent games very easy. You first have to register the data to a KGamePropertyHandler using KGamePropertyBase::registerData (which is called by the constructor). For the KGamePropertyHandler you can use KGame::dataHandler or KPlayer::dataHandler but you can also create your own data handler.
There are several concepts you can follow when writing network games. These concepts differ completely from the way how data is transferred so you should decide which one to use. You can also mix these concepts for a single property but we do not recommend this. The concepts:
This means that if you do the following:
KGamePropertyInt myProperty(id, dataHandler()); myProperty.initData(0); myProperty = 10; int value = myProperty.value();
This may look quite confusing but it has a big advantage: all KGameProperty objects are ensured to have the same value on all clients in the game at every time. This way you will save you a lot of trouble as debugging can be very difficult if the value of a property changes immediately on client A but only after one or two additianal messages (function calls, status changes, ...) on client B.
The only disadvantage of this (clean) concept is that you cannot use a changed variable immediately but have to wait for the KMessageServer to change it. You probably want to use KGamePropertyHandler::signalPropertyChanged for this.
KGamePropertyInt myProperty(id, dataHandler()); myProperty.setAlwaysConsistent(false); myProperty.initData(0); myProperty = 10; int value = myProperty.value();
The advantage of this concept is clear: you can use a KGameProperty as every other variable as the changes value takes immediate effect. Additionally you can be sure that the value is transferred to all clients. You will usually not experience serious bugs just because you use the "dirty" way. Several events have to happen at once to get these "strange errors" which result in inconsistent properties (like "game running" on client A but "game ended/paused" on client B). But note that there is a very good reason for the existence of these different concepts of KGameProperty. I have myself experienced such a "strange error" and it took me several days to find the reason until I could fix it. So I personally recommend the "clean" way. On the other hand if you want to port a non-network game to a network game you will probably start with "dirty" properties as it is you will not have to change that much code...
KGamePropertyInt myProperty(id, dataHandler()); myProperty.setAlwaysConsistent(false); myProperty.initData(0); myProperty = 10; myProperty.setAlwaysConsistent(true); myProperty = 20;
KGamePropertyInt myProperty1(id1, dataHandler()); KGamePropertyInt myProperty2(id2, dataHandler()); myProperty1.initData(0); myProperty2.initData(0); myProperty1.setAlwaysConsistent(false); myProperty2.setAlwaysConsistent(true); myProperty1 = 10; myProperty2 = 20;
So the right thing to do(tm) is to decide in the constructor whether you want a "clean" or "dirty" property.
Even if you have decided for one of the concepts you still can manually follow another concept than the "policy" of your property. So if you use an always consistent KGameProperty you still can manually call changeValue as if it was not always consistent. Note that although this is also kind of a "mixture" as described above this is very useful sometimes. In contrast to the "mixture" above you don't have the problem that you don't exactly know which concept you are currently following because you used the function of the other concept only once.
class Card { public: int type; int suite; }; QDataStream& operator<<(QDataStream& stream, Card& card) { Q_INT16 type = card.type; Q_INT16 suite = card.suite; s << type; s << suite; return s; } QDataStream& operator>>(QDataStream& stream, Card& card) { Q_INT16 type; Q_INT16 suite; s >> type; s >> suite; card.type = (int)type; card.suite = (int)suite; return s; } class Player : KPlayer { [...] KGameProperty<Card> mCards; };
Note: unlike most QT classes KGameProperty objects are *not* deleted automatically! So if you create an object using e.g. KGameProperty<int>* data = new KGameProperty(id, dataHandler()) you have to put a delete data into your destructor!
Definition at line 581 of file kgameproperty.h.
|
||||||||||||||||
|
Constructs a KGameProperty object. A KGameProperty object will transmit any changes to the KMessageServer and then to all clients in the game (including the one that has sent the new value)
Definition at line 597 of file kgameproperty.h. |
|
|||||||||
|
This constructor does nothing. You have to call KGamePropertyBase::registerData yourself before using the KGameProperty object. Definition at line 604 of file kgameproperty.h. |
|
|||||||||
|
Definition at line 606 of file kgameproperty.h. |
|
||||||||||
|
This function does both, change the local value and change the network value. The value is sent over network first, then changed locally. This function is a convenience function and just calls send followed by setLocal Note that emitSignal is also called twice: once after setLocal and once when the value from send is received
Definition at line 744 of file kgameproperty.h. References KGameProperty< type >::send(), and KGameProperty< type >::setLocal(). Referenced by KGame::setMaxPlayers(), KGame::setMinPlayers(), and KGameProperty< type >::setValue(). Here is the call graph for this function: ![]() |
|
||||||||||
|
Reads from a stream and assigns the read value to this object. This function is called automatically when a new value is received over network (i.e. it has been sent using send on this or any other client) or when a game is loaded (and maybe on some other events). Also calls emitSignal if isEmittingSignal is TRUE.
Implements KGamePropertyBase. Definition at line 780 of file kgameproperty.h. References KGamePropertyBase::emitSignal(), KGamePropertyBase::isEmittingSignal(), and KGamePropertyBase::setDirty(). Here is the call graph for this function: ![]() |
|
|||||||||
|
Yeah, you can do it!
int a = myGamePropertyInt;
Definition at line 831 of file kgameproperty.h. References KGameProperty< type >::value(). Here is the call graph for this function: ![]() |
|
||||||||||
|
This copies the data of property to the KGameProperty object. Equivalent to setValue(property.value()); Definition at line 818 of file kgameproperty.h. References KGameProperty< type >::setValue(), and KGameProperty< type >::value(). Here is the call graph for this function: ![]() |
|
||||||||||
|
This calls setValue to change the value of the property. Note that depending on the policy (see setAlwaysConsistent) the returned value might be different from the assigned value!! So if you use setPolicy(PolicyClean): int a, b = 10;
myProperty = b;
a = myProperty.value();
If you use a clean policy (see setPolicy) then the returned value is the assigned value Definition at line 807 of file kgameproperty.h. References KGameProperty< type >::setValue(), and KGameProperty< type >::value(). Here is the call graph for this function: ![]() |
|
||||||||||
|
Saves the object to a stream.
Implements KGamePropertyBase. Definition at line 754 of file kgameproperty.h. |
|
||||||||||
|
This function sends a new value over network. Note that the value DOES NOT change when you call this function. This function saves the value into a QDataStream and calls sendProperty where it gets forwarded to the owner and finally the value is sent over network. The KMessageServer now sends the value to ALL clients - even the one who called this function. As soon as the value from the message server is received load is called and _then_ the value of the KGameProperty has been set. This ensures that a KGameProperty has _always_ the same value on _every_ client in the network. Note that this means you can NOT do something like myProperty.send(1); doSomething(myProperty); You are informed about a value change by a singal from the parent of the property which can be deactivated by setEmittingSignal because of performance (you probably don't have to deactivate it - except you want to write a real-time game like Command&Conquer with a lot of acitvity). See emitSignal Note that if there is no KMessageServer accessible - before the property has been registered to the KGamePropertyHandler (as it is the case e.g. before a KPlayer has been plugged into the KGame object) the property is *not* sent but set *locally* (see setLocal)!
Definition at line 670 of file kgameproperty.h. References KGamePropertyBase::isLocked(), KGamePropertyBase::isOptimized(), KGamePropertyBase::sendProperty(), and KGameProperty< type >::setLocal(). Referenced by KGameProperty< type >::changeValue(), and KGameProperty< type >::setValue(). Here is the call graph for this function: ![]() |
|
||||||||||
|
This function sets the value of the property directly, i.e. it doesn't send it to the network. Int contrast to
myProperty.setLocal(1); doSomething(myProperty); If you want to set the value locally AND send it over network you want to call changeValue! You can also use setPolicy to set the default policy to PolicyLocal.
Definition at line 715 of file kgameproperty.h. References KGamePropertyBase::emitSignal(), KGamePropertyBase::isEmittingSignal(), KGamePropertyBase::isLocked(), KGamePropertyBase::isOptimized(), and KGamePropertyBase::setDirty(). Referenced by KGameProperty< type >::changeValue(), KGameProperty< type >::send(), and KGameProperty< type >::setValue(). Here is the call graph for this function: ![]() |
|
||||||||||
|
Set the value depending on the current policy (see setConsistent). By default KGameProperty just uses send to set the value of a property. This behaviour can be changed by using setConsistent.
Definition at line 615 of file kgameproperty.h. References KGameProperty< type >::changeValue(), KGamePropertyBase::id(), KGamePropertyBase::policy(), KGamePropertyBase::PolicyClean, KGamePropertyBase::PolicyDirty, KGamePropertyBase::PolicyLocal, KGameProperty< type >::send(), and KGameProperty< type >::setLocal(). Referenced by KGameProperty< type >::operator=(). Here is the call graph for this function: ![]() |
|
|||||||||
|
Reimplemented from KGamePropertyBase. Definition at line 833 of file kgameproperty.h. |
|
|||||||||
|
Definition at line 764 of file kgameproperty.h. Referenced by KPlayer::emitSignal(), KGame::gameStatus(), KGame::isRunning(), KGame::maxPlayers(), KGame::minPlayers(), KGameProperty< type >::operator type(), and KGameProperty< type >::operator=(). |
1.4.6