https://wiki.frictionalgames.com/page?title=HPL2/ScriptReference/Functions_Part_3&feed=atom&action=historyHPL2/ScriptReference/Functions Part 3 - Revision history2024-03-28T17:47:46ZRevision history for this page on the wikiMediaWiki 1.34.2https://wiki.frictionalgames.com/page?title=HPL2/ScriptReference/Functions_Part_3&diff=3471&oldid=prevDarkfire: Darkfire moved page Hpl2:Amnesia:script language reference and guide:functions - part 3 to HPL2/ScriptReference/Functions Part 3 without leaving a redirect: Moving to the main namespace2020-08-05T13:17:43Z<p>Darkfire moved page <a href="/page?title=Hpl2:Amnesia:script_language_reference_and_guide:functions_-_part_3&action=edit&redlink=1" class="new" title="Hpl2:Amnesia:script language reference and guide:functions - part 3 (page does not exist)">Hpl2:Amnesia:script language reference and guide:functions - part 3</a> to <a href="/page/HPL2/ScriptReference/Functions_Part_3" title="HPL2/ScriptReference/Functions Part 3">HPL2/ScriptReference/Functions Part 3</a> without leaving a redirect: Moving to the main namespace</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">Revision as of 13:17, 5 August 2020</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Darkfirehttps://wiki.frictionalgames.com/page?title=HPL2/ScriptReference/Functions_Part_3&diff=3468&oldid=prevDarkfire: Link updates2020-08-05T13:16:11Z<p>Link updates</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 13:16, 5 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l233" >Line 233:</td>
<td colspan="2" class="diff-lineno">Line 233:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== Callbacks ===</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== Callbacks ===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>If you read through the previous discussion, and through Functions [[<del class="diffchange diffchange-inline">hpl2:amnesia:script_language_reference_and_guide:funcions_-_part_1</del>|Part 1]] and [[<del class="diffchange diffchange-inline">hpl2:amnesia:script_language_reference_and_guide:funcions_-_part_2</del>|Part 2]], you should now have enough knowledge to understand how to use callback functions, and to get a good grasp on how they work.</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If you read through the previous discussion, and through Functions [[<ins class="diffchange diffchange-inline">HPL2/ScriptReference/Functions Part 1</ins>|Part 1]] and [[<ins class="diffchange diffchange-inline">HPL2/ScriptReference/Functions Part 2</ins>|Part 2]], you should now have enough knowledge to understand how to use callback functions, and to get a good grasp on how they work.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>A ''callback'' function is, simply put, just a function which is supposed to be, at some later point, called by some other peace of code, when some event of interest happens.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>A ''callback'' function is, simply put, just a function which is supposed to be, at some later point, called by some other peace of code, when some event of interest happens.</div></td></tr>
</table>Darkfirehttps://wiki.frictionalgames.com/page?title=HPL2/ScriptReference/Functions_Part_3&diff=1797&oldid=prevDarkfire: Syntax highlighting fix2020-07-23T16:31:34Z<p>Syntax highlighting fix</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 16:31, 23 July 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1" >Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>= Functions - Part 3: Digging Deeper =</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>= Functions - Part 3: Digging Deeper =</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">----</del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== At a Glance ==</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== At a Glance ==</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>'''Passing by Value vs Passing by Reference'''</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>'''Passing by Value vs Passing by Reference'''</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del class="diffchange diffchange-inline"><note tip></del></div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">There are two </ins>kinds of types:</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del class="diffchange diffchange-inline">Two </del>kinds of types:</div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div># Value types (all fundamental types, except string) </div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div># Value types (all fundamental types, except string) </div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div># Reference types (strings, arrays, and classes)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div># Reference types (strings, arrays, and classes)</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></note></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><u>Passing by Value</u></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><u>Passing by Value</u></div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l35" >Line 35:</td>
<td colspan="2" class="diff-lineno">Line 27:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><u>Passing by Reference</u></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><u>Passing by Reference</u></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><syntaxhighlight lang="c++"></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><syntaxhighlight lang="c++"></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>// Input references are best used with const parameters (see discussion)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>// Input references are best used with const parameters (see discussion)</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l120" >Line 120:</td>
<td colspan="2" class="diff-lineno">Line 113:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>}</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">----</del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Discussion ==</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Discussion ==</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l129" >Line 129:</td>
<td colspan="2" class="diff-lineno">Line 120:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>By default, parameters to functions are passed ''by value''. This means that ''a copy'' of the original value is created when the function is entered, and it is this copy that you work with within the scope of the function. If you make any changes to the value, the original is not affected, as it is in no way connected to the parameter. Once the function returns, the parameter goes out of scope, and no changes are preserved.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>By default, parameters to functions are passed ''by value''. This means that ''a copy'' of the original value is created when the function is entered, and it is this copy that you work with within the scope of the function. If you make any changes to the value, the original is not affected, as it is in no way connected to the parameter. Once the function returns, the parameter goes out of scope, and no changes are preserved.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>[[ :hpl2:amnesia:script_language_reference_and_guide:byvaluegif.gif?nolink |]]</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[ :hpl2:amnesia:script_language_reference_and_guide:byvaluegif.gif?nolink |<ins class="diffchange diffchange-inline">hpl2:amnesia:script_language_reference_and_guide:byvaluegif.gif?nolink </ins>]]</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>As the animation demonstrates, when the function makes changes to the parameter (when "{Function Body Code}" flashes in the image), it's the temporary, local value that is affected, and thus any changes within the scope of the function are local and temporary in nature as well - they exist only within the function, and only for the duration of the function.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>As the animation demonstrates, when the function makes changes to the parameter (when "{Function Body Code}" flashes in the image), it's the temporary, local value that is affected, and thus any changes within the scope of the function are local and temporary in nature as well - they exist only within the function, and only for the duration of the function.</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l135" >Line 135:</td>
<td colspan="2" class="diff-lineno">Line 126:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== Passing Parameters by Reference ===</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>=== Passing Parameters by Reference ===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>''There are other ways to pass parameters.'' Collectively, these ways are referred to as ''passing by reference''. The word "reference" describes something that ''refers to'' (or ''points to'') something else - just like a phone number is is a reference to someone's physical phone, or a web address is a reference to a web page.<del class="diffchange diffchange-inline"><br /></del>In the case of reference parameters, it means that the parameters themselves are somehow connected back to the original inputs. There are three kinds of references provided by the language:</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>''There are other ways to pass parameters.'' Collectively, these ways are referred to as ''passing by reference''. The word "reference" describes something that ''refers to'' (or ''points to'') something else - just like a phone number is is a reference to someone's physical phone, or a web address is a reference to a web page.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>In the case of reference parameters, it means that the parameters themselves are somehow connected back to the original inputs. There are three kinds of references provided by the language:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* input references (''&in'')</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* input references (''&in'')</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l148" >Line 148:</td>
<td colspan="2" class="diff-lineno">Line 140:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del class="diffchange diffchange-inline"><</del>note <del class="diffchange diffchange-inline">tip></del>In this guide, the third notation is preferred, since it allows you to think of ''int&'' as of an "int-reference" ''type'', while regarding the ''out'' keyword as a behavioral modifier, which determines the relation between the original variable that was passed, and the reference parameter itself (see below).<del class="diffchange diffchange-inline"></note></del></div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">{{</ins>note<ins class="diffchange diffchange-inline">|Note: </ins>In this guide, the third notation is preferred, since it allows you to think of ''int&'' as of an "int-reference" ''type'', while regarding the ''out'' keyword as a behavioral modifier, which determines the relation between the original variable that was passed, and the reference parameter itself (see below).<ins class="diffchange diffchange-inline">}}</ins></div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> </div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===== Input References =====</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===== Input References =====</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l159" >Line 159:</td>
<td colspan="2" class="diff-lineno">Line 150:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Marking a parameter as an out-reference designates it as output-only, which has more interesting consequences. Basically, you're declaring that the value of the variable passed as the out-parameter will be set from within the function. When the function starts, the parameter itself begins ''uninitialized''; you are then free to perform any calculations required, and set the value of the out-parameter. During the execution of the function, the original is not affected, but ''when the function reaches its end'', the the value of the out-parameter replaces the original value.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Marking a parameter as an out-reference designates it as output-only, which has more interesting consequences. Basically, you're declaring that the value of the variable passed as the out-parameter will be set from within the function. When the function starts, the parameter itself begins ''uninitialized''; you are then free to perform any calculations required, and set the value of the out-parameter. During the execution of the function, the original is not affected, but ''when the function reaches its end'', the the value of the out-parameter replaces the original value.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>[[ :hpl2:amnesia:script_language_reference_and_guide:byoutrefgif.gif?nolink |]]</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[ :hpl2:amnesia:script_language_reference_and_guide:byoutrefgif.gif?nolink |<ins class="diffchange diffchange-inline">hpl2:amnesia:script_language_reference_and_guide:byoutrefgif.gif?nolink </ins>]]</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===== Input-Output References =====</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===== Input-Output References =====</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l165" >Line 165:</td>
<td colspan="2" class="diff-lineno">Line 156:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Inout-references are probably the most useful, but are only allowed on so-called reference types, which are strings, array types, and user defined classes (you'll learn more about classes and objects in a different part of the guide). As you have guessed, they declare the parameter to be both for input and for output. When the function begins, no copies are made - the parameter instead points directly to the original memory location, effectively becoming an alias for the original variable. Any changes made from within the function are immediately "visible" to the original variable, because both the parameter and the variable point to the same thing.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Inout-references are probably the most useful, but are only allowed on so-called reference types, which are strings, array types, and user defined classes (you'll learn more about classes and objects in a different part of the guide). As you have guessed, they declare the parameter to be both for input and for output. When the function begins, no copies are made - the parameter instead points directly to the original memory location, effectively becoming an alias for the original variable. Any changes made from within the function are immediately "visible" to the original variable, because both the parameter and the variable point to the same thing.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>[[ :hpl2:amnesia:script_language_reference_and_guide:byrefgif.gif?nolink |]]</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[[ :hpl2:amnesia:script_language_reference_and_guide:byrefgif.gif?nolink |<ins class="diffchange diffchange-inline">hpl2:amnesia:script_language_reference_and_guide:byrefgif.gif?nolink </ins>]]</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The animation shows how an inout-reference works on a user defined type (class); the initial state ("value") of the object is represented by the cube; when the function is called, the function's code changes the state through the inout-reference parameter, which is represented by the cube becoming a cone. The effect on the original object is direct and immediate, and it is retained even after the function ends.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The animation shows how an inout-reference works on a user defined type (class); the initial state ("value") of the object is represented by the cube; when the function is called, the function's code changes the state through the inout-reference parameter, which is represented by the cube becoming a cone. The effect on the original object is direct and immediate, and it is retained even after the function ends.</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l259" >Line 259:</td>
<td colspan="2" class="diff-lineno">Line 250:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>void AddTimer(string& timerID, float timeSpan, string& timerCallback);</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>void AddTimer(string& timerID, float timeSpan, string& timerCallback);</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"><syntaxhighlight lang=""></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Creates a timer which calls a function when it expires.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Creates a timer which calls a function when it expires.</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Callback syntax: void MyFunc(string& in timerID)</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Callback syntax: <ins class="diffchange diffchange-inline"><code></ins>void MyFunc(string& in timerID)<ins class="diffchange diffchange-inline"></code></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>timerID - the name of the timer</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">'''</ins>timerID<ins class="diffchange diffchange-inline">''' </ins>- the name of the timer</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>timeSpan - time in seconds</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">'''</ins>timeSpan<ins class="diffchange diffchange-inline">''' </ins>- time in seconds</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>timerCallback - the function to call </div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">'''</ins>timerCallback<ins class="diffchange diffchange-inline">''' </ins>- the function to call </div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del class="diffchange diffchange-inline"></syntaxhighlight></del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This means that the user-defined callback has to have the return type of ''void'', and that it has to accept a single ''string'' parameter as an in-reference. In other words, the declaration of the callback can be expressed as:</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This means that the user-defined callback has to have the return type of ''void'', and that it has to accept a single ''string'' parameter as an in-reference. In other words, the declaration of the callback can be expressed as:</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l273" >Line 273:</td>
<td colspan="2" class="diff-lineno">Line 262:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>To assign a function which meets those requirements as a timer callback, all you need to do is to call the AddTimer() function, passing the ''name'' of your callback as the last string parameter. The game will then internally use the name to find the function itself, and store this information in an internal data structure, so that the function can be invoked when the time comes.<del class="diffchange diffchange-inline"><br /></del>E.g., if you wanted to add a 5-second timer, and make something happen when it expires, you'd do something like this:</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>To assign a function which meets those requirements as a timer callback, all you need to do is to call the AddTimer() function, passing the ''name'' of your callback as the last string parameter. The game will then internally use the name to find the function itself, and store this information in an internal data structure, so that the function can be invoked when the time comes.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>E.g., if you wanted to add a 5-second timer, and make something happen when it expires, you'd do something like this:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><syntaxhighlight lang="c++"></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><syntaxhighlight lang="c++"></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>void OnEnter()</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>void OnEnter()</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l357" >Line 357:</td>
<td colspan="2" class="diff-lineno">Line 347:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Coming soon... In the meantime, see: [[hpl2:tutorials:script:funcdef|]]</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Coming soon... In the meantime, see: [[hpl2:tutorials:script:funcdef|<ins class="diffchange diffchange-inline">tutorials:script:funcdef</ins>]]</div></td></tr>
</table>Darkfirehttps://wiki.frictionalgames.com/page?title=HPL2/ScriptReference/Functions_Part_3&diff=740&oldid=prevMaintenance script: Upload from wiki2020-07-09T13:47:54Z<p>Upload from wiki</p>
<p><b>New page</b></p><div>= Functions - Part 3: Digging Deeper =<br />
<br />
----<br />
<br />
<br />
<br />
== At a Glance ==<br />
<br />
<br />
'''Passing by Value vs Passing by Reference'''<br />
<br />
<note tip><br />
Two kinds of types:<br />
# Value types (all fundamental types, except string) <br />
# Reference types (strings, arrays, and classes)<br />
</note><br />
<br />
<u>Passing by Value</u><br />
<br />
<syntaxhighlight lang="c++"><br />
// Reminder: passing by value<br />
int Double(int input) // 'input' contains a copy of the original value<br />
{<br />
input = input * 2; // This is not enough, as 'input' is a copy<br />
<br />
return input; // The caller must get the new value through the standard return<br />
}<br />
</syntaxhighlight><br />
<br />
Reference parameters can be: <br />
* '''in''' - input references - not very useful, similar to passing by value<br />
* '''out''' - output references<br />
* '''inout''' - input/output references - only allowed for reference types<br />
<br />
<br />
<u>Passing by Reference</u><br />
<syntaxhighlight lang="c++"><br />
// Input references are best used with const parameters (see discussion)<br />
int GetSquare(const int& in input)<br />
{ <br />
return input * input;<br />
}<br />
<br />
<br />
// Output references replace the value of the original variable when the function ends<br />
void GetRandomEvenIntByOutRef(int& out result) // 'result' starts uninitialized<br />
{<br />
result = RandInt(0, 1000000) * 2; // this affects the original variable<br />
}<br />
<br />
// Input-output references immediately affect the original object:<br />
void AddBrackets(string& inout text)<br />
{<br />
text = "(" + text + ")"; // this immediately affects the original object<br />
}<br />
<br />
// In/out-reference parameters can be alternatively (and preferably) written as:<br />
void AddBrackets(string& text)<br />
{<br />
text = "(" + text + ")"; // this immediately affects the original object<br />
}<br />
<br />
// Input-output references with classes:<br />
void ResetPassCode(PassCode& passcodeObject)<br />
{<br />
passcodeObject.Reset(); // this immediately affects the original object<br />
}<br />
<br />
<br />
// Passing by reference using object handles:<br />
void ResetPassCode(PassCode@ passcodeObject)<br />
{<br />
passcodeObject.Reset(); // this immediately affects the original object<br />
}<br />
</syntaxhighlight><br />
<br />
<u>Using References to Support Multiple Return Values</u><br />
<syntaxhighlight lang="c++"><br />
void GetCircleProperties(float radius, float& out circumference, float& out area)<br />
{<br />
float pi = 3.14f;<br />
circumference = 2.0f * pi * radius;<br />
area = pi * radius * radius;<br />
}<br />
</syntaxhighlight><br />
<br />
'''Callbacks'''<br />
<br />
<u>Example 1</u>: SetEffectVoiceOverCallback(string& callbackName)<br />
<br />
<syntaxhighlight lang="c++"><br />
// Documentation wiki says: <br />
// Callback snytax: void MyFunc() --> return type and parameter list must match<br />
// for candidate functions<br />
<br />
// "hookup"<br />
SetEffectVoiceOverCallback("OnVoiceOver"); <br />
<br />
// The callback is defined elsewhere in the code:<br />
void OnVoiceOver() // Function name chosen by the scripter<br />
{<br />
// do something...<br />
}<br />
</syntaxhighlight><br />
<br />
<u>Example 2</u>: AddTimer(string& asTimer, float time, string& callbackName)<br />
<br />
<syntaxhighlight lang="c++"><br />
// Documentation wiki says: <br />
// Callback snytax: void MyFunc(string &in asTimer) --> return type and parameter list must match<br />
// for candidate functions<br />
<br />
// "hookup"<br />
AddTimer("some.timer.id", 2.0f, "TwoSecondsLater"); <br />
<br />
// The callback is defined elsewhere in the code:<br />
void TwoSecondsLater(string &in asTimer) // Function name chosen by the scripter<br />
{<br />
// do something...<br />
}<br />
</syntaxhighlight><br />
<br />
----<br />
<br />
== Discussion ==<br />
<br />
=== Passing Parameters by Value ===<br />
<br />
By default, parameters to functions are passed ''by value''. This means that ''a copy'' of the original value is created when the function is entered, and it is this copy that you work with within the scope of the function. If you make any changes to the value, the original is not affected, as it is in no way connected to the parameter. Once the function returns, the parameter goes out of scope, and no changes are preserved.<br />
<br />
[[ :hpl2:amnesia:script_language_reference_and_guide:byvaluegif.gif?nolink |]]<br />
<br />
As the animation demonstrates, when the function makes changes to the parameter (when "{Function Body Code}" flashes in the image), it's the temporary, local value that is affected, and thus any changes within the scope of the function are local and temporary in nature as well - they exist only within the function, and only for the duration of the function.<br />
<br />
=== Passing Parameters by Reference ===<br />
<br />
''There are other ways to pass parameters.'' Collectively, these ways are referred to as ''passing by reference''. The word "reference" describes something that ''refers to'' (or ''points to'') something else - just like a phone number is is a reference to someone's physical phone, or a web address is a reference to a web page.<br />In the case of reference parameters, it means that the parameters themselves are somehow connected back to the original inputs. There are three kinds of references provided by the language:<br />
<br />
* input references (''&in'')<br />
* output references (''&out'')<br />
* input-output references (''&inout'', or just ''&'')<br />
<br />
You can turn a by-value parameter, like ''int paramName'', to a reference parameter by appending the symbols listed above to the type of the variable. For example, to declare a parameter as an out-reference, you would use any of these (they are all equivalent and are a matter of preference):<br />
<syntaxhighlight lang="c++"><br />
int&out paramName<br />
int &out paramName<br />
int& out paramName<br />
</syntaxhighlight><br />
<br />
<note tip>In this guide, the third notation is preferred, since it allows you to think of ''int&'' as of an "int-reference" ''type'', while regarding the ''out'' keyword as a behavioral modifier, which determines the relation between the original variable that was passed, and the reference parameter itself (see below).</note><br />
<br />
<br />
===== Input References =====<br />
<br />
An in-reference designates the parameter as input-only, denying the possibility of affecting the original, which ''mostly'' has the exact same effect as passing by value. As such, they are not particularly useful, except with const-parameters (which can't be changed by definition), when they ''may'' improve performance by not creating a copy, letting the parameter to point to the original memory location instead.<br />
<br />
===== Output References =====<br />
<br />
Marking a parameter as an out-reference designates it as output-only, which has more interesting consequences. Basically, you're declaring that the value of the variable passed as the out-parameter will be set from within the function. When the function starts, the parameter itself begins ''uninitialized''; you are then free to perform any calculations required, and set the value of the out-parameter. During the execution of the function, the original is not affected, but ''when the function reaches its end'', the the value of the out-parameter replaces the original value.<br />
<br />
[[ :hpl2:amnesia:script_language_reference_and_guide:byoutrefgif.gif?nolink |]]<br />
<br />
===== Input-Output References =====<br />
<br />
Inout-references are probably the most useful, but are only allowed on so-called reference types, which are strings, array types, and user defined classes (you'll learn more about classes and objects in a different part of the guide). As you have guessed, they declare the parameter to be both for input and for output. When the function begins, no copies are made - the parameter instead points directly to the original memory location, effectively becoming an alias for the original variable. Any changes made from within the function are immediately "visible" to the original variable, because both the parameter and the variable point to the same thing.<br />
<br />
[[ :hpl2:amnesia:script_language_reference_and_guide:byrefgif.gif?nolink |]]<br />
<br />
The animation shows how an inout-reference works on a user defined type (class); the initial state ("value") of the object is represented by the cube; when the function is called, the function's code changes the state through the inout-reference parameter, which is represented by the cube becoming a cone. The effect on the original object is direct and immediate, and it is retained even after the function ends.<br />
<br />
==== Using Reference Parameters to Support Multiple Return Values ====<br />
<br />
Normally, a function may return only one result; however, with out-references and inout-references, you can return multiple values from a single function. In the example below, ''radius'' is passed by value as an argument, while ''circumference'' and ''area'' represent output parameters, which are to be the return values of the GetCircleProperties() function.<br />
<br />
<syntaxhighlight lang="c++"><br />
void GetCircleProperties(float radius, float& out circumference, float& out area)<br />
{<br />
float pi = 3.14f;<br />
circumference = 2.0f * pi * radius;<br />
area = pi * radius * radius;<br />
}<br />
<br />
// Somewhere else in the code:<br />
float circleRadius = 10.0f;<br />
float circ, area;<br />
<br />
GetCircleProperties(radius, circ, area);<br />
<br />
// At this point, <br />
// circ == 62.8f, and <br />
// area == 314.0f<br />
</syntaxhighlight><br />
<br />
Output parameters can also be useful when the function needs to return a success/failure indicator, alongside the result of some calculation. In the example below, the SetEntityActiveRandom() function randomly activates/deactivates an entity. It returns ''false'' if the specified entity couldn't be found on the map (say, if you misspelled the name); otherwise it returns ''true''. The randomly picked activation state is returned via an output parameter, for convenience, in case the caller might want to somehow use it elsewhere. Also, now that you know what reference parameters are, note that the string is passed by an inout-reference, since this is faster, avoids unnecessary copies, and is consistent with the rest of the functions exposed by the engine.<br />
<syntaxhighlight lang="c++"><br />
bool SetEntityActiveRandom(string& entityID, bool& out activationState)<br />
{<br />
if(!GetEntityExists(entityID))<br />
return false; // no such entity<br />
<br />
// OK, the entity exists<br />
activationState = (RandInt(0, 1) == 1);<br />
<br />
SetEntityActive(entityID, activationState); <br />
return true;<br />
}<br />
<br />
// Usage:<br />
bool wasActivated;<br />
bool success = SetEntityActiveRandom("servant_grunt_corridor", wasActivated);<br />
<br />
if (success && wasActivated)<br />
{<br />
// do something with the monster<br />
}<br />
</syntaxhighlight><br />
<br />
Note how the code that uses the function can test if the call was sucessfull. Often, functions which return boolean success indicators, like SetEntityActiveRandom() does, are called from within the if-conditionals themselves; the next code snippet is equivalent to the "usage" code above:<br />
<syntaxhighlight lang="c++"><br />
bool wasActivated;<br />
if(SetEntityActiveRandom("servant_grunt_corridor", wasActivated))<br />
{<br />
if (wasActivated)<br />
{<br />
// do something with the monster<br />
}<br />
}<br />
</syntaxhighlight><br />
<br />
This is possible because the function returns a ''bool'', that is, it ''evaluates'' to a ''bool'', which is all that is required for it to be used as a condition in an if-statement.<br />
<br />
<br />
=== Do Reference Parameters Play a Role When Overloading Functions? ===<br />
<br />
Yes they do, but in a ''specific way'', and with a few ''caveats''. Essentially, adding a reference modifier to a parameter changes its ''type''. E.g., ''int'' is the integer type, while ''int& out'' is an "integer reference" type. Reference types are ''only allowed'' as function parameters though. Now, although the different reference modes (in, out, inout) behave differently, as far as the compiler is concerned, all of them give the same function signature. In its eyes, ''string&'', ''string& in'' and ''string& out'' are the same when it comes to deciding which overload to call.<br />
<br />
For this reason, except for some special cases and unless you take some special actions, the compiler often won't be able to decide which overload you intended to make the call to. This is why you generally shouldn't overload a function simply by providing another version which uses reference parameters.<br />
<br />
This is also one of the reasons this guide prefers writing ''int& out'' to ''int &out''.<br />
<br />
<br />
=== Callbacks ===<br />
<br />
If you read through the previous discussion, and through Functions [[hpl2:amnesia:script_language_reference_and_guide:funcions_-_part_1|Part 1]] and [[hpl2:amnesia:script_language_reference_and_guide:funcions_-_part_2|Part 2]], you should now have enough knowledge to understand how to use callback functions, and to get a good grasp on how they work.<br />
<br />
A ''callback'' function is, simply put, just a function which is supposed to be, at some later point, called by some other peace of code, when some event of interest happens.<br />
<br />
Among the predefined, engine-exposed functions, there's quite a number of functions which let you add a callback to some internal game structure, so that it can be called when a certain in-game event, such as an entity collision, happens. Here are some of them:<br />
<syntaxhighlight lang="c++"><br />
void AddTimer(string& timerID, float timeSpan, string& timerCallback); // 'timerCallback' is called when timeSpan elapses<br />
void SetEffectVoiceOverCallback(string& voCallback); // 'voCallback' is called when voice-over effects end<br />
void SetLanternLitCallback(string& lanternCallback); // 'lanternCallback' is invoked when the lantern is lit<br />
void SetEntityCallbackFunc(string& entityID, string& interactCallback); // ''interactCallback'' called when 'entityID' is interacted with<br />
</syntaxhighlight><br />
<br />
However, each of these predefined functions requires a callback of a specific format - the callback has to have a specific return type, and a specific parameter list. The name of the callback is not important, nor are the names of the parameters; the number, types, and the order of the parameters are, though. For each of the engine-defined functions, the wiki documentation, along with the meaning of each parameter, states the required callback format as "Callback syntax:". For example, for the AddTimer() function, it says:<br />
<br />
<syntaxhighlight lang="c++"><br />
void AddTimer(string& timerID, float timeSpan, string& timerCallback);<br />
</syntaxhighlight><br />
<syntaxhighlight lang=""><br />
Creates a timer which calls a function when it expires.<br />
Callback syntax: void MyFunc(string& in timerID)<br />
<br />
timerID - the name of the timer<br />
timeSpan - time in seconds<br />
timerCallback - the function to call <br />
</syntaxhighlight><br />
<br />
This means that the user-defined callback has to have the return type of ''void'', and that it has to accept a single ''string'' parameter as an in-reference. In other words, the declaration of the callback can be expressed as:<br />
<syntaxhighlight lang="c++"><br />
void FunctionName(string& in);<br />
</syntaxhighlight><br />
<br />
To assign a function which meets those requirements as a timer callback, all you need to do is to call the AddTimer() function, passing the ''name'' of your callback as the last string parameter. The game will then internally use the name to find the function itself, and store this information in an internal data structure, so that the function can be invoked when the time comes.<br />E.g., if you wanted to add a 5-second timer, and make something happen when it expires, you'd do something like this:<br />
<syntaxhighlight lang="c++"><br />
void OnEnter()<br />
{<br />
AddTimer("id.timer.screamScare", 5.0f, "OnScreamScare");<br />
}<br />
<br />
// the callback<br />
void OnScreamScare(string& in timerID)<br />
{<br />
PlayGuiSound("05_event_door_bang.snt", 1.0f);<br />
}<br />
</syntaxhighlight><br />
<br />
The format of the callback serves a specific purpose; in the case of a timer callback, the ''timerID'' parameter is an input-reference parameter which identifies the timer (useful, as it is possible for several timers to use the same callback). The parameter is passed in ''by the game itself'', when the timer expires, and when the ''game'' makes the call (or "calls the function back"). You can check its value from within the callback if you so desire.<br />
<br />
Here's an example from the game (from 00_rainy_hall.hps):<br />
<syntaxhighlight lang="c++"><br />
// NOTE: The code has been formated to match the style of this guide;<br />
// some comments are added as well.<br />
<br />
// somewhere in the code...<br />
AddTimer("door_gallery", 0.01f, "TimerSwingDoor");<br />
<br />
// someplace else...<br />
AddTimer("door_gust", 0.3f, "TimerSwingDoor");<br />
<br />
// callback<br />
void TimerSwingDoor(string& in timerID)<br />
{<br />
// This makes sure that the AddPropForce() function is<br />
// applied 10 times in short (0.03-second) intervals,<br />
// simulating the effect of a wind blowing<br />
if(GetLocalVarInt("SwingDoor") == 10)<br />
{<br />
SetLocalVarInt("SwingDoor", 0);<br />
<br />
// when the force is applied for the 10th time, <br />
// simply return from the function<br />
return; <br />
}<br />
<br />
// This code check which timer invoked the callback,<br />
// and then applies a different force based on that<br />
if(timerID == "door_gallery") <br />
AddPropForce(timerID, 70.0f, 0, 0, "World"); <br />
else <br />
AddPropForce(timerID, -95.0f, 0, 0, "World"); <br />
<br />
// This increments the "SwingDoor" indicator by 1, <br />
// (or sets it to 1 on the first call)<br />
AddLocalVarInt("SwingDoor", 1); <br />
<br />
// This re-assigns this same function as a callback, <br />
// restarting the same timer by passing 'timerID'<br />
AddTimer(timerID, 0.03f, "TimerSwingDoor"); <br />
<br />
// (a debug message ommited here...)<br />
}<br />
</syntaxhighlight><br />
<br />
<br />
To be qualified as a callback of a specific kind, it is enough for a function to match the required callback format (return type and parameter list). It doesn't even have to be defined by you - it is perfectly legitimate to add a predefined engine function as a callback for some event (although there aren't as many cases where you might want to do this). The code below demonstrates this:<br />
<syntaxhighlight lang="c++"><br />
void OnEnter()<br />
{<br />
SetEffectVoiceOverCallback("StartRandomInsanityEvent");<br />
}<br />
<br />
// someplace else in the script...<br />
AddEffectVoice("CH01L00_DanielsMind01_01.ogg", "", "Flashbacks", "CH01L00_DanielsMind01_01", false, "", 0, 0);<br />
</syntaxhighlight><br />
<br />
The SetEffectVoiceOverCallback() function defines a callback to be called when a voice-over sequence that can be started during the execution of the game reaches its end. The AddEffectVoice() voice function starts the sequence at some point, and when the sequence finishes, the predefined StartRandomInsanityEvent() is called, and an insanity event (a random "voice in the head") is played out.<br />
<br />
This is possible because SetEffectVoiceOverCallback() requires a callback of the following format:<br />
<syntaxhighlight lang="c++">void FunctionName()</syntaxhighlight><br />
<br />
And from the declaration of StartRandomInsanityEvent(), you can see that the format matches:<br />
<syntaxhighlight lang="c++">void StartRandomInsanityEvent();</syntaxhighlight><br />
<br />
=== Function Handles (Function Pointers) ===<br />
<br />
<br />
Coming soon... In the meantime, see: [[hpl2:tutorials:script:funcdef|]]</div>Maintenance script