simBase_ScriptBinding.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #ifndef _SIMBASE_H_
  23. #include "simBase.h"
  24. #endif
  25. //-----------------------------------------------------------------------------
  26. /*!
  27. @defgroup EventScheduleFunctions Event Scheduling
  28. @ingroup TorqueScriptFunctions
  29. @{
  30. */
  31. //-----------------------------------------------------------------------------
  32. /*! get the time, in ticks, that has elapsed since the engine started executing.
  33. @return the time in ticks since the engine was started.
  34. @sa getRealTime
  35. @boundto
  36. Sim::getCurrentTime
  37. */
  38. ConsoleFunctionWithDocs( getSimTime, ConsoleInt, 1, 1, () )
  39. {
  40. return Sim::getCurrentTime();
  41. }
  42. /*! cancel a previously scheduled event
  43. @param eventID The numeric ID of a previously scheduled event.
  44. @return No return value.
  45. @sa getEventTimeLeft, getScheduleDuration, getTimeSinceStart, isEventPending, schedule, SimObject::schedule
  46. @boundto
  47. Sim::cancelEvent
  48. */
  49. ConsoleFunctionWithDocs(cancel,ConsoleVoid,2,2, ( eventID ) )
  50. {
  51. Sim::cancelEvent(dAtoi(argv[1]));
  52. }
  53. /*! See if the event associated with eventID is still pending.
  54. When an event passes, the eventID is removed from the event queue, becoming invalid, so there is no discnerable difference between a completed event and a bad event ID.
  55. @param eventID The numeric ID of a previously scheduled event.
  56. @return true if this event is still outstanding and false if it has passed or eventID is invalid.
  57. @par Example
  58. @code
  59. $Game::Schedule = schedule($Game::EndGamePause * 1000, 0, "onCyclePauseEnd");
  60. if( isEventPending($Game::Schedule) ) echo("got a pending event");
  61. @endcode
  62. @sa cancel, getEventTimeLeft, getScheduleDuration, getTimeSinceStart, schedule, SimObject::schedule
  63. @boundto
  64. Sim::isEventPending
  65. */
  66. ConsoleFunctionWithDocs(isEventPending, ConsoleBool, 2, 2, ( eventID ) )
  67. {
  68. return Sim::isEventPending(dAtoi(argv[1]));
  69. }
  70. /*!
  71. Determines how much time remains until the event specified by eventID occurs.
  72. @param eventID The numeric ID of a previously scheduled event.
  73. @return a non-zero integer value equal to the milliseconds until the event specified by eventID will occur. However, if eventID is invalid, or the event has passed, this function will return zero.
  74. @sa cancel, getScheduleDuration, getTimeSinceStart, isEventPending, schedule, SimObject::schedule
  75. @boundto
  76. Sim::getEventTimeLeft
  77. */
  78. ConsoleFunctionWithDocs(getEventTimeLeft, ConsoleInt, 2, 2, ( eventID ) )
  79. {
  80. return Sim::getEventTimeLeft(dAtoi(argv[1]));
  81. }
  82. /*!
  83. Determines how long the event associated with eventID was scheduled for.
  84. @param eventID The numeric ID of a previously scheduled event.
  85. @return a non-zero integer value equal to the milliseconds used in the schedule call that created this event. However, if eventID is invalid, this function will return zero.
  86. @sa cancel, getEventTimeLeft, getTimeSinceStart, isEventPending, schedule, SimObject::schedule
  87. @boundto
  88. Sim::getScheduleDuration
  89. */
  90. ConsoleFunctionWithDocs(getScheduleDuration, ConsoleInt, 2, 2, ( eventID ) )
  91. {
  92. S32 ret = Sim::getScheduleDuration(dAtoi(argv[1]));
  93. return ret;
  94. }
  95. /*!
  96. Determines how much time has passed since the event specified by eventID was scheduled.
  97. @param eventID The numeric ID of a previously scheduled event.
  98. @return a non-zero integer value equal to the milliseconds that have passed since this event was scheduled. However, if eventID is invalid, or the event has passed, this function will return zero.
  99. @sa cancel, getEventTimeLeft, getScheduleDuration, isEventPending, schedule, SimObject::schedule
  100. @boundto
  101. Sim::getTimeSinceStart
  102. */
  103. ConsoleFunctionWithDocs(getTimeSinceStart, ConsoleInt, 2, 2, ( eventID ) )
  104. {
  105. S32 ret = Sim::getTimeSinceStart(dAtoi(argv[1]));
  106. return ret;
  107. }
  108. /*!
  109. Schedule "functionName" to be executed with optional arguments at time t (specified in milliseconds) in the future.
  110. This function may be associated with an object ID or not. If it is associated with an object ID and the object is deleted prior to this event occurring, the event is automatically canceled.
  111. @param t The time to wait (in milliseconds) before executing functionName.
  112. @param objID An optional ID to associate this event with.
  113. @param functionName An unadorned (flat) function name.
  114. @param arg0, .. , argN - Any number of optional arguments to be passed to functionName.
  115. @return a non-zero integer representing the event ID for the scheduled event.
  116. @par Example
  117. @code
  118. $Game::Schedule = schedule($Game::EndGamePause * 1000, 0, "onCyclePauseEnd");
  119. @endcode
  120. @sa cancel, getEventTimeLeft, getScheduleDuration, getTimeSinceStart, isEventPending, SimObject::schedule
  121. @boundto
  122. Sim::postEvent
  123. */
  124. ConsoleFunctionWithDocs(schedule, ConsoleInt, 4, 0, ( t , objID || 0 , functionName, arg0, ... , argN ) )
  125. {
  126. U32 timeDelta = U32(dAtof(argv[1]));
  127. SimObject *refObject = Sim::findObject(argv[2]);
  128. if(!refObject)
  129. {
  130. if(argv[2][0] != '0')
  131. return 0;
  132. refObject = Sim::getRootGroup();
  133. }
  134. SimConsoleEvent *evt = new SimConsoleEvent(argc - 3, argv + 3, false);
  135. S32 ret = Sim::postEvent(refObject, evt, Sim::getCurrentTime() + timeDelta);
  136. // #ifdef DEBUG
  137. // Con::printf("ref %s schedule(%s) = %d", argv[2], argv[3], ret);
  138. // Con::executef(1, "backtrace");
  139. // #endif
  140. return ret;
  141. }
  142. //-----------------------------------------------------------------------------
  143. /*! @} */
  144. /*!
  145. @defgroup ObjectFunctions Object
  146. @ingroup TorqueScriptFunctions
  147. @{
  148. */
  149. //-----------------------------------------------------------------------------
  150. /*! Use the nameToID function to convert an object name into an object ID.
  151. Helper function for those odd cases where a string will not covert properly, but generally this can be replaced with a statement like: ("someName")
  152. @param objectName A string containing the name of an object.
  153. @return a positive non-zero value if the name corresponds to an object, or a -1 if it does not.
  154. @boundto
  155. Sim::findObject
  156. */
  157. ConsoleFunctionWithDocs( nameToID, ConsoleInt, 2, 2, ( string objectName ) )
  158. {
  159. SimObject *obj = Sim::findObject(argv[1]);
  160. if(obj)
  161. return obj->getId();
  162. else
  163. return -1;
  164. }
  165. /*! check if the name or ID specified is a valid object.
  166. @param handle A name or ID of a possible object.
  167. @return true if handle refers to a valid object, false otherwise
  168. @boundto
  169. Sim::findObject
  170. */
  171. ConsoleFunctionWithDocs(isObject, ConsoleBool, 2, 2, ( handle ) )
  172. {
  173. if (!dStrcmp(argv[1], "0") || !dStrcmp(argv[1], ""))
  174. return false;
  175. else
  176. return (Sim::findObject(argv[1]) != NULL);
  177. }
  178. //-----------------------------------------------------------------------------
  179. /*! @} */