simObject_ScriptBinding.h 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleMethodRootGroupBeginWithDocs(SimObject)
  23. /*! @class SimObject
  24. SimObject is the base class for all other scripted classes.
  25. This means that all other "simulation" classes -- be they SceneObjects, Scenes, or plain-old SimObjects
  26. -- can use the methods and fields of SimObject.
  27. @par Identity
  28. When we create a SimObject with `::new`, it is given a unique id which is returned by `::new`.
  29. We usually save the id in our own variables. Alternatively, we can give the SimObject a name which we can
  30. use to directly manipulate it. This name can be set with the `::new` operator or it can be added later.
  31. If we give an object a name, then we can write script methods that are "scoped" to run on this object only.
  32. For instance, if we have a SimObject named `MyObject`, and if we call `MyObject.myMethod()`, then this
  33. call will be handled by a method we named `MyObject::myMethod` if one exists.
  34. See getId(), getName(), setName()
  35. @par Static and Dynamic Fields
  36. Each subclass of SimObject will provide important fields. For instance, a SceneObject will have a position,
  37. scale, etc. These are known as "static" or "built-in" fields. Additionally, you can add any number of your
  38. own fields, for example `myField` or `hitPoints`. These are known as "dynamic" or "add-on" fields.
  39. To do so only requires you to set the field directly with `%%myObject.myField = myValue;`. Attempting to
  40. retrieve a field that does not exist (yet) returns nothing. There is no error or warning.
  41. Note that static fields exist for every object of a class, while dynamic fields are unique to any one instance. Adding
  42. `myField` to one SceneObject does not add it to all SceneObjects.
  43. For working with fields more programmatically, see *Reflection* below.
  44. @par Script Namespace
  45. We can attach a `Namespace` to an object. Then calls to this object
  46. will be handled by the script functions in that Namespace. For instance, if we set `%%myObject.class = MyScope` then
  47. the call `%%myObject.myMethod` will be handled with a method named `MyScope::myMethod()`. (If we also named the object `MyObject`, then
  48. if there is a `MyObject::myMethod()` it will run. Otherwise, Torque2D will look for `MyScope::myMethod()` and run that
  49. if found.)
  50. Finally there is also a *secondary* `Namespace` that will receive the call if neither the `Name` nor the *primary* `Namespace`
  51. had a method to handle the call.
  52. Unfortunately, these `Namespaces` are called `Classes` in this context. You set the `class` or `superclass`. But this
  53. should not be confused with the SimObject's "real" class which is SimObject or Scene as examples.
  54. See getClassNamespace(), setClassNamespace(), getSuperClassNamespace(), setSuperClassNamespace()
  55. @par Reflection
  56. SimObject supports "reflection" -- the run-time inspection of an object's methods, fields, etc. For instance,
  57. we can ask an object what class it instantiates, what dynamic fields it has, etc. We can also use this feature to
  58. call a method on an object even if we only know the string name of the method.
  59. See getClassName(), isMemberOfClass(), isMethod(), dump(), dumpClassHierarchy(), call()
  60. See getFieldValue(), setFieldValue(), getFieldCount(), getField(), getDynamicFieldCount(), getDynamicField()
  61. @par Scheduling Callbacks
  62. We can set a SimObject to regularly call its own onTimer() callback. Additionally, we can schedule a single call to
  63. any method at any time in the future.
  64. See startTimer(), stopTimer(), isTimerActive(), schedule()
  65. @par Groups
  66. TBD
  67. canSaveDynamicFields bool = "1" - Whether a save() shall save the object's Dynamic Fields (member fields created by TorqueScript)
  68. */
  69. /*!
  70. */
  71. ConsoleMethodWithDocs(SimObject, save, ConsoleBool, 3, 4, (fileName, [selectedOnly]?))
  72. {
  73. bool bSelectedOnly = false;
  74. if(argc > 3)
  75. bSelectedOnly = dAtob(argv[3]);
  76. const char* filename = NULL;
  77. filename = argv[2];
  78. if(filename == NULL || *filename == 0)
  79. return false;
  80. return object->save(filename, bSelectedOnly);
  81. }
  82. /*! @name Identity
  83. Reference an object
  84. @{
  85. */
  86. /*! get the unique numeric ID -- or "handle" -- of this object.
  87. @return Returns the numeric ID.
  88. The id is provided for you by the simulator upon object creation. You can not change it
  89. and it likely will not be reused by any other object after this object is deleted.
  90. @par Example
  91. @code
  92. new SimObject(example);
  93. echo(example.getId());
  94. > 1752
  95. @endcode
  96. @par Caveat
  97. You can not access the id directly. That is, you can not access `%%object.id`.
  98. If you do set `%%object.id` you will only succeed in creating a dynamic field named
  99. `id` -- an unrelated field to the actual object's id.
  100. @par Example
  101. @code
  102. %example = SimObject();
  103. echo(%example.getId());
  104. > 1753
  105. // warning! this will fail to change the id!
  106. // it will also not warn you as it is legal syntax
  107. %example.id = 50;
  108. echo(%example.getId());
  109. > 1753
  110. echo(%example.id);
  111. > 50
  112. @endcode
  113. @sa getName, setName
  114. */
  115. ConsoleMethodWithDocs(SimObject, getId, ConsoleInt, 2, 2, ())
  116. {
  117. return object->getId();
  118. }
  119. /*! Set the objects name field.
  120. @param newName name for objects
  121. @return no return value
  122. Now the object can be invoked by this name.
  123. This is different than tracking an object by a variable, such as `%%myObject` or `$myObject`.
  124. Only one object can have a specific name. Setting a second object
  125. with this name will remove the name from the former object.
  126. Note not to confuse this with the `internalName` which is a name for grouping purposes.
  127. @par Example
  128. @code
  129. %obj = new SimObject();
  130. %obj.setName("MyName");
  131. // these are now equivalent
  132. %obj.save();
  133. MyName.save();
  134. @endcode
  135. @par Caveat
  136. You can not access the name directly. That is, you can not access `%%object.name`.
  137. If you do set `%%object.name` you will only succeed in creating a dynamic field named
  138. `name` -- an unrelated field to the actual object's name.
  139. @par Example
  140. @code
  141. SimObject("example");
  142. echo(example.getName());
  143. > example
  144. // warning! the field `name` does not exist yet
  145. echo(example.name);
  146. >
  147. // warning! this will fail to change the name!
  148. // it will also not warn you as it is legal syntax
  149. %example.name = "newExample";
  150. echo(%example.getName());
  151. > example
  152. echo(%example.name);
  153. > newExample
  154. @endcode
  155. @see setName, getId
  156. */
  157. ConsoleMethodWithDocs(SimObject, setName, ConsoleVoid, 3, 3, (newName))
  158. {
  159. object->assignName(argv[2]);
  160. }
  161. /*! Returns the name of the object
  162. @return the "global" name
  163. See setName() for a description of the name field.
  164. Note not to confuse this with the `internalName` which is a name for grouping purposes.
  165. @par Example
  166. @code
  167. %example = new SimObject();
  168. %example.setName("myObject");
  169. // now we can reference our object with variables and with its name
  170. %example.getId();
  171. > 160
  172. myObject.getId();
  173. > 160
  174. @endcode
  175. @Caveats
  176. See setName() for caveats.
  177. @see setName, getId
  178. */
  179. ConsoleMethodWithDocs(SimObject, getName, ConsoleString, 2, 2, ())
  180. {
  181. const char *ret = object->getName();
  182. return ret ? ret : "";
  183. }
  184. /*! @} */ // member group Identity
  185. /*! @name Scoping
  186. Manipulate the object's script-defined `Namespace`
  187. @{
  188. */
  189. /*! Returns the `Namespace` of this object as set by the user.
  190. @return The Namespace as set in the object's `class` field.
  191. The class namespace is a a scripting concept that provides a "namespace" in which the engine looks
  192. to find user-defined scripting functions. It can be set, and reset, by the user
  193. by using setClassNamespace(). Alternatively, it can be set directly using the `class` field of the object.
  194. Note that this can easily be confused with getClassName(), which is unrelated, and returns the "true"
  195. engine class name of an object, such as `SimObject`.
  196. See setClassNamespace() for examples.
  197. @see setClassNamespace
  198. */
  199. ConsoleMethodWithDocs(SimObject, getClassNamespace, ConsoleString, 2, 2, ())
  200. {
  201. return object->getClassNamespace();
  202. }
  203. /*! Return the superclass `Namespace` of this object as set by the user.
  204. An object can have a primary and secondary `Namespace` also known as its
  205. `class` and `superclass`. If a user-defined function is not found in the `class`
  206. then the `superclass` is searched.
  207. @see getClassNamespace
  208. */
  209. ConsoleMethodWithDocs(SimObject, getSuperClassNamespace, ConsoleString, 2, 2, ())
  210. {
  211. return object->getSuperClassNamespace();
  212. }
  213. /*! Sets the `Namespace` of this object.
  214. @return no return value
  215. The class namespace is a a scripting concept that provides a "namespace" in which the engine looks
  216. to find user-defined scripting functions. It can be set, and reset, by the user using setClassNamespace().
  217. Alternatively, it can be set directly using the `class` field of the object.
  218. The `Namespace` or `class` can then be returned with getClassNamespace(). Note that this can easily be
  219. confused with getClassName(), which is unrelated, and returns the "true" engine class name of an object,
  220. such as `SimObject`.
  221. @par Example
  222. @code
  223. %example = new SimObject()
  224. {
  225. class = MyScope;
  226. };
  227. echo(%example.class);
  228. > MyScope
  229. // set the namespace using setNamespace()
  230. %example.setClassNamespace(DifferentScope);
  231. echo(%example.class);
  232. > DifferentScope
  233. // set the namespace directly using the field 'class'
  234. %example.class = YetAnotherScope;
  235. echo(%example.getClassNamespace());
  236. > YetAnotherScope
  237. @endcode
  238. @see getClassNamespace
  239. */
  240. ConsoleMethodWithDocs(SimObject, setClassNamespace, ConsoleVoid, 2, 3, (nameSpace))
  241. {
  242. object->setClassNamespace(argv[2]);
  243. }
  244. /*! Sets the superclass `Namespace` of this object.
  245. An object can have a primary and secondary `Namespace` also known as its
  246. `class` and `superclass`. If a user-defined function is not found in the `class`
  247. then the `superclass` is searched.
  248. @see setClassNamespace
  249. */
  250. ConsoleMethodWithDocs(SimObject, setSuperClassNamespace, ConsoleVoid, 2, 3, ())
  251. {
  252. object->setSuperClassNamespace(argv[2]);
  253. }
  254. /*! @} */ // member group Scoping
  255. /*! @name Reflection
  256. Methods to query and manipulate the object's class, methods, and fields.
  257. @{
  258. */
  259. /*! Returns wether the method exists for this object.
  260. @returns true if the method exists; false otherwise
  261. The method must be a "built-in" method, or one that is not user-defined in script.
  262. It must also be a direct method on the object, and not a behavior defined in a Behavior.
  263. */
  264. ConsoleMethodWithDocs(SimObject, isMethod, ConsoleBool, 3, 3, (string methodName))
  265. {
  266. return object->isMethod( argv[2] );
  267. }
  268. /*! Dynamically call a method by a string name
  269. Normally you would call a method in the form `%object.myMethod(param1, param2)`.
  270. Alternatively, you can use `%object.call(myMethod, param1, param2)`. This can be
  271. useful if, for instance, you don't know which method to call in advance.
  272. @par Example
  273. @code
  274. %method = "setClassNamespace";
  275. %newNamespace = "myNamespace";
  276. %object.call(%method, %newNamespace);
  277. @endcode
  278. */
  279. ConsoleMethodWithDocs( SimObject, call, ConsoleString, 2, 0, ( methodName, [args]* ))
  280. {
  281. argv[1] = argv[2];
  282. return Con::execute( object, argc - 1, argv + 1 );
  283. }
  284. /*! Write the class hierarchy of an object to the console.
  285. @return no return value
  286. @par Example
  287. @code
  288. new SimGroup(sg);
  289. echo(sg.dumpClassHierarchy());
  290. > SimGroup ->
  291. > SimSet ->
  292. > SimObject
  293. @endcode
  294. */
  295. ConsoleMethodWithDocs(SimObject, dumpClassHierarchy, ConsoleVoid, 2, 2, ())
  296. {
  297. object->dumpClassHierarchy();
  298. }
  299. /*! dump the object to the console.
  300. Use the dump method to display the following information about this object:
  301. + All static and dynamic fields that are non-null
  302. + All engine and script-registered console methods (including superclass methods) for this object
  303. @return No return value
  304. */
  305. ConsoleMethodWithDocs(SimObject,dump, ConsoleVoid, 2, 2, ())
  306. {
  307. object->dump();
  308. }
  309. /*! returns true if this object is of the specified class or a subclass of the specified class
  310. @return true if a class or subclass of the given class
  311. @par Example
  312. @code
  313. %example = new SceneObject();
  314. echo(%example.isMemberOfClass(SimObject);
  315. > 1
  316. echo(%example.isMemberOfClass(SimSet);
  317. > 0
  318. @endcode
  319. */
  320. ConsoleMethodWithDocs(SimObject, isMemberOfClass, ConsoleBool, 3, 3, (string classname))
  321. {
  322. AbstractClassRep* pRep = object->getClassRep();
  323. while(pRep)
  324. {
  325. if(!dStricmp(pRep->getClassName(), argv[2]))
  326. {
  327. //matches
  328. return true;
  329. }
  330. pRep = pRep->getParentClass();
  331. }
  332. return false;
  333. }
  334. /*! Returns the engine class of this object such as `SimObject` or `SceneObject`
  335. @return class name
  336. Note that this method is defined in SimObject but is inherited by subclasses of SimObject.
  337. Subclasses will return the correct subclass name.
  338. Note also, getClassName() is not related to an object's `class` field! The `class` field
  339. is a scripting concept that provides a "namespace" to look for user-defined functions (see getClassNamespace()).
  340. @par Example
  341. @code
  342. %example = new SimObject()
  343. {
  344. class = MyScope;
  345. };
  346. echo(%example.getClassName());
  347. > SimObject
  348. echo(%example.class);
  349. > MyScope
  350. @endcode
  351. */
  352. ConsoleMethodWithDocs(SimObject, getClassName, ConsoleString, 2, 2, ())
  353. {
  354. const char *ret = object->getClassName();
  355. return ret ? ret : "";
  356. }
  357. /*! Return the value of any field.
  358. This can be a static ("built-in") field or a dynamic ("add-on") field.
  359. Normally, you would get a field directly as `%%object.field`.
  360. However, in some cases you may want to use getFieldValue(). For instance,
  361. suppose you allow the field name to be passed into a function. You can still
  362. get that field with `%%object.getFieldValue(%%field)`.
  363. @param fieldName the name of the field
  364. @return the value of the field
  365. @par Example
  366. @code
  367. // create a SimObject and set its 'class' field for our example
  368. %example = new SimObject()
  369. {
  370. class = "MyClass";
  371. }
  372. // 'class' is a static "built-in" field. retrieve it directly and with getFieldValue()
  373. echo(%example.class);
  374. > MyClass
  375. echo(%example.getFieldValue(class));
  376. > MyClass
  377. // set a dynamic "add-on" field
  378. %example.myField = "myValue";
  379. echo(%example.myField);
  380. > myValue
  381. echo(%example.getFieldValue(myField));
  382. > myValue
  383. @endcode
  384. */
  385. ConsoleMethodWithDocs(SimObject, getFieldValue, ConsoleString, 3, 3, (fieldName))
  386. {
  387. const char *fieldName = StringTable->insert( argv[2] );
  388. return object->getDataField( fieldName, NULL );
  389. }
  390. /*! Set the value of any field.
  391. This can be a static ("built-in") field or a dynamic ("add-on") field.
  392. Normally, you would set a field directly as `%%object.field = value`.
  393. However, in some cases you may want to use setFieldValue(). For instance,
  394. suppose you allow the field name to be passed into a function. You can still
  395. set that field with `%%object.setFieldValue(%field, "myValue")`.
  396. @param fieldName the name of the field to set
  397. @param value the value to set
  398. @return always returns true
  399. @par Example
  400. @code
  401. // create a SimObject
  402. %example = new SimObject();
  403. // 'class' is a static "built-in" field. set it directly and with setFieldValue()
  404. echo(%example.class);
  405. >
  406. %example.class = "MyClass";
  407. echo(%example.class);
  408. > MyClass
  409. %example.setFieldValue(class, "AnotherClass");
  410. echo(%example.class);
  411. > AnotherClass
  412. // set a dynamic "add-on" field
  413. echo(%example.myField);
  414. >
  415. %example.myField = "myValue";
  416. echo(%example.myField);
  417. > myValue
  418. %example.setFieldValue(anotherField, "anotherValue");
  419. echo(%example.anotherField);
  420. > anotherValue
  421. @endcode
  422. */
  423. ConsoleMethodWithDocs(SimObject, setFieldValue, ConsoleBool, 4, 4, (fieldName,value))
  424. {
  425. const char *fieldName = StringTable->insert(argv[2]);
  426. const char *value = argv[3];
  427. object->setDataField( fieldName, NULL, value );
  428. return true;
  429. }
  430. /*! return the number of dynamic ("add-on") fields.
  431. @return the number of dynamic fields
  432. Note that static (or "built-in") fields are not counted. For instance,
  433. `SimObject.class` will not count.
  434. See getDynamicField() for an explanation and examples.
  435. @see getDynamicField, getField, getFieldCount
  436. */
  437. ConsoleMethodWithDocs(SimObject, getDynamicFieldCount, ConsoleInt, 2, 2, ())
  438. {
  439. S32 count = 0;
  440. SimFieldDictionary* fieldDictionary = object->getFieldDictionary();
  441. for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
  442. count++;
  443. return count;
  444. }
  445. /*! Return the field name of a specific dynamic ("add-on") field by index.
  446. @param index the dynamic field for which to retrieve the name
  447. @return the name of the field
  448. You would normally access dynamic fields directly `%%object.field` or
  449. indirectly `%%object.getFieldValue(%%field)`. However, you may not know the
  450. field's names or otherwise need to iterate over the fields. Use getDynamicFieldCount()
  451. to get the number of dynamic fields, and then iterate over them with this function.
  452. Note that only dynamic ("add-on") fields will be surfaced. Static ("built-in") fields
  453. like `SimSet.class` will not be counted or listed.
  454. While static and dynamic fields have separate functions to get their counts and names, they
  455. share getFieldValue() and setFieldValue() to read and set any field by name.
  456. Also note that the order of the fields by an index has no meaning. It is not alphabetical,
  457. the order created, or otherwise.
  458. @par Example
  459. @code
  460. %count = %example.getDynamicFieldCount();
  461. for (%i = 0; %i < %count; %i++)
  462. {
  463. %fieldName = %example.getDynamicField(%i);
  464. %fieldValue = %example.getFieldValue(%fieldName);
  465. echo(%fieldName @ " = " @ %fieldValue);
  466. }
  467. @endcode
  468. @see getDynamicFieldCount, getField, getFieldCount
  469. */
  470. ConsoleMethodWithDocs(SimObject, getDynamicField, ConsoleString, 3, 3, (index))
  471. {
  472. SimFieldDictionary* fieldDictionary = object->getFieldDictionary();
  473. SimFieldDictionaryIterator itr(fieldDictionary);
  474. S32 index = dAtoi(argv[2]);
  475. for (S32 i = 0; i < index; i++)
  476. {
  477. if (!(*itr))
  478. {
  479. Con::warnf("Invalid dynamic field index passed to SimObject::getDynamicField!");
  480. return NULL;
  481. }
  482. ++itr;
  483. }
  484. char* buffer = Con::getReturnBuffer(256);
  485. if (*itr)
  486. {
  487. SimFieldDictionary::Entry* entry = *itr;
  488. dSprintf(buffer, 256, "%s", entry->slotName);
  489. return buffer;
  490. }
  491. Con::warnf("Invalid dynamic field index passed to SimObject::getDynamicField!");
  492. return NULL;
  493. }
  494. /*! return the number of static ("built-in") fields.
  495. @return the number of dynamic fields
  496. Note that dynamic (or "add-on") fields are not counted. For instance,
  497. `%%object.class` will count, but `%%object.myField` will not.
  498. See getField() for an explanation and examples.
  499. @see getDynamicField, getDynamicFieldCount, getField
  500. */
  501. ConsoleMethodWithDocs( SimObject, getFieldCount, ConsoleInt, 2, 2, ())
  502. {
  503. const AbstractClassRep::FieldList &list = object->getFieldList();
  504. const AbstractClassRep::Field* f;
  505. U32 numDummyEntries = 0;
  506. for(int i = 0; i < list.size(); i++)
  507. {
  508. f = &list[i];
  509. if( f->type == AbstractClassRep::DepricatedFieldType ||
  510. f->type == AbstractClassRep::StartGroupFieldType ||
  511. f->type == AbstractClassRep::EndGroupFieldType )
  512. {
  513. numDummyEntries++;
  514. }
  515. }
  516. return list.size() - numDummyEntries;
  517. }
  518. /*! Return the field name of a specific static ("built-in") field by index.
  519. @param index the static field for which to retrieve the name
  520. @return the name of the field
  521. You would normally access static fields directly `%%object.class` or
  522. indirectly `%%object.getFieldValue(%%field)`. However, you may not know the
  523. field's names or otherwise need to iterate over the fields. Use getFieldCount()
  524. to get the number of static fields, and then iterate over them with this function.
  525. Note that only static ("built-in") fields will be surfaced. Dynamic ("add-on") fields
  526. like `%%SimSet.myField` will not be counted or listed.
  527. While static and dynamic fields have separate functions to get their counts and names, they
  528. share getFieldValue() and setFieldValue() to read and set any field by name.
  529. Also note that the order of the fields by an index has no meaning. It is not alphabetical,
  530. the order created, or otherwise.
  531. @par Example
  532. @code
  533. %count = %example.getFieldCount();
  534. for (%i = 0; %i < %count; %i++)
  535. {
  536. %fieldName = %example.getField(%i);
  537. %fieldValue = %example.getFieldValue(%fieldName);
  538. echo(%fieldName @ " = " @ %fieldValue);
  539. }
  540. @endcode
  541. @see getDynamicField, getDynamicFieldCount, getFieldCount
  542. */
  543. ConsoleMethodWithDocs( SimObject, getField, ConsoleString, 3, 3, (int index))
  544. {
  545. S32 index = dAtoi( argv[2] );
  546. const AbstractClassRep::FieldList &list = object->getFieldList();
  547. if( ( index < 0 ) || ( index >= list.size() ) )
  548. return "";
  549. const AbstractClassRep::Field* f;
  550. S32 currentField = 0;
  551. for(int i = 0; i < list.size() && currentField <= index; i++)
  552. {
  553. f = &list[i];
  554. // skip any dummy fields
  555. if(f->type == AbstractClassRep::DepricatedFieldType ||
  556. f->type == AbstractClassRep::StartGroupFieldType ||
  557. f->type == AbstractClassRep::EndGroupFieldType)
  558. {
  559. continue;
  560. }
  561. if(currentField == index)
  562. return f->pFieldname;
  563. currentField++;
  564. }
  565. // if we found nada, return nada.
  566. return "";
  567. }
  568. /*! Sets the progenitor file responsible for this instances creation.
  569. @param file The progenitor file responsible for this instances creation.
  570. @return No return value.
  571. */
  572. ConsoleMethodWithDocs(SimObject, setProgenitorFile, ConsoleVoid, 3, 3, (file))
  573. {
  574. object->setProgenitorFile( argv[2] );
  575. }
  576. //-----------------------------------------------------------------------------
  577. /*! Gets the progenitor file responsible for this instances creation.
  578. @return The progenitor file responsible for this instances creation.
  579. */
  580. ConsoleMethodWithDocs(SimObject, getProgenitorFile, ConsoleString, 2, 2, ())
  581. {
  582. return object->getProgenitorFile();
  583. }
  584. /*! Use the getType method to get the type for this object.
  585. @return Returns a bit mask containing one or more set bits.
  586. This is here for legacy purposes.
  587. This type is an integer value composed of bitmasks. For simplicity, these bitmasks
  588. are defined in the engine and exposed for our use as global variables.
  589. To simplify the writing of scripts, a set of globals has been provided containing
  590. the bit setting for each class corresponding to a particular type.
  591. @sa getClassName
  592. */
  593. ConsoleMethodWithDocs(SimObject, getType, ConsoleInt, 2, 2, ())
  594. {
  595. return((S32)object->getType());
  596. }
  597. /*! @} */ // member group Reflection
  598. /*!
  599. */
  600. ConsoleMethodWithDocs(SimObject, getFieldType, ConsoleString, 3, 3, (fieldName))
  601. {
  602. const char *fieldName = StringTable->insert( argv[2] );
  603. U32 typeID = object->getDataFieldType( fieldName, NULL );
  604. ConsoleBaseType* type = ConsoleBaseType::getType( typeID );
  605. if( type )
  606. return type->getTypeClassName();
  607. return "";
  608. }
  609. /*! @name Grouping
  610. Manipulate the (singular) group for this object.
  611. @{
  612. */
  613. //-----------------------------------------------------------------------------
  614. // Set the internal name, can be used to find child objects
  615. // in a meaningful way, usually from script, while keeping
  616. // common script functionality together using the controls "Name" field.
  617. //-----------------------------------------------------------------------------
  618. /*!
  619. */
  620. ConsoleMethodWithDocs( SimObject, setInternalName, ConsoleVoid, 3, 3, (string InternalName))
  621. {
  622. object->setInternalName(argv[2]);
  623. }
  624. /*! returns the objects internal name
  625. */
  626. ConsoleMethodWithDocs( SimObject, getInternalName, ConsoleString, 2, 2, ())
  627. {
  628. return object->getInternalName();
  629. }
  630. /*! returns true, if we are in the specified simgroup - or a subgroup thereof
  631. */
  632. ConsoleMethodWithDocs(SimObject, isChildOfGroup, ConsoleBool, 3,3, ())
  633. {
  634. SimGroup* pGroup = dynamic_cast<SimGroup*>(Sim::findObject(dAtoi(argv[2])));
  635. if(pGroup)
  636. {
  637. return object->isChildOfGroup(pGroup);
  638. }
  639. return false;
  640. }
  641. /*! Use the getGroup method to determine if this object is contained in a SimGroup and if so, which one.
  642. @return Returns the ID of the SimGroup this shape is in or zero if the shape is not contained in a SimGroup
  643. */
  644. ConsoleMethodWithDocs(SimObject, getGroup, ConsoleInt, 2, 2, ())
  645. {
  646. SimGroup *grp = object->getGroup();
  647. if(!grp)
  648. return -1;
  649. return grp->getId();
  650. }
  651. /*! @} */ // member group Grouping
  652. /*! Use the delete method to delete this object.
  653. When an object is deleted, it automatically
  654. + Unregisters its ID and name (if it has one) with the engine.
  655. + Removes itself from any SimGroup or SimSet it may be a member of.
  656. + (eventually) returns the memory associated with itself and its non-dynamic members.
  657. + Cancels all pending %obj.schedule() events.
  658. For objects in the GameBase, ScriptObject, or GUIControl hierarchies, an object will first: Call the onRemove() method for the object's namespace
  659. @return No return value.
  660. */
  661. ConsoleMethodWithDocs(SimObject, delete, ConsoleVoid, 2, 2, ())
  662. {
  663. object->deleteObject();
  664. }
  665. /*! Clones the object.
  666. @param copyDynamicFields Whether the dynamic fields should be copied to the cloned object or not. Optional: Defaults to false.
  667. @return (newObjectID) The newly cloned object's id if successful, otherwise a 0.
  668. */
  669. ConsoleMethodWithDocs(SimObject, clone, ConsoleInt, 2, 3, ([copyDynamicFields = false]?))
  670. {
  671. // Fetch copy dynamic fields flag.
  672. const bool copyDynamicFields = ( argc >= 3 ) ? dAtob( argv[2] ) : false;
  673. // Clone Object.
  674. SimObject* pClonedObject = object->clone( copyDynamicFields );
  675. // Finish if object was not cloned.
  676. if ( pClonedObject == NULL )
  677. return 0;
  678. return pClonedObject->getId();
  679. }
  680. /*! @name Timer/Scheduled Events
  681. Perform timed callbacks on the object.
  682. @{
  683. */
  684. /*! Starts a periodic timer for this object.
  685. Sets a timer on the object that, when it expires, will cause the object to execute the onTimer() callback.
  686. The timer event will continue to occur at regular intervals until setTimerOff() is called.
  687. @param callbackFunction The name of the callback function to call for each timer repetition.
  688. @param timePeriod The period of time (in milliseconds) between each callback.
  689. @param repeat The number of times the timer should repeat. If not specified or zero then it will run infinitely
  690. @return No return Value.
  691. */
  692. ConsoleMethodWithDocs(SimObject, startTimer, ConsoleBool, 4, 5, (callbackFunction, float timePeriod, [repeat]?))
  693. {
  694. // Is the periodic timer running?
  695. if ( object->getPeriodicTimerID() != 0 )
  696. {
  697. // Yes, so cancel it.
  698. Sim::cancelEvent( object->getPeriodicTimerID() );
  699. // Reset Timer ID.
  700. object->setPeriodicTimerID( 0 );
  701. }
  702. // Fetch the callback function.
  703. StringTableEntry callbackFunction = StringTable->insert( argv[2] );
  704. // Does the function exist?
  705. if ( !object->isMethod( callbackFunction ) )
  706. {
  707. // No, so warn.
  708. Con::warnf("SimObject::startTimer() - The callback function of '%s' does not exist.", callbackFunction );
  709. return false;
  710. }
  711. // Fetch the time period.
  712. const S32 timePeriod = dAtoi(argv[3]);
  713. // Is the time period valid?
  714. if ( timePeriod < 1 )
  715. {
  716. // No, so warn.
  717. Con::warnf("SimObject::startTimer() - The time period of '%d' is invalid.", timePeriod );
  718. return false;
  719. }
  720. // Fetch the repeat count.
  721. const S32 repeat = argc >= 5 ? dAtoi(argv[4]) : 0;
  722. // Create Timer Event.
  723. SimObjectTimerEvent* pEvent = new SimObjectTimerEvent( callbackFunction, (U32)timePeriod, (U32)repeat );
  724. // Post Event.
  725. object->setPeriodicTimerID( Sim::postEvent( object, pEvent, Sim::getCurrentTime() + timePeriod ) );
  726. return true;
  727. }
  728. //-----------------------------------------------------------------------------
  729. /*! Stops the periodic timer for this object.
  730. @return No return Value.
  731. */
  732. ConsoleMethodWithDocs(SimObject, stopTimer, ConsoleVoid, 2, 2, ())
  733. {
  734. // Finish if the periodic timer isn't running.
  735. if ( object->getPeriodicTimerID() == 0 )
  736. return;
  737. // Cancel It.
  738. Sim::cancelEvent( object->getPeriodicTimerID() );
  739. // Reset Timer ID.
  740. object->setPeriodicTimerID( 0 );
  741. }
  742. //-----------------------------------------------------------------------------
  743. /*! Checks whether the periodic timer is active for this object or not.
  744. @return Whether the periodic timer is active for this object or not.
  745. */
  746. ConsoleMethodWithDocs(SimObject, isTimerActive, ConsoleBool, 2, 2, ())
  747. {
  748. return object->isPeriodicTimerActive();
  749. }
  750. /*! schedule an action to be executed upon this object in the future.
  751. @param time Time in milliseconds till action is scheduled to occur.
  752. @param command Name of the command to execute. This command must be scoped to this object
  753. (i.e. It must exist in the namespace of the object), otherwise the schedule call will fail.
  754. @param arg1...argN These are optional arguments which will be passed to the command.
  755. This version of schedule automatically passes the ID of %obj as arg0 to command.
  756. @return Returns an integer schedule ID.
  757. The major difference between this and the ::schedule() console function is that if this object is deleted prior
  758. to the scheduled event, the event is automatically canceled. Times should not be treated as exact since some
  759. 'simulation delay' is to be expected. The minimum resolution for a scheduled event is 32 ms, or one tick.
  760. The existence of command is not validated. If you pass an invalid console method name, the
  761. schedule() method will still return a schedule ID, but the subsequent event will fail silently.
  762. To manipulate the scheduled event, use the id returned with the system schedule functions.
  763. @see ::schedule
  764. */
  765. ConsoleMethodWithDocs(SimObject,schedule, ConsoleInt, 4, 0, (time , command , [arg]* ))
  766. {
  767. U32 timeDelta = U32(dAtof(argv[2]));
  768. argv[2] = argv[3];
  769. argv[3] = argv[1];
  770. SimConsoleEvent *evt = new SimConsoleEvent(argc - 2, argv + 2, true);
  771. S32 ret = Sim::postEvent(object, evt, Sim::getCurrentTime() + timeDelta);
  772. // #ifdef DEBUG
  773. // Con::printf("obj %s schedule(%s) = %d", argv[3], argv[2], ret);
  774. // Con::executef(1, "backtrace");
  775. // #endif
  776. return ret;
  777. }
  778. /*! @} */ // member group Timer Events
  779. ConsoleMethodRootGroupEndWithDocs(SimObject)