sample.dc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // It's necessary to explicitly import the Python files that define
  2. // the Python representation of the classes and structs named in this
  3. // file. (Implicitly importing these files turned out to be a bad
  4. // idea.)
  5. from direct.distributed import DistributedObject
  6. from direct.distributed import DistributedNode
  7. from game.avatar import AvatarObject
  8. from game.avatar import DistributedObjectHolder
  9. // All of the named modules are imported into a common Python
  10. // namespace, which is then searched for each named dclass or struct.
  11. // It is OK for a dclass to be named in this file that doesn't have a
  12. // corresponding Python representation, but it is then an error to
  13. // receive a generate message for one of these objects.
  14. // A simple typedef provides a name and a semantic context to types.
  15. typedef uint32 DoId;
  16. // A dclass defines a collection of "methods" which may be called as
  17. // network messages.
  18. dclass DistributedObject {
  19. setColor(uint8 red, uint8 green, uint8 blue);
  20. // The uint8[] syntax replaces the old uint8array type. All of the
  21. // old hard-coded array types are now deprecated in favor of C-style
  22. // array definitions.
  23. setPropertiesList(uint8 properties[]);
  24. };
  25. // You can also define C-style structs. This is really the same thing
  26. // as a dclass, except it can be embedded in a message rather than
  27. // created as an object in its own right. The struct may or may not
  28. // correspond with a Python class of the same name. If the struct
  29. // does have a Python representation, an instance of that class is
  30. // created and passed in to functions that receive this kind of
  31. // parameter; otherwise, a tuple with all of the fields is passed
  32. // instead.
  33. struct AvatarObject {
  34. // Limits to numeric ranges may be specified within parentheses
  35. // following the typename.
  36. uint8(0-10) type;
  37. int16(0-999, 2000-2999, 5000-5199) roomId;
  38. // You can specify a default initial value in case the value is not
  39. // already defined at the time generate is called.
  40. int8 code = 0;
  41. // You can also define "methods" on a struct, just as on a dclass.
  42. // This implies the existence of the corresponding get method
  43. // (e.g. getObjectCode() in this case), to query the information at
  44. // generate time.
  45. setObjectCode(int8(0-50) code, DoId player);
  46. };
  47. // Multiple inheritance is also supported.
  48. dclass DistributedObjectHolder : DistributedObject {
  49. dropObject(AvatarObject object);
  50. // You can also have an array of structs. This specifies a
  51. // fixed-length array of five elements. This is slightly more
  52. // optimal than an array of unrestricted length, since the length
  53. // prefix need not be transmitted as part of the message.
  54. setObjectList(AvatarObject objectArray[5]);
  55. // In addition to fixed-length arrays and unbounded arrays, you can
  56. // specify a range for the valid size of the array:
  57. setRelatedObjects(AvatarObject relatedObjects[0, 3 - 5] = []);
  58. };
  59. // You can specify a default initial value on the typedef, if you
  60. // like. This will be overridden by a default initial value specified
  61. // on the instance.
  62. typedef uint8(0-25) DNAColor = 1;
  63. struct AvatarDNA {
  64. // This defines a character element that can be any one of the three
  65. // specified. (It is similar in form to the uint8 definitions
  66. // below, except that the legal values are ASCII characters instead
  67. // of numbers.)
  68. char('a','q','x') type;
  69. // These specify one-byte numeric elements that can be any of the
  70. // values in the specified ranges.
  71. uint8(0-10) torsoIndex;
  72. uint8(0-5) headIndex;
  73. uint8(0-4) legsIndex;
  74. // A switch can be used to define alternate versions of the data
  75. // according to some value read from the stream. In this example,
  76. // the alternate cases are very similar, but they need not be
  77. // similar at all.
  78. switch (uint8 gender) {
  79. case 1:
  80. // Girl clothes
  81. uint8(0-35) shirtIndex;
  82. DNAColor shirtColor;
  83. uint8(0-25) skirtIndex;
  84. DNAColor skirtColor;
  85. break;
  86. case 0:
  87. // Boy clothes
  88. uint8(0-20) shirtIndex;
  89. DNAColor shirtColor;
  90. uint8(0-15) shortsIndex;
  91. DNAColor shortsColor;
  92. break;
  93. };
  94. // Nested structure references.
  95. DNAColor armColor;
  96. DNAColor headColor;
  97. };
  98. dclass DistributedAvatar {
  99. // An example of defining the default value for a complex nested
  100. // field, like the above.
  101. setDNA(AvatarDNA dna = {'a', 1, 2, 3, { 1, 0, 1, 0, 1 }, 1, 1 }) required broadcast db;
  102. };