Angelscript/docs/manual/doc_addon_array.html

233 lines
20 KiB
HTML
Raw Permalink Normal View History

2021-04-12 18:25:02 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: array template object</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_addon_array.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">array template object </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Path:</b> /sdk/add_on/scriptarray/</p>
<p>The <code>array</code> type is a <a class="el" href="doc_adv_template.html">template object</a> that allow the scripts to declare arrays of any type. Since it is a generic class it is not the most performatic due to the need to determine characteristics at runtime. For that reason it is recommended that the application registers a <a class="el" href="doc_adv_template.html#doc_adv_template_2">template specialization</a> for the array types that are most commonly used.</p>
<p>The type is registered with <code>RegisterScriptArray(asIScriptEngine *engine, bool defaultArrayType)</code>. The second parameter should be set to true if you wish to allow the syntax form <code>type[]</code> to declare arrays.</p>
<p>Compile the add-on with the pre-processor define AS_USE_STLNAMES=1 to register the methods with the same names as used by C++ STL where the methods have the same significance. Not all methods from STL is implemented in the add-on, but many of the most frequent once are so a port from script to C++ and vice versa might be easier if STL names are used.</p>
<p>Compile the add-on with the pre-processor define AS_USE_ACCESSORS=1 to register length as a virtual property instead of the method length().</p>
<h1><a class="anchor" id="doc_addon_array_1"></a>
Public C++ interface</h1>
<div class="fragment"><div class="line"><span class="keyword">class </span>CScriptArray</div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line"> <span class="comment">// Set the memory functions that should be used by all CScriptArrays</span></div>
<div class="line"> <span class="keyword">static</span> <span class="keywordtype">void</span> SetMemoryFunctions(<a class="code" href="angelscript_8h.html#ac69a827822c73771cd972bff270cefc7">asALLOCFUNC_t</a> allocFunc, <a class="code" href="angelscript_8h.html#adeecc934971e695fc4441a47694860fb">asFREEFUNC_t</a> freeFunc);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Factory functions</span></div>
<div class="line"> <span class="keyword">static</span> CScriptArray *Create(<a class="code" href="classas_i_type_info.html">asITypeInfo</a> *arrayType);</div>
<div class="line"> <span class="keyword">static</span> CScriptArray *Create(<a class="code" href="classas_i_type_info.html">asITypeInfo</a> *arrayType, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> length);</div>
<div class="line"> <span class="keyword">static</span> CScriptArray *Create(<a class="code" href="classas_i_type_info.html">asITypeInfo</a> *arrayType, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> length, <span class="keywordtype">void</span> *defaultValue);</div>
<div class="line"> <span class="keyword">static</span> CScriptArray *Create(<a class="code" href="classas_i_type_info.html">asITypeInfo</a> *arrayType, <span class="keywordtype">void</span> *listBuffer);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Memory management</span></div>
<div class="line"> <span class="keywordtype">void</span> AddRef() <span class="keyword">const</span>;</div>
<div class="line"> <span class="keywordtype">void</span> Release() <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Type information</span></div>
<div class="line"> <a class="code" href="classas_i_type_info.html">asITypeInfo</a> *GetArrayObjectType() <span class="keyword">const</span>;</div>
<div class="line"> <span class="keywordtype">int</span> GetArrayTypeId() <span class="keyword">const</span>;</div>
<div class="line"> <span class="keywordtype">int</span> GetElementTypeId() <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Get the current size</span></div>
<div class="line"> <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> GetSize() <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Returns true if the array is empty</span></div>
<div class="line"> <span class="keywordtype">bool</span> IsEmpty() <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Pre-allocates memory for elements</span></div>
<div class="line"> <span class="keywordtype">void</span> Reserve(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> numElements);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Resize the array</span></div>
<div class="line"> <span class="keywordtype">void</span> Resize(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> numElements);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Get a pointer to an element. Returns 0 if out of bounds</span></div>
<div class="line"> <span class="keywordtype">void</span> *At(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> index);</div>
<div class="line"> <span class="keyword">const</span> <span class="keywordtype">void</span> *At(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> index) <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Set value of an element. </span></div>
<div class="line"> <span class="comment">// The value arg should be a pointer to the value that will be copied to the element.</span></div>
<div class="line"> <span class="comment">// Remember, if the array holds handles the value parameter should be the </span></div>
<div class="line"> <span class="comment">// address of the handle. The refCount of the object will also be incremented</span></div>
<div class="line"> <span class="keywordtype">void</span> SetValue(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> index, <span class="keywordtype">void</span> *value);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Copy the contents of one array to another (only if the types are the same)</span></div>
<div class="line"> CScriptArray &amp;operator=(<span class="keyword">const</span> CScriptArray&amp;);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Compare two arrays</span></div>
<div class="line"> <span class="keywordtype">bool</span> operator==(<span class="keyword">const</span> CScriptArray &amp;) <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Array manipulation</span></div>
<div class="line"> <span class="keywordtype">void</span> InsertAt(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> index, <span class="keywordtype">void</span> *value);</div>
<div class="line"> <span class="keywordtype">void</span> RemoveAt(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> index);</div>
<div class="line"> <span class="keywordtype">void</span> InsertLast(<span class="keywordtype">void</span> *value);</div>
<div class="line"> <span class="keywordtype">void</span> RemoveLast();</div>
<div class="line"> <span class="keywordtype">void</span> SortAsc();</div>
<div class="line"> <span class="keywordtype">void</span> SortAsc(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> startAt, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> count);</div>
<div class="line"> <span class="keywordtype">void</span> SortDesc();</div>
<div class="line"> <span class="keywordtype">void</span> SortDesc(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> startAt, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> count);</div>
<div class="line"> <span class="keywordtype">void</span> Sort(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> startAt, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> count, <span class="keywordtype">bool</span> asc);</div>
<div class="line"> <span class="keywordtype">void</span> Sort(<a class="code" href="classas_i_script_function.html">asIScriptFunction</a> *less, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> startAt, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> count);</div>
<div class="line"> <span class="keywordtype">void</span> Reverse();</div>
<div class="line"> <span class="keywordtype">int</span> Find(<span class="keywordtype">void</span> *value) <span class="keyword">const</span>;</div>
<div class="line"> <span class="keywordtype">int</span> Find(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> startAt, <span class="keywordtype">void</span> *value) <span class="keyword">const</span>;</div>
<div class="line"> <span class="keywordtype">int</span> FindByRef(<span class="keywordtype">void</span> *ref) <span class="keyword">const</span>;</div>
<div class="line"> <span class="keywordtype">int</span> FindByRef(<a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> startAt, <span class="keywordtype">void</span> *ref) <span class="keyword">const</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Returns the address of the inner buffer for direct manipulation</span></div>
<div class="line"> <span class="keywordtype">void</span> *GetBuffer();</div>
<div class="line">};</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_addon_array_2"></a>
Public script interface</h1>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_datatypes_arrays.html">Arrays in the script language</a></dd></dl>
<h1><a class="anchor" id="doc_addon_array_4"></a>
C++ example</h1>
<p>This function shows how a script array can be instantiated from the application and then passed to the script.</p>
<div class="fragment"><div class="line"><span class="comment">// Registered with AngelScript as &#39;array&lt;string&gt; @CreateArrayOfString()&#39;</span></div>
<div class="line">CScriptArray *CreateArrayOfStrings()</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// If called from the script, there will always be an active </span></div>
<div class="line"> <span class="comment">// context, which can be used to obtain a pointer to the engine.</span></div>
<div class="line"> <a class="code" href="classas_i_script_context.html">asIScriptContext</a> *ctx = <a class="code" href="group__api__principal__functions.html#gad3a20dc58093b92a5a44c7b6ada34a10">asGetActiveContext</a>();</div>
<div class="line"> <span class="keywordflow">if</span>( ctx )</div>
<div class="line"> {</div>
<div class="line"> <a class="code" href="classas_i_script_engine.html">asIScriptEngine</a>* engine = ctx-&gt;<a class="code" href="classas_i_script_context.html#a07f12016c5435aec5b63449abb6e4d8d">GetEngine</a>();</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// The script array needs to know its type to properly handle the elements.</span></div>
<div class="line"> <span class="comment">// Note that the object type should be cached to avoid performance issues</span></div>
<div class="line"> <span class="comment">// if the function is called frequently.</span></div>
<div class="line"> <a class="code" href="classas_i_type_info.html">asITypeInfo</a>* t = engine-&gt;<a class="code" href="classas_i_script_engine.html#ab00808f9b762c4badf508ed511789d1b">GetTypeInfoByDecl</a>(<span class="stringliteral">&quot;array&lt;string&gt;&quot;</span>);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Create an array with the initial size of 3 elements</span></div>
<div class="line"> CScriptArray* arr = CScriptArray::Create(t, 3);</div>
<div class="line"> <span class="keywordflow">for</span>( <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a> i = 0; i &lt; arr-&gt;GetSize(); i++ )</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Set the value of each element</span></div>
<div class="line"> <span class="keywordtype">string</span> val(<span class="stringliteral">&quot;test&quot;</span>);</div>
<div class="line"> arr-&gt;SetValue(i, &amp;val);</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// The ref count for the returned handle was already set in the array&#39;s constructor</span></div>
<div class="line"> <span class="keywordflow">return</span> arr;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<div class="ttc" id="aclassas_i_script_context_html"><div class="ttname"><a href="classas_i_script_context.html">asIScriptContext</a></div><div class="ttdoc">The interface to the virtual machine.</div><div class="ttdef"><b>Definition:</b> angelscript.h:2717</div></div>
<div class="ttc" id="aclassas_i_script_engine_html"><div class="ttname"><a href="classas_i_script_engine.html">asIScriptEngine</a></div><div class="ttdoc">The engine interface.</div><div class="ttdef"><b>Definition:</b> angelscript.h:1092</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a07f12016c5435aec5b63449abb6e4d8d"><div class="ttname"><a href="classas_i_script_context.html#a07f12016c5435aec5b63449abb6e4d8d">asIScriptContext::GetEngine</a></div><div class="ttdeci">virtual asIScriptEngine * GetEngine() const =0</div><div class="ttdoc">Returns a pointer to the engine.</div></div>
<div class="ttc" id="aclassas_i_type_info_html"><div class="ttname"><a href="classas_i_type_info.html">asITypeInfo</a></div><div class="ttdoc">The interface for describing types This interface is used to describe the types in the script engine.</div><div class="ttdef"><b>Definition:</b> angelscript.h:3527</div></div>
<div class="ttc" id="aangelscript_8h_html_adeecc934971e695fc4441a47694860fb"><div class="ttname"><a href="angelscript_8h.html#adeecc934971e695fc4441a47694860fb">asFREEFUNC_t</a></div><div class="ttdeci">void(* asFREEFUNC_t)(void *)</div><div class="ttdoc">The function signature for the custom memory deallocation function.</div><div class="ttdef"><b>Definition:</b> angelscript.h:637</div></div>
<div class="ttc" id="aangelscript_8h_html_ac8186f029686800b7ce36bde4a55c815"><div class="ttname"><a href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a></div><div class="ttdeci">unsigned int asUINT</div><div class="ttdoc">32 bit unsigned integer</div><div class="ttdef"><b>Definition:</b> angelscript.h:600</div></div>
<div class="ttc" id="aclassas_i_script_function_html"><div class="ttname"><a href="classas_i_script_function.html">asIScriptFunction</a></div><div class="ttdoc">The interface for a script function description.</div><div class="ttdef"><b>Definition:</b> angelscript.h:3823</div></div>
<div class="ttc" id="aangelscript_8h_html_ac69a827822c73771cd972bff270cefc7"><div class="ttname"><a href="angelscript_8h.html#ac69a827822c73771cd972bff270cefc7">asALLOCFUNC_t</a></div><div class="ttdeci">void *(* asALLOCFUNC_t)(size_t)</div><div class="ttdoc">The function signature for the custom memory allocation function.</div><div class="ttdef"><b>Definition:</b> angelscript.h:635</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_ab00808f9b762c4badf508ed511789d1b"><div class="ttname"><a href="classas_i_script_engine.html#ab00808f9b762c4badf508ed511789d1b">asIScriptEngine::GetTypeInfoByDecl</a></div><div class="ttdeci">virtual asITypeInfo * GetTypeInfoByDecl(const char *decl) const =0</div><div class="ttdoc">Returns a type by declaration.</div></div>
<div class="ttc" id="agroup__api__principal__functions_html_gad3a20dc58093b92a5a44c7b6ada34a10"><div class="ttname"><a href="group__api__principal__functions.html#gad3a20dc58093b92a5a44c7b6ada34a10">asGetActiveContext</a></div><div class="ttdeci">AS_API asIScriptContext * asGetActiveContext()</div><div class="ttdoc">Returns the currently active context.</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:24 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>