simObject_ScriptBinding.h 60 KB

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