/xbmc/visualizations/Vortex/angelscript/docs/doxygen/source/doc_addon.h

http://github.com/xbmc/xbmc · C++ Header · 1083 lines · 0 code · 0 blank · 1083 comment · 0 complexity · 0acaf18b428fc6a7009258372d3ae7a8 MD5 · raw file

  1. /**
  2. \page doc_addon Add-ons
  3. This page gives a brief description of the add-ons that you'll find in the /sdk/add_on/ folder.
  4. - \subpage doc_addon_application
  5. - \subpage doc_addon_script
  6. \page doc_addon_application Application modules
  7. - \subpage doc_addon_build
  8. - \subpage doc_addon_ctxmgr
  9. - \subpage doc_addon_autowrap
  10. - \subpage doc_addon_helpers
  11. - \subpage doc_addon_clib
  12. \page doc_addon_script Script extensions
  13. - \subpage doc_addon_std_string
  14. - \subpage doc_addon_string
  15. - \subpage doc_addon_array
  16. - \subpage doc_addon_any
  17. - \subpage doc_addon_dict
  18. - \subpage doc_addon_file
  19. - \subpage doc_addon_math
  20. - \subpage doc_addon_math3d
  21. \page doc_addon_ctxmgr Context manager
  22. <b>Path:</b> /sdk/add_on/contextmgr/
  23. The <code>CContextMgr</code> is a class designed to aid the management of multiple simultaneous
  24. scripts executing in parallel. It supports both \ref doc_adv_concurrent "concurrent script threads" and \ref doc_adv_coroutine "co-routines".
  25. If the application doesn't need multiple contexts, i.e. all scripts that are executed
  26. always complete before the next script is executed, then this class is not necessary.
  27. Multiple context managers can be used, for example when you have a group of scripts controlling
  28. ingame objects, and another group of scripts controlling GUI elements, then each of these groups
  29. may be managed by different context managers.
  30. Observe, that the context manager class hasn't been designed for multithreading, so you need to
  31. be careful if your application needs to execute scripts from multiple threads.
  32. \see The samples \ref doc_samples_concurrent and \ref doc_samples_corout for uses
  33. \section doc_addon_ctxmgr_1 Public C++ interface
  34. \code
  35. class CContextMgr
  36. {
  37. public:
  38. CContextMgr();
  39. ~CContextMgr();
  40. // Set the function that the manager will use to obtain the time in milliseconds.
  41. void SetGetTimeCallback(TIMEFUNC_t func);
  42. // Registers the script function
  43. //
  44. // void sleep(uint milliseconds)
  45. //
  46. // The application must set the get time callback for this to work
  47. void RegisterThreadSupport(asIScriptEngine *engine);
  48. // Registers the script functions
  49. //
  50. // void createCoRoutine(const string &in functionName, any @arg)
  51. // void yield()
  52. void RegisterCoRoutineSupport(asIScriptEngine *engine);
  53. // Create a new context, prepare it with the function id, then return
  54. // it so that the application can pass the argument values. The context
  55. // will be released by the manager after the execution has completed.
  56. asIScriptContext *AddContext(asIScriptEngine *engine, int funcId);
  57. // Create a new context, prepare it with the function id, then return
  58. // it so that the application can pass the argument values. The context
  59. // will be added as a co-routine in the same thread as the currCtx.
  60. asIScriptContext *AddContextForCoRoutine(asIScriptContext *currCtx, int funcId);
  61. // Execute each script that is not currently sleeping. The function returns after
  62. // each script has been executed once. The application should call this function
  63. // for each iteration of the message pump, or game loop, or whatever.
  64. void ExecuteScripts();
  65. // Put a script to sleep for a while
  66. void SetSleeping(asIScriptContext *ctx, asUINT milliSeconds);
  67. // Switch the execution to the next co-routine in the group.
  68. // Returns true if the switch was successful.
  69. void NextCoRoutine();
  70. // Abort all scripts
  71. void AbortAll();
  72. };
  73. \endcode
  74. \page doc_addon_array array template object
  75. <b>Path:</b> /sdk/add_on/scriptarray/
  76. The <code>array</code> type is a \ref doc_adv_template "template object" that allow the scripts to declare arrays of any type.
  77. Since it is a generic class it is not the most performatic due to the need to determine characteristics at
  78. runtime. For that reason it is recommended that the application registers a \ref doc_adv_template_2 "template specialization" for the
  79. array types that are most commonly used.
  80. The type is registered with <code>RegisterScriptArray(asIScriptEngine*)</code>.
  81. \section doc_addon_array_1 Public C++ interface
  82. \code
  83. class CScriptArray
  84. {
  85. public:
  86. // Constructor
  87. CScriptArray(asUINT length, asIObjectType *ot);
  88. virtual ~CScriptArray();
  89. // Memory management
  90. void AddRef();
  91. void Release();
  92. // Type information
  93. asIObjectType *GetArrayObjectType() const;
  94. int GetArrayTypeId() const;
  95. int GetElementTypeId() const;
  96. // Get the current size
  97. asUINT GetSize();
  98. // Resize the array
  99. void Resize(asUINT numElements);
  100. // Get a pointer to an element. Returns 0 if out of bounds
  101. void *At(asUINT index);
  102. // Copy the contents of one array to another (only if the types are the same)
  103. CScriptArray &operator=(const CScriptArray&);
  104. };
  105. \endcode
  106. \section doc_addon_array_2 Public script interface
  107. <pre>
  108. class array<class T>
  109. {
  110. array();
  111. array(uint length);
  112. // Access elements
  113. // T & operator [] (uint)
  114. // const T & operator [] (uint) const
  115. array<T> opAssign(const array<T> & in);
  116. uint length();
  117. void resize(uint);
  118. }
  119. </pre>
  120. \section doc_addon_array_3 Script example
  121. <pre>
  122. int main()
  123. {
  124. array<int> arr(3);
  125. arr[0] = 1;
  126. arr[1] = 2;
  127. arr[2] = 3;
  128. int sum = 0;
  129. for( uint n = 0; n < arr.length(); n++ )
  130. sum += arr[n];
  131. return sum;
  132. }
  133. </pre>
  134. \page doc_addon_any any object
  135. <b>Path:</b> /sdk/add_on/scriptany/
  136. The <code>any</code> type is a generic container that can hold any value. It is a reference type.
  137. The type is registered with <code>RegisterScriptAny(asIScriptEngine*)</code>.
  138. \section doc_addon_any_1 Public C++ interface
  139. \code
  140. class CScriptAny
  141. {
  142. public:
  143. // Constructors
  144. CScriptAny(asIScriptEngine *engine);
  145. CScriptAny(void *ref, int refTypeId, asIScriptEngine *engine);
  146. // Memory management
  147. int AddRef();
  148. int Release();
  149. // Copy the stored value from another any object
  150. CScriptAny &operator=(const CScriptAny&);
  151. int CopyFrom(const CScriptAny *other);
  152. // Store the value, either as variable type, integer number, or real number
  153. void Store(void *ref, int refTypeId);
  154. void Store(asINT64 &value);
  155. void Store(double &value);
  156. // Retrieve the stored value, either as variable type, integer number, or real number
  157. bool Retrieve(void *ref, int refTypeId) const;
  158. bool Retrieve(asINT64 &value) const;
  159. bool Retrieve(double &value) const;
  160. // Get the type id of the stored value
  161. int GetTypeId() const;
  162. };
  163. \endcode
  164. \section doc_addon_any_2 Public script interface
  165. <pre>
  166. class any
  167. {
  168. any();
  169. any(? &in value);
  170. void store(? &in value);
  171. void store(int64 &in value);
  172. void store(double &in value);
  173. bool retrieve(? &out value) const;
  174. bool retrieve(int64 &out value) const;
  175. bool retrieve(double &out value) const;
  176. }
  177. </pre>
  178. \section doc_addon_any_3 Example usage
  179. In the scripts it can be used as follows:
  180. <pre>
  181. int value;
  182. obj object;
  183. obj \@handle;
  184. any a,b,c;
  185. a.store(value); // store the value
  186. b.store(\@handle); // store an object handle
  187. c.store(object); // store a copy of the object
  188. a.retrieve(value); // retrieve the value
  189. b.retrieve(\@handle); // retrieve the object handle
  190. c.retrieve(object); // retrieve a copy of the object
  191. </pre>
  192. In C++ the type can be used as follows:
  193. \code
  194. CScriptAny *myAny;
  195. int typeId = engine->GetTypeIdByDecl("string@");
  196. CScriptString *str = new CScriptString("hello world");
  197. myAny->Store((void*)&str, typeId);
  198. myAny->Retrieve((void*)&str, typeId);
  199. \endcode
  200. \page doc_addon_std_string string object (STL)
  201. <b>Path:</b> /sdk/add_on/scriptstdstring/
  202. This add-on registers the <code>std::string</code> type as-is with AngelScript. This gives
  203. perfect compatibility with C++ functions that use <code>std::string</code> in parameters or
  204. as return type.
  205. A potential drawback is that the <code>std::string</code> type is a value type, thus may
  206. increase the number of copies taken when string values are being passed around
  207. in the script code. However, this is most likely only a problem for scripts
  208. that perform a lot of string operations.
  209. Register the type with <code>RegisterStdString(asIScriptEngine*)</code>.
  210. \see \ref doc_addon_string
  211. \section doc_addon_std_string_1 Public C++ interface
  212. Refer to the <code>std::string</code> implementation for your compiler.
  213. \section doc_addon_std_string_2 Public script interface
  214. <pre>
  215. class string
  216. {
  217. // Constructors
  218. string();
  219. // Returns the length of the string
  220. uint length() const;
  221. // Assignment and concatenation
  222. string &opAssign(const string &in other);
  223. string &opAddAssign(const string &in other);
  224. string opAdd(const string &in right) const;
  225. // Access individual characters
  226. // uint8 &operator [] (uint)
  227. // const uint8 &operator [] (uint) const
  228. // Comparison operators
  229. bool opEquals(const string &in right) const;
  230. int opCmp(const string &in right) const;
  231. // Automatic conversion from number types to string type
  232. string &opAssign(double val);
  233. string &opAddAssign(double val);
  234. string opAdd(double val) const;
  235. string opAdd_r(double val) const;
  236. string &opAssign(int val);
  237. string &opAddAssign(int val);
  238. string opAdd(int val) const;
  239. string opAdd_r(int val) const;
  240. string &opAssign(uint val);
  241. string &opAddAssign(uint val);
  242. string opAdd(uint val) const;
  243. string opAdd_r(uint val) const;
  244. }
  245. </pre>
  246. \page doc_addon_string string object (reference counted)
  247. <b>Path:</b> /sdk/add_on/scriptstring/
  248. This add-on registers a string type that is in most situations compatible with the
  249. <code>std::string</code>, except that it uses reference counting. This means that if you have an
  250. application function that takes a <code>std::string</code> by reference, you can register it
  251. with AngelScript to take a script string by reference. This works because the CScriptString
  252. wraps the <code>std::string</code> type, with the std::string type at the first byte of the CScriptString
  253. object.
  254. Register the type with <code>RegisterScriptString(asIScriptEngine*)</code>. Register the
  255. utility functions with <code>RegisterScriptStringUtils(asIScriptEngine*)</code>.
  256. \see \ref doc_addon_std_string
  257. \section doc_addon_string_1 Public C++ interface
  258. \code
  259. class CScriptString
  260. {
  261. public:
  262. // Constructors
  263. CScriptString();
  264. CScriptString(const CScriptString &other);
  265. CScriptString(const char *s);
  266. CScriptString(const std::string &s);
  267. // Memory management
  268. void AddRef();
  269. void Release();
  270. // Assignment
  271. CScriptString &operator=(const CScriptString &other);
  272. // Concatenation
  273. CScriptString &operator+=(const CScriptString &other);
  274. friend CScriptString *operator+(const CScriptString &a, const CScriptString &b);
  275. // Memory buffer
  276. std::string buffer;
  277. };
  278. \endcode
  279. \section doc_addon_string_2 Public script interface
  280. <pre>
  281. class string
  282. {
  283. // Constructors
  284. string();
  285. string(const string &in other);
  286. // Returns the length of the string
  287. uint length() const;
  288. // Assignment and concatenation
  289. string &opAssign(const string &in other);
  290. string &opAddAssign(const string &in other);
  291. string \@opAdd(const string &in right) const;
  292. // Access individual characters
  293. // uint8 &operator [] (uint)
  294. // const uint8 &operator [] (uint) const
  295. // Comparison operators
  296. bool opEquals(const string &in right) const;
  297. int opCmp(const string &in right) const;
  298. // Automatic conversion from number types to string type
  299. string &opAssign(double val);
  300. string &opAddAssign(double val);
  301. string \@opAdd(double val) const;
  302. string \@opAdd_r(double val) const;
  303. string &opAssign(float val);
  304. string &opAddAssign(float val);
  305. string \@opAdd(float val) const;
  306. string \@opAdd_r(float val) const;
  307. string &opAssign(int val);
  308. string &opAddAssign(int val);
  309. string \@opAdd(int val) const;
  310. string \@opAdd_r(int val) const;
  311. string &opAssign(uint val);
  312. string &opAddAssign(uint val);
  313. string \@opAdd(uint val) const;
  314. string \@opAdd_r(uint val) const;
  315. }
  316. // Get a substring of a string
  317. string @ substring(const string &in str, int start, int length);
  318. // Find the first occurrance of the substring
  319. int findFirst(const string &in str, const string &in sub);
  320. int findFirst(const string &in str, const string &in sub, int startAt)
  321. // Find the last occurrance of the substring
  322. int findLast(const string &in str, const string &in sub);
  323. int findLast(const string &in str, const string &in sub, int startAt);
  324. // Find the first character from the set
  325. int findFirstOf(const string &in str, const string &in set);
  326. int findFirstOf(const string &in str, const string &in set, int startAt);
  327. // Find the first character not in the set
  328. int findFirstNotOf(const string &in str, const string &in set);
  329. int findFirstNotOf(const string &in str, const string &in set, int startAt);
  330. // Find the last character from the set
  331. int findLastOf(const string &in str, const string &in set);
  332. int findLastOf(const string &in str, const string &in set, int startAt);
  333. // Find the last character not in the set
  334. int findLastNotOf(const string &in str, const string &in set);
  335. int findLastNotOf(const string &in str, const string &in set, int startAt);
  336. // Split the string into an array of substrings
  337. string@[]@ split(const string &in str, const string &in delimiter);
  338. // Join an array of strings into a larger string separated by a delimiter
  339. string@ join(const string@[] &in str, const string &in delimiter);
  340. </pre>
  341. \page doc_addon_dict dictionary object
  342. <b>Path:</b> /sdk/add_on/scriptdictionary/
  343. The dictionary object maps string values to values or objects of other types.
  344. Register with <code>RegisterScriptDictionary(asIScriptEngine*)</code>.
  345. \section doc_addon_dict_1 Public C++ interface
  346. \code
  347. class CScriptDictionary
  348. {
  349. public:
  350. // Memory management
  351. CScriptDictionary(asIScriptEngine *engine);
  352. void AddRef();
  353. void Release();
  354. // Sets/Gets a variable type value for a key
  355. void Set(const std::string &key, void *value, int typeId);
  356. bool Get(const std::string &key, void *value, int typeId) const;
  357. // Sets/Gets an integer number value for a key
  358. void Set(const std::string &key, asINT64 &value);
  359. bool Get(const std::string &key, asINT64 &value) const;
  360. // Sets/Gets a real number value for a key
  361. void Set(const std::string &key, double &value);
  362. bool Get(const std::string &key, double &value) const;
  363. // Returns true if the key is set
  364. bool Exists(const std::string &key) const;
  365. // Deletes the key
  366. void Delete(const std::string &key);
  367. // Deletes all keys
  368. void DeleteAll();
  369. };
  370. \endcode
  371. \section doc_addon_dict_2 Public script interface
  372. <pre>
  373. class dictionary
  374. {
  375. void set(const string &in key, ? &in value);
  376. bool get(const string &in value, ? &out value) const;
  377. void set(const string &in key, int64 &in value);
  378. bool get(const string &in key, int64 &out value) const;
  379. void set(const string &in key, double &in value);
  380. bool get(const string &in key, double &out value) const;
  381. bool exists(const string &in key) const;
  382. void delete(const string &in key);
  383. void deleteAll();
  384. }
  385. </pre>
  386. \section doc_addon_dict_3 Script example
  387. <pre>
  388. dictionary dict;
  389. obj object;
  390. obj \@handle;
  391. dict.set("one", 1);
  392. dict.set("object", object);
  393. dict.set("handle", \@handle);
  394. if( dict.exists("one") )
  395. {
  396. bool found = dict.get("handle", \@handle);
  397. if( found )
  398. {
  399. dict.delete("object");
  400. }
  401. }
  402. dict.deleteAll();
  403. </pre>
  404. \page doc_addon_file file object
  405. <b>Path:</b> /sdk/add_on/scriptfile/
  406. This object provides support for reading and writing files.
  407. Register with <code>RegisterScriptFile(asIScriptEngine*)</code>.
  408. If you do not want to provide write access for scripts then you can compile
  409. the add on with the define AS_WRITE_OPS 0, which will disable support for writing.
  410. This define can be made in the project settings or directly in the header.
  411. \section doc_addon_file_1 Public C++ interface
  412. \code
  413. class CScriptFile
  414. {
  415. public:
  416. // Constructor
  417. CScriptFile();
  418. // Memory management
  419. void AddRef();
  420. void Release();
  421. // Opening and closing file handles
  422. // mode = "r" -> open the file for reading
  423. // mode = "w" -> open the file for writing (overwrites existing files)
  424. // mode = "a" -> open the file for appending
  425. int Open(const std::string &filename, const std::string &mode);
  426. int Close();
  427. // Returns the size of the file
  428. int GetSize() const;
  429. // Returns true if the end of the file has been reached
  430. bool IsEOF() const;
  431. // Reads a specified number of bytes into the string
  432. int ReadString(unsigned int length, std::string &str);
  433. // Reads to the next new-line character
  434. int ReadLine(std::string &str);
  435. // Writes a string to the file
  436. int WriteString(const std::string &str);
  437. // File cursor manipulation
  438. int GetPos() const;
  439. int SetPos(int pos);
  440. int MovePos(int delta);
  441. };
  442. \endcode
  443. \section doc_addon_file_2 Public script interface
  444. <pre>
  445. class file
  446. {
  447. int open(const string &in filename, const string &in mode);
  448. int close();
  449. int getSize() const;
  450. bool isEndOfFile() const;
  451. int readString(uint length, string &out str);
  452. int readLine(string &out str);
  453. int writeString(const string &in string);
  454. int getPos() const;
  455. int setPos(int pos);
  456. int movePos(int delta);
  457. }
  458. </pre>
  459. \section doc_addon_file_3 Script example
  460. <pre>
  461. file f;
  462. // Open the file in 'read' mode
  463. if( f.open("file.txt", "r") >= 0 )
  464. {
  465. // Read the whole file into the string buffer
  466. string str;
  467. f.readString(f.getSize(), str);
  468. f.close();
  469. }
  470. </pre>
  471. \page doc_addon_math math functions
  472. <b>Path:</b> /sdk/add_on/scriptmath/
  473. This add-on registers the math functions from the standard C runtime library with the script
  474. engine. Use <code>RegisterScriptMath(asIScriptEngine*)</code> to perform the registration.
  475. By defining the preprocessor word AS_USE_FLOAT=0, the functions will be registered to take
  476. and return doubles instead of floats.
  477. \section doc_addon_math_1 Public script interface
  478. <pre>
  479. float cos(float rad);
  480. float sin(float rad);
  481. float tan(float rad);
  482. float acos(float val);
  483. float asin(float val);
  484. float atan(float val);
  485. float atan2(float y, float x);
  486. float cosh(float rad);
  487. float sinh(float rad);
  488. float tanh(float rad);
  489. float log(float val);
  490. float log10(float val);
  491. float pow(float val, float exp);
  492. float sqrt(float val);
  493. float ceil(float val);
  494. float abs(float val);
  495. float floor(float val);
  496. float fraction(float val);
  497. </pre>
  498. \page doc_addon_math3d 3D math functions
  499. <b>Path:</b> /sdk/add_on/scriptmath3d/
  500. This add-on registers some value types and functions that permit the scripts to perform
  501. 3D mathematical operations. Use <code>RegisterScriptMath3D(asIScriptEngine*)</code> to
  502. perform the registration.
  503. Currently the only thing registered is the <code>vector3</code> type, representing a 3D vector,
  504. with basic math operators, such as add, subtract, scalar multiply, equality comparison, etc.
  505. This add-on serves mostly as a sample on how to register a value type. Application
  506. developers will most likely want to register their own math library rather than use
  507. this add-on as-is.
  508. \page doc_addon_build Script builder helper
  509. <b>Path:</b> /sdk/add_on/scriptbuilder/
  510. This class is a helper class for loading and building scripts, with a basic pre-processor
  511. that supports conditional compilation, include directives, and metadata declarations.
  512. By default the script builder resolves include directives by loading the included file
  513. from the relative directory of the file it is included from. If you want to do this in another
  514. way, then you should implement the \ref doc_addon_build_1_1 "include callback" which will
  515. let you process the include directive in a custom way, e.g. to load the included file from
  516. memory, or to support multiple search paths. The include callback should call the AddSectionFromFile or
  517. AddSectionFromMemory to include the section in the current build.
  518. If you do not want process metadata then you can compile the add-on with the define
  519. AS_PROCESS_METADATA 0, which will exclude the code for processing this. This define
  520. can be made in the project settings or directly in the header.
  521. \section doc_addon_build_1 Public C++ interface
  522. \code
  523. class CScriptBuilder
  524. {
  525. public:
  526. // Start a new module
  527. int StartNewModule(asIScriptEngine *engine, const char *moduleName);
  528. // Load a script section from a file on disk
  529. int AddSectionFromFile(const char *filename);
  530. // Load a script section from memory
  531. int AddSectionFromMemory(const char *scriptCode,
  532. const char *sectionName = "");
  533. // Build the added script sections
  534. int BuildModule();
  535. // Register the callback for resolving include directive
  536. void SetIncludeCallback(INCLUDECALLBACK_t callback, void *userParam);
  537. // Add a pre-processor define for conditional compilation
  538. void DefineWord(const char *word);
  539. // Get metadata declared for class types and interfaces
  540. const char *GetMetadataStringForType(int typeId);
  541. // Get metadata declared for functions
  542. const char *GetMetadataStringForFunc(int funcId);
  543. // Get metadata declared for global variables
  544. const char *GetMetadataStringForVar(int varIdx);
  545. };
  546. \endcode
  547. \subsection doc_addon_build_1_1 The include callback signature
  548. \code
  549. // This callback will be called for each #include directive encountered by the
  550. // builder. The callback should call the AddSectionFromFile or AddSectionFromMemory
  551. // to add the included section to the script. If the include cannot be resolved
  552. // then the function should return a negative value to abort the compilation.
  553. typedef int (*INCLUDECALLBACK_t)(const char *include, const char *from, CScriptBuilder *builder, void *userParam);
  554. \endcode
  555. \section doc_addon_build_2 Include directives
  556. Example script with include directive:
  557. <pre>
  558. \#include "commonfuncs.as"
  559. void main()
  560. {
  561. // Call a function from the included file
  562. CommonFunc();
  563. }
  564. </pre>
  565. \section doc_addon_build_condition Conditional programming
  566. The builder supports conditional programming through the \#if/\#endif preprocessor directives.
  567. The application may define a word with a call to DefineWord(), then the scripts may check
  568. for this definition in the code in order to include/exclude parts of the code.
  569. This is especially useful when scripts are shared between different binaries, for example, in a
  570. client/server application.
  571. Example script with conditional compilation:
  572. <pre>
  573. class CObject
  574. {
  575. void Process()
  576. {
  577. \#if SERVER
  578. // Do some server specific processing
  579. \#endif
  580. \#if CLIENT
  581. // Do some client specific processing
  582. \#endif
  583. // Do some common processing
  584. }
  585. }
  586. </pre>
  587. \section doc_addon_build_metadata Metadata in scripts
  588. Metadata can be added before script class, interface, function, and global variable
  589. declarations. The metadata is removed from the script by the builder class and stored
  590. for post build lookup by the type id, function id, or variable index.
  591. Exactly what the metadata looks like is up to the application. The builder class doesn't
  592. impose any rules, except that the metadata should be added between brackets []. After
  593. the script has been built the application can obtain the metadata strings and interpret
  594. them as it sees fit.
  595. Example script with metadata:
  596. <pre>
  597. [factory func = CreateOgre,
  598. editable: myPosition,
  599. editable: myStrength [10, 100]]
  600. class COgre
  601. {
  602. vector3 myPosition;
  603. int myStrength;
  604. }
  605. [factory]
  606. COgre \@CreateOgre()
  607. {
  608. return \@COgre();
  609. }
  610. </pre>
  611. Example usage:
  612. \code
  613. CScriptBuilder builder;
  614. int r = builder.StartNewModule(engine, "my module");
  615. if( r >= 0 )
  616. r = builder.AddSectionFromMemory(script);
  617. if( r >= 0 )
  618. r = builder.BuildModule();
  619. if( r >= 0 )
  620. {
  621. // Find global variables that have been marked as editable by user
  622. asIScriptModule *mod = engine->GetModule("my module");
  623. int count = mod->GetGlobalVarCount();
  624. for( int n = 0; n < count; n++ )
  625. {
  626. string metadata = builder.GetMetadataStringForVar(n);
  627. if( metadata == "editable" )
  628. {
  629. // Show the global variable in a GUI
  630. ...
  631. }
  632. }
  633. }
  634. \endcode
  635. \page doc_addon_autowrap Automatic wrapper functions
  636. <b>Path:</b> /sdk/add_on/autowrapper/aswrappedcall.h
  637. This header file declares some macros and template functions that will let the application developer
  638. automatically generate wrapper functions using the \ref doc_generic "generic calling convention" with
  639. a simple call to a preprocessor macro. This is useful for those platforms where the native calling
  640. conventions are not yet supported.
  641. The macros are defined as
  642. \code
  643. #define asDECLARE_FUNCTION_WRAPPER(wrapper_name,func)
  644. #define asDECLARE_FUNCTION_WRAPPERPR(wrapper_name,func,params,rettype)
  645. #define asDECLARE_METHOD_WRAPPER(wrapper_name,cl,func)
  646. #define asDECLARE_METHOD_WRAPPERPR(wrapper_name,cl,func,params,rettype)
  647. \endcode
  648. where wrapper_name is the name of the function that you want to generate, and func is a function pointer
  649. to the function you want to wrap, cl is the class name, params is the parameter list, and rettype is the return type.
  650. Unfortunately the template functions needed to perform this generation are quite complex and older
  651. compilers may not be able to handle them. One such example is Microsoft Visual C++ 6, though luckily
  652. it has no need for them as AngelScript already supports native calling conventions for it.
  653. \section doc_addon_autowrap_1 Example usage
  654. \code
  655. #include "aswrappedcall.h"
  656. // The application function that we want to register
  657. void DoSomething(std::string param1, int param2);
  658. // Generate the wrapper for the function
  659. asDECLARE_FUNCTION_WRAPPER(DoSomething_Generic, DoSomething);
  660. // Registering the wrapper with AngelScript
  661. void RegisterWrapper(asIScriptEngine *engine)
  662. {
  663. int r;
  664. r = engine->RegisterGlobalFunction("void DoSomething(string, int)", asFUNCTION(DoSomething_Generic), asCALL_GENERIC); assert( r >= 0 );
  665. }
  666. \endcode
  667. \page doc_addon_clib ANSI C library interface
  668. <b>Path:</b> /sdk/add_on/clib/
  669. This add-on defines a pure C interface, that can be used in those applications that do not
  670. understand C++ code but do understand C, e.g. Delphi, Java, and D.
  671. To compile the AngelScript C library, you need to compile the library source files in sdk/angelscript/source together
  672. with the source files in sdk/add-on/clib, and link them as a shared dynamic library. In the application that will use
  673. the AngelScript C library, you'll include the <code>angelscript_c.h</code> header file, instead of the ordinary
  674. <code>%angelscript.h</code> header file. After that you can use the library much the same way that it's used in C++.
  675. To find the name of the C functions to call, you normally take the corresponding interface method
  676. and give a prefix according to the following table:
  677. <table border=0 cellspacing=0 cellpadding=0>
  678. <tr><td><b>interface &nbsp;</b></td><td><b>prefix&nbsp;</b></td></tr>
  679. <tr><td>asIScriptEngine &nbsp;</td> <td>asEngine_</td></tr>
  680. <tr><td>asIScriptModule &nbsp;</td> <td>asModule_</td></tr>
  681. <tr><td>asIScriptContext &nbsp;</td> <td>asContext_</td></tr>
  682. <tr><td>asIScriptGeneric &nbsp;</td> <td>asGeneric_</td></tr>
  683. <tr><td>asIScriptArray &nbsp;</td> <td>asArray_</td></tr>
  684. <tr><td>asIScriptObject &nbsp;</td> <td>asObject_</td></tr>
  685. <tr><td>asIObjectType &nbsp;</td> <td>asObjectType_</td></tr>
  686. <tr><td>asIScriptFunction &nbsp;</td> <td>asScriptFunction_</td></tr>
  687. </table>
  688. All interface methods take the interface pointer as the first parameter when in the C function format, the rest
  689. of the parameters are the same as in the C++ interface. There are a few exceptions though, e.g. all parameters that
  690. take an <code>asSFuncPtr</code> take a normal function pointer in the C function format.
  691. Example:
  692. \code
  693. #include <stdio.h>
  694. #include <assert.h>
  695. #include "angelscript_c.h"
  696. void MessageCallback(asSMessageInfo *msg, void *);
  697. void PrintSomething();
  698. int main(int argc, char **argv)
  699. {
  700. int r = 0;
  701. // Create and initialize the script engine
  702. asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  703. r = asEngine_SetMessageCallback(engine, (asFUNCTION_t)MessageCallback, 0, asCALL_CDECL); assert( r >= 0 );
  704. r = asEngine_RegisterGlobalFunction(engine, "void print()", (asFUNCTION_t)PrintSomething, asCALL_CDECL); assert( r >= 0 );
  705. // Execute a simple script
  706. r = asEngine_ExecuteString(engine, 0, "print()", 0, 0);
  707. if( r != asEXECUTION_FINISHED )
  708. {
  709. printf("Something wen't wrong with the execution\n");
  710. }
  711. else
  712. {
  713. printf("The script was executed successfully\n");
  714. }
  715. // Release the script engine
  716. asEngine_Release(engine);
  717. return r;
  718. }
  719. void MessageCallback(asSMessageInfo *msg, void *)
  720. {
  721. const char *msgType = 0;
  722. if( msg->type == 0 ) msgType = "Error ";
  723. if( msg->type == 1 ) msgType = "Warning";
  724. if( msg->type == 2 ) msgType = "Info ";
  725. printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, msgType, msg->message);
  726. }
  727. void PrintSomething()
  728. {
  729. printf("Called from the script\n");
  730. }
  731. \endcode
  732. \page doc_addon_helpers Helper functions
  733. <b>Path:</b> /sdk/add_on/scripthelper/
  734. These helper functions simplify the implemention of common tasks. They can be used as is
  735. or can serve as the starting point for your own framework.
  736. \section doc_addon_helpers_1 Public C++ interface
  737. \code
  738. // Compare relation between two objects of the same type.
  739. // Uses the object's opCmp method to perform the comparison.
  740. // Returns a negative value if the comparison couldn't be performed.
  741. int CompareRelation(asIScriptEngine *engine, void *leftObj, void *rightObj, int typeId, int &result);
  742. // Compare equality between two objects of the same type.
  743. // Uses the object's opEquals method to perform the comparison, or if that doesn't exist the opCmp method.
  744. // Returns a negative value if the comparison couldn't be performed.
  745. int CompareEquality(asIScriptEngine *engine, void *leftObj, void *rightObj, int typeId, bool &result);
  746. // Compile and execute simple statements.
  747. // The module is optional. If given the statements can access the entities compiled in the module.
  748. // The caller can optionally provide its own context, for example if a context should be reused.
  749. int ExecuteString(asIScriptEngine *engine, const char *code, asIScriptModule *mod = 0, asIScriptContext *ctx = 0);
  750. \endcode
  751. \section doc_addon_helpers_2 Example
  752. To compare two script objects the application can execute the following code:
  753. \code
  754. void Compare(asIScriptObject *a, asIScriptObject *b)
  755. {
  756. asIScriptEngine *engine = a->GetEngine();
  757. int typeId = a->GetTypeId();
  758. int cmp;
  759. int r = CompareRelation(engine, a, b, typeId, cmp);
  760. if( r < 0 )
  761. {
  762. cout << "The relation between a and b cannot be established b" << endl;
  763. }
  764. else
  765. {
  766. if( cmp < 0 )
  767. cout << "a is smaller than b" << endl;
  768. else if( cmp == 0 )
  769. cout << "a is equal to b" << endl;
  770. else
  771. cout << "a is greater than b" << endl;
  772. }
  773. }
  774. \endcode
  775. */