{"id":4,"date":"2021-07-20T20:53:09","date_gmt":"2021-07-20T20:53:09","guid":{"rendered":"https:\/\/chronocrash.com\/obor\/wiki\/?p=4"},"modified":"2026-05-04T23:38:30","modified_gmt":"2026-05-05T04:38:30","slug":"variables","status":"publish","type":"post","link":"https:\/\/chronocrash.com\/obor\/wiki\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Variables are named identifiers that contain a value. If you&#8217;ve ever done algebra, you&#8217;ve used a variable. Variables are a basic building block of script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Variable type describes the kind of data the variable contains, and how it will behave when interacting with logic expressions. One of the fundamental differences between C and OpenBOR Script is that variables in OpenBOR Script are weakly typed as opposed to strong typed. By weak typed, we mean the engine handles typing for you, and will assign or reassign variable types automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The obvious advantage to this model is not having to worry about type, but it does mean you need to be wary of what&#8217;s going on. Take a look at this example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint i;\n\ni = 0;\n\ni = i + &quot; is a string&quot;;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In C, this would never work. You&#8217;d get a nasty typing error and the application would not compile. In OpenBOR Script, the engine reassigns <code>i<\/code> to a string type, and <code>i<\/code> is now <em>&#8220;0 is a string&#8221;<\/em>. It&#8217;s simple to work with at first, but be careful you don&#8217;t let it ruin your logic expressions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In any case, there may be times when you want to know what type OpenBOR has assigned to a variable and act accordingly. To do so, use the <code>typeof()<\/code> function as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint type = typeof(mixed &lt;variable&gt;);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The <code>typeof()<\/code>function will return a constant indicating the variable type.<\/p>\n\n\n\n<figure id=\"type\" class=\"wp-block-table\"><table><thead><tr><th>Type<\/th><th>Constant<\/th><th> Declaration <\/th><th> Notes <\/th><\/tr><\/thead><tbody><tr><td>Float<\/td><td><code>openborconstant(\"VT_DECIMAL\")<\/code><\/td><td><code>float x = 0.1;<\/code><\/td><td>Floating point decimal. In OpenBOR script you must fully qualify a float. In other words, <code>0.1<\/code> is a valid floating decimal, <code>.1<\/code> is not. Floating values have a precision of seven decimals and a value range of <code>-3.4E+38<\/code> to <code>3.4E+38<\/code>.<\/td><\/tr><tr><td>Integer<\/td><td><code>openborconstant(\"VT_INTEGER\")<\/code><\/td><td><code>int x = 1;<\/code><\/td><td>A whole numeric value. Probably the most commonly used type. Integer values can range from <code>\u22122,147,483,648<\/code> to <code>2,147,483,647<\/code> (signed) or <code>0<\/code> to <code>4,294,967,295<\/code> (unsigned). Native integers are signed unless noted otherwise. All user defined integers are signed. Some native integer values may be limited to a specific list of constants. See the individual properties for details.<\/td><\/tr><tr><td>Null<\/td><td><code>openborconstant(\"VT_EMPTY\")<\/code><\/td><td><code>mixed x = NULL();<\/code><\/td><td>A variable that is undefined or has no value at all.<\/td><\/tr><tr><td>Pointer<\/td><td><code>openborconstant(\"VT_PTR\")<\/code><\/td><td><code>void x = &lt;object&gt;;<\/code><\/td><td>Pointer to a memory address. Pointers are typically used to reference structured objects (ex. an entity) or collections of items.<\/td><\/tr><tr><td>String<\/td><td><code>openborconstant(\"VT_STR\")<\/code><\/td><td><code>char x = \"string\";<\/code><\/td><td>Any arbitrary collection of characters. This sentence is a string.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Note that once a variable is typed to a string, it is never auto typed into anything else. You should always avoid using strings in logical expressions as they are extremely sub-optimal compared to other types (this is not an OpenBOR caveat, take this advice for any other engine or coding). Moreover, if it isn&#8217;t obvious, you can&#8217;t do any sort of math on a string at all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are cases where you might end up with numeric values typed as a string. To force them back to integer or float type, use the following functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>float x = string_to_float(&lt;string&gt;)<\/code><\/li>\n\n\n\n<li><code>int x = string_to_int(&lt;string&gt;)<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/* This number is typed as a string, so we can&#039;t do much with it. *\/\nchar string_value = &quot;5&quot;;\n\n\/* Now we have an integer we can add, subtract, multiply, etc. *\/\nint integer_value = string_to_int(string_value);\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Scope refers to the visibility of a variable throughout a function, its parent scripts, and so on. It&#8217;s best practice to use the most narrow scope possible for a given task (this again is true for all coding, not just OpenBOR Script).  <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Function variables are variables you define inside a given function without specifying the scope. They are unique to that function and only that function. Function vars are destroyed as soon as the function is finished. In other languages, these are sometimes called local variables, and that&#8217;s the correct terminology here too. We just call them function variables to avoid confusion with <a href=\"http:\/\/www.chronocrash.com\/apps\/wiki\/obor\/index.php?title=Variables#local\">Local Vars<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nfunction somefunction() {\n\n    int name = value;\n\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Global<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nmixed var = setglobalvar(mixed &lt;identifier&gt;, mixed &lt;value&gt;); \n\ngetglobalvar(mixed &lt;identifier&gt;);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As their name implies, global vars occupy the global scope. Once defined, they are available to all functions in all scripts at all times. Global vars are even persistent through game saves, so turning off the engine is not always enough to get rid of them. This later caveat can be very powerful if used correctly, but if misunderstood can also introduce bugs into your scripts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Tip:<\/strong> Avoid global vars whenever possible They are very powerful, but also easily misused and extremely difficult to debug. It is best practice to limit yourself to the scope you absolutely need for a given task, and save global vars for the jobs only they can do.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Entity<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nsetentityvar(void &lt;entity&gt;, mixed &lt;identifier&gt;, mixed &lt;value&gt;) \n\nmixed var = getentityvar(void &lt;object&gt;, mixed &lt;identifier&gt;) \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Entity variables are similar to global variables. They occupy the global scope, and so are available to any function, anywhere, anytime. The difference is that entity vars include a second key to identify them as belonging to a specific entity. Entity vars remain in memory until the associated entity is <a href=\"http:\/\/www.chronocrash.com\/apps\/wiki\/obor\/index.php?title=Kill\">killed<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nfunction somefunction() {\n\nvoid entity  = getlocalvar(&quot;self&quot;);\n\nsetentityvar(entity, &quot;birthdate&quot;, &quot;2004-01-01&quot;);\n\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Local<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nsetlocalvar(mixed &lt;identifier&gt;, mixed &lt;value&gt;); \n\nmixed var = getlocalvar(mixed &lt;identifier&gt;);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Local vars can be a little confusing to understand, but they are immensely powerful once mastered. A local variable is accessible to all functions within a given individual script instance, and remains persistent until the script is destroyed. For those familiar with object oriented programming, local vars could be considered analogous to class variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you declare a local var in an entity&#8217;s animation script, that variable is now available to all functions in that specific entity&#8217;s animation script. Animation scripts stay active as long as the entity does, so the local var remains persistent until the entity is <a href=\"https:\/\/chronocrash.com\/obor\/wiki\/killed-and-dead\/\" data-type=\"post\" data-id=\"283\">killed<\/a>. When the entity is <a href=\"https:\/\/chronocrash.com\/obor\/wiki\/killed-and-dead\/\" data-type=\"post\" data-id=\"283\">killed<\/a> and its animation script destroyed, the local var is destroyed along with it. This means unlike global vars, you don&#8217;t need to worry about clean up since the engine will do it for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Local vars are ideal for transferring information between functions in a script without using tons of parameters or global level variables. They also are perfect for temporary storage of information across multiple runs of a function(s).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nmain() {\n\n   \/* Set local var. *\/\n   setlocalvar(&quot;test_localvar&quot;, &quot;Hello world!&quot;);\n\n   \/* Print local var to log with another function. *\/\n   print_log();\n\n}\n\nfunction print_log() {\n\n    char test;\n\n    \/* Get the local var *\/\n    test = getlocalvar(&quot;test_localvar&quot;);\n\n    \/* Print text to log. *\/\n    log(test);\n\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Strings\u200b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As noted in types, strings are any arbitrary collection of characters.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Maximum string length is 127 characters. Any characters exceeding the limit are truncated.<\/li>\n\n\n\n<li>You may use the <code>+<\/code> operator to concatenate a string with any other variable (the result is always a string).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, the following functions are included to assist in string work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">strinfirst()\u200b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar haystack = &quot;Hello World&quot;;\nchar needle = &quot;llo&quot;;\n\nchar result = strinfirst(haystack, needle);\n\n\/\/ result = &quot;llo World&quot;;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Accepts a string (haystack) and a search string (needle). Returns string starting from the first occurrence of substring. If substring is not found, returns <code>-1<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">strinlast()\u200b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar haystack = &quot;Hello World&quot;;\nchar needle = &quot;o&quot;;\n\nchar result = strinlast(haystack, needle);\n\n\/\/ result = &quot;orld&quot;;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Accepts a string (haystack) and a search string (needle). Returns string starting from the last occurrence of substring. If substring is not found, returns <code>-1<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">strleft()\u200b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar haystack = &quot;Hello World&quot;;\nchar length = 3;\n\nchar result = strleft(haystack, length);\n\n\/\/ result = &quot;Hel&quot;;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Accepts a string and a length. Returns length characters of string starting from the left.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">strright()\u200b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar haystack = &quot;Hello World&quot;;\nchar length = 3;\n\nchar result = strright(haystack, length);\n\n\/\/ result = &quot;rld&quot;;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Accepts a string and a length. Returns length characters of string starting from the right.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">strlength()\u200b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar haystack = &quot;Hello World&quot;;\n\nint result = strlength(haystack);\n\n\/\/ result = 11;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Accepts a string and returns the number of characters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">strwidth()\u200b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar haystack = &quot;Hello World&quot;;\nint font = &lt;font index&gt;;\n\nint result = strwidth(haystack, font);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Accepts a string and a font index. Returns the total width in pixels. For example, if the font&#8217;s character width is 10 pixels and the string has 11 characters, the width returned is 110 pixels.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Arrays\u200b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">OpenBOR supports indexed and string-keyed arrays. In actuality, an OpenBOR array is a defined object occupying the global space, so you will need to treat it as such for purposes of initializing, referencing, and cleanup (see free()).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While they may behave similarly in script, under the hood the two types of arrays behave very differently, and you should tailor your data structures and scripts accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Indexed<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Indexed arrays are essentially C arrays, meaning they are zero indexed, concurrent groups of elements with a contiguous space allocated in the hardware platform&#8217;s memory. This makes indexed arrays blazing fast and memory efficient for accessing and modifying existing elements. However, arrays cannot be resized without reallocating a whole new array and copying existing elements over, so it is important to figure out how many elements you will need and avoid inserts or deletes during gameplay.  <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">String-Keyed<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes called literal arrays. String-keyed arrays are not actually arrays all, but a double linked list with hash map acceleration. They are thus not less memory efficient and not as fast as indexed arrays, but are highly flexible and can shrink or grow without penalty.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Switching Types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You do not actually switch array types. When you allocate an array, OpenBOR creates the indexed array first. If you then <code>add()<\/code> or <code>set()<\/code> with a string key, OpenBOR builds the linked list along-side the original indexed array. This is why when planning to use string keys, you should allocate <code>0<\/code>.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-4d52096d92a5a6590875940c245ae845 wp-block-paragraph\">Warning: It is technically possible to treat an array as both index and string-keyed \u2013 but you should never do this. Treat an array as either string-keyed or indexed, not both. Mixing them can produce confusing size results, broken assumptions, erroneous output, and hard-to-track bugs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tips<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Don&#8217;t forget indexed keys are 0 indexed.<\/li>\n\n\n\n<li>You can nest array pointers to create multidimensional data structures. Nested arrays can safely have different key types than their parents. <\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Functions<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">add()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid key = 0; \/\/ Int or string.\nvoid value = 0; \/\/ Any value type.\n\nadd(void array_pointer, index, value);\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indexed \u2013<\/strong> Inserts value at <code>key<\/code>. Key must be from <code>0<\/code> through <code>size(array_pointer)<\/code>. Resizes the array by one and shifts all indexes from <code>key<\/code> upward. <\/li>\n\n\n\n<li><strong>String-Keyed \u2013<\/strong> Creates a named entry for <code>key<\/code> with <code>value<\/code>, or updates <code>value<\/code> if <code>key<\/code> already exists. Same behavior as <code>set()<\/code>.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">array()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid array_pointer = array(int size);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Allocates an array with <code>int size<\/code> initial indexed elements and returns the array pointer.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indexed \u2013<\/strong> Allocate the number of elements you expect to use.<\/li>\n\n\n\n<li><strong>String-Keyed \u2013<\/strong> Use <code>array(0)<\/code>, because string-keyed entries are stored separately from indexed block.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">delete()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid index = 0; \/\/ Int or string.\n\ndelete(void array_pointer, index);\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indexed \u2013<\/strong> Deletes the target element, resizes the array, and shifts all higher indexes down.<\/li>\n\n\n\n<li><strong>String-Keyed \u2013<\/strong> Removes the named entry for <code>key<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid using <code>delete()<\/code> during active gameplay. For indexed arrays, <code>delete()<\/code> causes resizing and shifting. For string-keyed arrays, it changes the named list structure. Both create avoidable memory churn.<br><br><strong>Tip: <\/strong>You can set <code>NULL()<\/code> to clear a value without churn. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">get()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid key = 0; \/\/ Int or string.\n\nvoid value = get(void array_pointer, key); \/\/ Can return any type.\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Returns value at <code>key<\/code>, or <code>NULL()<\/code> if the entry does not exist.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>String-Keyed<\/strong> \u2013 Retrieves by string key.<\/li>\n\n\n\n<li><strong>Indexed<\/strong> \u2013 Retrieves by integer index.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">isArray()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint value = isarray(void object_pointer);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Version 4.0+ only. Returns <code>1<\/code> if pointer is an array. <code>0<\/code> otherwise.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">isFirst()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint value = isfirst(void array_pointer);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-keyed only. Returns <code>1<\/code> if the internal cursor is at first node. <code>0<\/code> otherwise.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">islast()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint value = islast(void array_pointer);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-Keyed only.Returns <code>1<\/code> if the internal cursor is at last node. <code>0<\/code> otherwise.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">key()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid value = key(void array_pointer); \/\/ String.\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-Keyed only. Returns the string key at current cursor position.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">next()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint success = next(void array_pointer);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-Keyed only. Attempts to move cursor to next node in order. Returns <code>1<\/code> on success, <code>0<\/code> if already at last node.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">previous()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint success = previous(void array_pointer);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-keyed only. Attempts to move cursor to previous node in order. Returns <code>1<\/code> on success, <code>0<\/code> if already at first node.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">reset()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint success = reset(void array_pointer);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-Keyed only. Attempts to move cursor to first node in order. Returns <code>1<\/code> on success, <code>0<\/code> if array has no nodes.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">set()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid key = 0; \/\/ Int or string.\nvoid value = 0; \/\/ Any type.\n\nset(void array_pointer, index, value);\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indexed \u2013<\/strong> Set target <code>key<\/code> to value. If <code>key<\/code> does not exist, resizes array to accommodate <code>key<\/code> before setting value.<\/li>\n\n\n\n<li><strong>String-Keyed \u2013<\/strong> Creates a named entry for <code>key<\/code> with <code>value<\/code>, or updates <code>value<\/code> if <code>key<\/code> already exists. Same behavior as <code>add()<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For indexed arrays, prefer <code>set()<\/code> over <code>add()<\/code> when you already know the slot you want to write.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">size()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint array_size = size(void array_pointer);\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indexed \u2013<\/strong> Returns current number of elements in the array.<\/li>\n\n\n\n<li><strong>String-Keyed \u2013<\/strong> Returns number of named entries.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-orange-color has-text-color has-link-color wp-elements-d0c934517e9cc982d34f68a6d001ddec wp-block-paragraph\">Caution: If you mistakenly allocate <code>>0<\/code> entries to an array you intend for string keys, <code>size()<\/code> will return that allocated number of elements until you add or set a string-keyed node. Always allocate <code>0<\/code> for string-keyed arrays, and never mix key types.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">value()\u200b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid value = value(void array_pointer); \/\/ Any type.\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">String-Keyed only.Returns value at current cursor position.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cleanup<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Deleting Variables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To delete any variable, assign it a <code>NULL()<\/code> value. OpenBOR will destroy the variable and free the memory it used.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nsetglobalvar(&quot;some_name&quot;, NULL())\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">You can also remove an string-keyed array element by setting its value to <code>NULL()<\/code>. The node will remain allocated until the array is freed, but OpenBOR will treat it as removed for purposes of <code>size()<\/code> and iteration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Indexed array elements can cleared, but not removed with <code>NULL()<\/code>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Freeing Objects<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the variable was pointing to an object of some type (i.e. a pointer), you may first want to delete the object it is pointing to. Otherwise, the object continues to exist in memory with no variable to reference it. To do this, use the <code>free()<\/code> function. In the example below, we load a sprite into memory, then delete the sprite, and afterward delete the variable that pointed to it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid load_sprite() {\n\n\/* Load sprite into memory and get its pointer so we can work with it. *\/\n    void sprite = loadsprite(&quot;data\/some_folder\/some_sprite.png&quot;);\n\n\/* Store the pointer into a localvar so other functions can use it. *\/\n    setlocalvar(&quot;sprite_name&quot;, sprite);\n\n}\n\nvoid free_sprite() {\n\n\/* Get the sprite pointer so we can work with it. *\/\n    void sprite = getlocalvar(&quot;sprite_name&quot;);\n\n\/* Remove the sprite from memory. *\/\n    free(sprite);\n\n\/* Delete the variable that held sprite pointer. *\/\n    setlocalvar(&quot;sprite_name&quot;, NULL());\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Limitations<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Variables In Use<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There is no hard code limit for the number of variables you may have in use. It&#8217;s still a good idea to only allocate what you need and clean up what you don&#8217;t, since every active variable consumes memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Variable Length<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prior to release 4539, variables in OpenBOR Script were limited to 63 characters in length. Note that&#8217;s <em>length<\/em>, not <em>value<\/em> &#8211; so it really only matters for strings. Should the length exceed 63, the remaining characters are truncated and lost. As of 4539+ the limit is now 127.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables are named identifiers that contain a value. They are a basic building block of script.<\/p>\n","protected":false},"author":1,"featured_media":1322,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[328,2],"tags":[383,388,387,376,385,372,371,373,396,395,394,393,375,390,391,386,370,369,374,384,377,366,368,378,379,381,380,382,365,392,389,4,3],"class_list":["post-4","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-openbor","category-script","tag-array","tag-func_add","tag-obor_func_delete","tag-obor_func_free-2","tag-obor_func_get","tag-obor_func_getentityvar-2","tag-obor_func_getglobalvar-2","tag-obor_func_getlocalvar-2","tag-obor_func_isarray","tag-obor_func_isfirst","tag-obor_func_islast","tag-obor_func_key","tag-obor_func_null-2","tag-obor_func_previous","tag-obor_func_reset","tag-obor_func_set","tag-obor_func_setentityvar-2","tag-obor_func_setglobalvar-2","tag-obor_func_setlocalvar-2","tag-obor_func_size","tag-obor_func_strinfirst","tag-obor_func_string_to_float","tag-obor_func_string_to_int","tag-obor_func_strinlast","tag-obor_func_strleft","tag-obor_func_strlength","tag-obor_func_strright","tag-obor_func_strwidth","tag-obor_func_typeof-2","tag-obor_func_value","tag-next","tag-openbor-script","tag-variables"],"revision_note":"","jetpack_featured_media_url":"https:\/\/i0.wp.com\/chronocrash.com\/obor\/wiki\/wp-content\/uploads\/2023\/12\/c_scripting.png?fit=100%2C100&ssl=1&wsr","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":4,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":1463,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/posts\/4\/revisions\/1463"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/media\/1322"}],"wp:attachment":[{"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}