<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
</script>
<style type="text/css">
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
</style>
<script type="text/javascript">
gRootRelPath = "../../..";
gCommonRootRelPath = "../../..";
gTopicId = "9.1.2.0_15";
<script type="text/javascript" src="../../../template/scripts/rh.min.js"></script>
<script type="text/javascript" src="../../../template/scripts/common.min.js"></script>
<script type="text/javascript" src="../../../template/scripts/topic.min.js"></script>
<script type="text/javascript" src="../../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../../whxdata/projectsettings.js"></script>
<link rel="stylesheet" type="text/css" href="../../../template/styles/topic.min.css"/>
<link rel="stylesheet" type="text/css" href="../../../template/Charcoal_Grey/topicheader.css"/>
<meta name="topic-status" content="Draft"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>new</title>
<meta name="generator" content="Adobe RoboHelp 2019"/>
<link rel="stylesheet" href="../../../assets/css/default.css" type="text/css"/>
<meta name="rh-authors" content="Mark Alexander"/>
<meta name="topic-comment" content="Page outlining the new function"/>
<meta name="rh-index-keywords" content="new"/>
<meta name="search-keywords" content="new"/>
</head>
<body>
<div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
<div class="logo">
</div>
<div class="nav">
<div class="title" title="new">
<span>new</span>
</div>
<div class="gotohome" title="Click here to see this page in full context">
<span>Click here to see this page in full context</span>
</div>
</div>
</div>
<div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>
<h1>new</h1>
<p>The <tt>new</tt> operator can be used to create a struct with a function which is called with the given arguments. The function can then populate the struct with variables from the arguments - much like the create event of an instance will set the initial
variables for the instance - and then the <tt>new</tt> operator will return the struct. Before using this operator it is important to note that the function given <i>must be flagged as a <tt>constructor</tt> function</i> otherwise the <tt>new</tt> operator will not create the struct (the code example below shows this, and for more information, please see the page on <a href="../Structs.htm">Structs</a>).</p>
<p class="note"><strong>NOTE</strong>: You can check a struct to find out which function was used to create it using the runtime function <a href="../../GML_Reference/Variable_Functions/instance_of.htm"><tt>instance_of()</tt></a>.</p>
<p>This operator has the following syntax:</p>
<p class="code"><span><variable> = </span>new <function> (<argument>, <argument>, ...);</p>
<p>When you use the new operator along with a function, it will return a struct reference which is stored in the given variable, permitting you to access the struct and any variables that were created within it by the function.</p>
<p>The following example shows a function that has been defined to use as a contructor:</p>
<p class="code">function init_struct(_a, _b, _c) constructor<br/> {
<br/> a = _a;<br/> b = _b;<br/> c = _c;<br/> }
</p>
<p>This function can then be used along with the <tt>new</tt> operator to create a struct and populate it with the variables set to the values of the arguments used in the function, like this:</p>
<p class="code">mystruct = new init_struct(10, 100, "Hello World");</p>
<p>It is important to note that calling new on any function that has <em>not</em> been flagged as a constructor will cause a <a href="../../../Additional_Information/Errors/Runner_Errors.htm">runtime exception</a>.</p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="../Language_Features.htm">Language Features</a></div>
<div style="float:right">Next: <a href="delete.htm">delete</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
</body></html>