Angelscript/docs/manual/doc_compile_script.html

188 lines
19 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: Compiling scripts</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_compile_script.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">Compiling scripts </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>After <a class="el" href="doc_register_api.html">registering the application interface</a> it's time to compile the scripts that will be executed.</p>
<h1><a class="anchor" id="doc_compile_script_msg"></a>
Message callback</h1>
<p>Before starting the compilation, remember to have the message callback set in the engine so that you can get more information on compilation errors than just an error code. In fact, it is recommended to set the message callback right after creating the script engine, as the message callback may even be helpful while registering the application interface.</p>
<p>The message callback has been designed so that it doesn't output anything if there are no errors or warnings, so when everything is ok, you shouldn't get anything from it. But if the <a class="el" href="classas_i_script_module.html#a8acf107194c5f079d7f7507309ebe613">Build</a> method returns an error, the message callback will have received detailed information about the error.</p>
<p>If desired, the application may send its own messages to the callback via the <a class="el" href="classas_i_script_engine.html#a936ce6566af958bb75ba1c0945d8b03a">WriteMessage</a> method on the engine.</p>
<div class="fragment"><div class="line"><span class="comment">// Implement a simple message callback function</span></div>
<div class="line"><span class="keywordtype">void</span> MessageCallback(<span class="keyword">const</span> <a class="code" href="structas_s_message_info.html">asSMessageInfo</a> *msg, <span class="keywordtype">void</span> *param)</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span> *type = <span class="stringliteral">&quot;ERR &quot;</span>;</div>
<div class="line"> <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8">type</a> == <a class="code" href="angelscript_8h.html#a8badcd23652646db5c5c6981dc73d4f5a210c2023d6971d688a0302096acf945d">asMSGTYPE_WARNING</a> ) </div>
<div class="line"> type = <span class="stringliteral">&quot;WARN&quot;</span>;</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8">type</a> == <a class="code" href="angelscript_8h.html#a8badcd23652646db5c5c6981dc73d4f5ae29dba474231c07149dca09a9258f80d">asMSGTYPE_INFORMATION</a> ) </div>
<div class="line"> type = <span class="stringliteral">&quot;INFO&quot;</span>;</div>
<div class="line"> printf(<span class="stringliteral">&quot;%s (%d, %d) : %s : %s\n&quot;</span>, msg-&gt;<a class="code" href="structas_s_message_info.html#aeca6368be12c84b62ed8c659b1e4615c">section</a>, msg-&gt;<a class="code" href="structas_s_message_info.html#a21ef80321436f229a547411a6598ea21">row</a>, msg-&gt;<a class="code" href="structas_s_message_info.html#a08b23a360ac52110323bbf4aad553d9d">col</a>, type, msg-&gt;<a class="code" href="structas_s_message_info.html#af76694c6342dd82ef6aca0dff42072f5">message</a>);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Set the message callback when creating the engine</span></div>
<div class="line"><a class="code" href="classas_i_script_engine.html">asIScriptEngine</a> *engine = <a class="code" href="group__api__principal__functions.html#gacb6a62345d9cca6c9b5a3dac67d80d0b">asCreateScriptEngine</a>();</div>
<div class="line">engine-&gt;<a class="code" href="classas_i_script_engine.html#a74192fe950808eb72a64e3e371f0ea02">SetMessageCallback</a>(<a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae">asFUNCTION</a>(MessageCallback), 0, <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4">asCALL_CDECL</a>);</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_compile_script_load"></a>
Loading and compiling scripts</h1>
<p>To build a script module you first obtain a module from the engine, then add the script sections, and finally compile them. A compiled script module may be composed of one or more script sections, so your application may store each section in different files, or even generate them dynamically. It doesn't matter in which order the script sections are added to the module, as the compiler is able to resolve all names regardless of where they are declared in the script.</p>
<div class="fragment"><div class="line"><span class="comment">// Create a new script module</span></div>
<div class="line"><a class="code" href="classas_i_script_module.html">asIScriptModule</a> *mod = engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4">GetModule</a>(<span class="stringliteral">&quot;module&quot;</span>, <a class="code" href="angelscript_8h.html#ae4cf50de5273eb8c03c6e91e6e014f0ca0843ab784ed9a9ea6cb47d915825186f">asGM_ALWAYS_CREATE</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Load and add the script sections to the module</span></div>
<div class="line"><span class="keywordtype">string</span> script;</div>
<div class="line">LoadScriptFile(<span class="stringliteral">&quot;script.as&quot;</span>, script);</div>
<div class="line">mod-&gt;<a class="code" href="classas_i_script_module.html#a330835919b565c76c25e9425536f50b7">AddScriptSection</a>(<span class="stringliteral">&quot;script.as&quot;</span>, script.c_str());</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Build the module</span></div>
<div class="line"><span class="keywordtype">int</span> r = mod-&gt;<a class="code" href="classas_i_script_module.html#a8acf107194c5f079d7f7507309ebe613">Build</a>();</div>
<div class="line"><span class="keywordflow">if</span>( r &lt; 0 )</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// The build failed. The message stream will have received </span></div>
<div class="line"> <span class="comment">// compiler errors that shows what needs to be fixed</span></div>
<div class="line">}</div>
</div><!-- fragment --><p>AngelScript doesn't provide built-in functions for loading script files as most applications have their own way of loading files. However, it is quite easy to write your own routines for loading script files, for example:</p>
<div class="fragment"><div class="line"><span class="comment">// Load the entire script file into a string buffer</span></div>
<div class="line"><span class="keywordtype">void</span> LoadScriptFile(<span class="keyword">const</span> <span class="keywordtype">char</span> *fileName, <span class="keywordtype">string</span> &amp;script)</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// Open the file in binary mode</span></div>
<div class="line"> FILE *f = fopen(<span class="stringliteral">&quot;test.as&quot;</span>, <span class="stringliteral">&quot;rb&quot;</span>);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Determine the size of the file</span></div>
<div class="line"> fseek(f, 0, SEEK_END);</div>
<div class="line"> <span class="keywordtype">int</span> len = ftell(f);</div>
<div class="line"> fseek(f, 0, SEEK_SET);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Load the entire file in one call</span></div>
<div class="line"> script.resize(len);</div>
<div class="line"> fread(&amp;script[0], len, 1, f);</div>
<div class="line"> </div>
<div class="line"> fclose(f);</div>
<div class="line">} </div>
</div><!-- fragment --><p>As AngelScript doesn't load the files itself, it also doesn't have built-in support for including other files from within the script. However, if you look in the add-on directory, you'll find a <a class="el" href="doc_addon_build.html">CScriptBuilder</a> class that provides this support and more. It is a helper class for loading files, perform a pre-processing pass, and then building the module. You can a see an example of how to use the script builder in <a class="el" href="doc_hello_world.html">Your first script</a>.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_adv_precompile.html">Pre-compiled byte code</a> </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<div class="ttc" id="astructas_s_message_info_html_a08b23a360ac52110323bbf4aad553d9d"><div class="ttname"><a href="structas_s_message_info.html#a08b23a360ac52110323bbf4aad553d9d">asSMessageInfo::col</a></div><div class="ttdeci">int col</div><div class="ttdoc">The column.</div><div class="ttdef"><b>Definition:</b> angelscript.h:773</div></div>
<div class="ttc" id="aangelscript_8h_html_a8badcd23652646db5c5c6981dc73d4f5ae29dba474231c07149dca09a9258f80d"><div class="ttname"><a href="angelscript_8h.html#a8badcd23652646db5c5c6981dc73d4f5ae29dba474231c07149dca09a9258f80d">asMSGTYPE_INFORMATION</a></div><div class="ttdeci">@ asMSGTYPE_INFORMATION</div><div class="ttdoc">The message is informational only.</div><div class="ttdef"><b>Definition:</b> angelscript.h:424</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_a9f7cdc52b59034e6e55eb8a56b427aa4"><div class="ttname"><a href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4">asIScriptEngine::GetModule</a></div><div class="ttdeci">virtual asIScriptModule * GetModule(const char *module, asEGMFlags flag=asGM_ONLY_IF_EXISTS)=0</div><div class="ttdoc">Return an interface pointer to the module.</div></div>
<div class="ttc" id="aangelscript_8h_html_ae4cf50de5273eb8c03c6e91e6e014f0ca0843ab784ed9a9ea6cb47d915825186f"><div class="ttname"><a href="angelscript_8h.html#ae4cf50de5273eb8c03c6e91e6e014f0ca0843ab784ed9a9ea6cb47d915825186f">asGM_ALWAYS_CREATE</a></div><div class="ttdeci">@ asGM_ALWAYS_CREATE</div><div class="ttdoc">Always create a new module, discarding the existing one.</div><div class="ttdef"><b>Definition:</b> angelscript.h:528</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_engine_html_a74192fe950808eb72a64e3e371f0ea02"><div class="ttname"><a href="classas_i_script_engine.html#a74192fe950808eb72a64e3e371f0ea02">asIScriptEngine::SetMessageCallback</a></div><div class="ttdeci">virtual int SetMessageCallback(const asSFuncPtr &amp;callback, void *obj, asDWORD callConv)=0</div><div class="ttdoc">Sets a message callback that will receive compiler messages.</div></div>
<div class="ttc" id="astructas_s_message_info_html_a21ef80321436f229a547411a6598ea21"><div class="ttname"><a href="structas_s_message_info.html#a21ef80321436f229a547411a6598ea21">asSMessageInfo::row</a></div><div class="ttdeci">int row</div><div class="ttdoc">The row number.</div><div class="ttdef"><b>Definition:</b> angelscript.h:771</div></div>
<div class="ttc" id="aangelscript_8h_html_a78f8f2c7f1c88b12e74a5ac47b4184ae"><div class="ttname"><a href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae">asFUNCTION</a></div><div class="ttdeci">#define asFUNCTION(f)</div><div class="ttdoc">Returns an asSFuncPtr representing the function specified by the name.</div><div class="ttdef"><b>Definition:</b> angelscript.h:675</div></div>
<div class="ttc" id="agroup__api__principal__functions_html_gacb6a62345d9cca6c9b5a3dac67d80d0b"><div class="ttname"><a href="group__api__principal__functions.html#gacb6a62345d9cca6c9b5a3dac67d80d0b">asCreateScriptEngine</a></div><div class="ttdeci">AS_API asIScriptEngine * asCreateScriptEngine(asDWORD version=ANGELSCRIPT_VERSION)</div><div class="ttdoc">Creates the script engine.</div></div>
<div class="ttc" id="astructas_s_message_info_html_aeca6368be12c84b62ed8c659b1e4615c"><div class="ttname"><a href="structas_s_message_info.html#aeca6368be12c84b62ed8c659b1e4615c">asSMessageInfo::section</a></div><div class="ttdeci">const char * section</div><div class="ttdoc">The script section where the message is raised.</div><div class="ttdef"><b>Definition:</b> angelscript.h:769</div></div>
<div class="ttc" id="aangelscript_8h_html_a8badcd23652646db5c5c6981dc73d4f5a210c2023d6971d688a0302096acf945d"><div class="ttname"><a href="angelscript_8h.html#a8badcd23652646db5c5c6981dc73d4f5a210c2023d6971d688a0302096acf945d">asMSGTYPE_WARNING</a></div><div class="ttdeci">@ asMSGTYPE_WARNING</div><div class="ttdoc">The message is a warning.</div><div class="ttdef"><b>Definition:</b> angelscript.h:422</div></div>
<div class="ttc" id="aclassas_i_script_module_html"><div class="ttname"><a href="classas_i_script_module.html">asIScriptModule</a></div><div class="ttdoc">The interface to the script modules.</div><div class="ttdef"><b>Definition:</b> angelscript.h:2218</div></div>
<div class="ttc" id="aclassas_i_script_module_html_a8acf107194c5f079d7f7507309ebe613"><div class="ttname"><a href="classas_i_script_module.html#a8acf107194c5f079d7f7507309ebe613">asIScriptModule::Build</a></div><div class="ttdeci">virtual int Build()=0</div><div class="ttdoc">Build the previously added script sections.</div></div>
<div class="ttc" id="aclassas_i_script_module_html_a330835919b565c76c25e9425536f50b7"><div class="ttname"><a href="classas_i_script_module.html#a330835919b565c76c25e9425536f50b7">asIScriptModule::AddScriptSection</a></div><div class="ttdeci">virtual int AddScriptSection(const char *name, const char *code, size_t codeLength=0, int lineOffset=0)=0</div><div class="ttdoc">Add a script section for the next build.</div></div>
<div class="ttc" id="aangelscript_8h_html_a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4"><div class="ttname"><a href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4">asCALL_CDECL</a></div><div class="ttdeci">@ asCALL_CDECL</div><div class="ttdoc">A cdecl function.</div><div class="ttdef"><b>Definition:</b> angelscript.h:226</div></div>
<div class="ttc" id="astructas_s_message_info_html_af76694c6342dd82ef6aca0dff42072f5"><div class="ttname"><a href="structas_s_message_info.html#af76694c6342dd82ef6aca0dff42072f5">asSMessageInfo::message</a></div><div class="ttdeci">const char * message</div><div class="ttdoc">The message text.</div><div class="ttdef"><b>Definition:</b> angelscript.h:777</div></div>
<div class="ttc" id="astructas_s_message_info_html_a6aa9231534b8aea2a3099cdc3206bcc8"><div class="ttname"><a href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8">asSMessageInfo::type</a></div><div class="ttdeci">asEMsgType type</div><div class="ttdoc">The type of message.</div><div class="ttdef"><b>Definition:</b> angelscript.h:775</div></div>
<div class="ttc" id="astructas_s_message_info_html"><div class="ttname"><a href="structas_s_message_info.html">asSMessageInfo</a></div><div class="ttdoc">Represents a compiler message.</div><div class="ttdef"><b>Definition:</b> angelscript.h:767</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>