{"id":181,"date":"2021-07-25T22:33:25","date_gmt":"2021-07-26T03:33:25","guid":{"rendered":"https:\/\/chronocrash.com\/obor\/wiki\/?p=181"},"modified":"2023-12-28T12:03:08","modified_gmt":"2023-12-28T17:03:08","slug":"constants","status":"publish","type":"post","link":"https:\/\/chronocrash.com\/obor\/wiki\/constants\/","title":{"rendered":"Constants"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Constants are named values just like <a href=\"https:\/\/chronocrash.com\/obor\/wiki\/variables\/\" data-type=\"post\" data-id=\"4\">variables<\/a>, except constants never change while the engine is running (hence the name). Constants provide a way to give any value a human readable label without using memory to store it as a variable. They are highly valuable for lists of static values, and as a tool for making code easier to read.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OpenBOR relies heavily on constants. Nearly every list of items, properties, or values is comprised of named constants. OpenBOR script also enables creators to define their own constants.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Predefined<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are innumerable predefined constants used by the OpenBOR engine. Constants exposed for script use are accessible with the <code>openborconstant(char &lt;name&gt;)<\/code> function. Constant names are strings, so enclose them in quotes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Get the ANI_IDLE constant value: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint i = openborconstant(&quot;ANI_IDLE&quot;);\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Optimizing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is very important to use <code>openborconstant()<\/code> as documented. This is because <code>openborconstant()<\/code> is an expensive function requiring a lengthy internal string search. However, if written correctly OpenBOR can completely eliminate the in game CPU load by pre-processing <code>openborconstant()<\/code> calls. See the following examples:<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-337f1c9428642e8fb83f31fa8f2762fd wp-block-paragraph\"><strong>Incorrect<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar animation_name = &quot;ANI_IDLE&quot;\n\nint i = openborconstant(animation_name);\n<\/pre><\/div>\n\n\n<p class=\"has-vivid-green-cyan-color has-text-color has-link-color wp-elements-2f90d4dd4f179947709fe8b2c80ede6a wp-block-paragraph\"><strong>Correct<\/strong> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint i = openborconstant(&quot;ANI_IDLE&quot;);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In most other functions, the <strong>incorrect<\/strong> example is best practice. But <code>openborconstant()<\/code> is unique. Assuming you provide the name argument directly as in <strong>correct<\/strong> example, the <code>openborconstant()<\/code> function is processed as the module loads. It essentially becomes a &#8220;free&#8221; function that costs no memory or CPU time at all. In the incorrect example, the engine has no way to know what the constant argument is until the parent function runs. Consider the following practical example, performing an animation switch:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Function <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid do_something_cool(char animation_name, int special_number) \n{\n     ...do some other stuff...\n\n     \/\/ Change animation.\n     performattack(entity, openborconstant(animation_name + special_number), 1);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Model text <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@cmd do_something_cool \"ANI_FREESPECIAL\" 1<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code is a common misuse of <code>openborconstant()<\/code>. It will work, but it is unnecessarily expensive. There is a pointless string concatenation and the engine is unable to process <code>openborconstant()<\/code> until the moment it runs. Try a more optimal approach like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Function <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid do_something_cool(int animation_id) \n{\n     ...do some other stuff...\n\n     \/\/ Change animation.\n     performattack(entity, animation_id, 1);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Model text<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@cmd do_something_cool openborconstant(\"ANI_FREESPECIAL\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code eliminates the string concatenation and allows OpenBOR to process <code>openborconstant()<\/code> into the actual constant value during engine start up. When the animation function runs, it simply receives an animation ID and acts on it without wasting any resources on extra calculations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">User Defined<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">OpenBOR script supports the #define directive, enabling user defined macros and constants. A define comprises three parts:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Definition:<\/strong> Always \u201c#define\u201d.<\/li>\n\n\n\n<li><strong>Identification:<\/strong> This is the name of your macro (in this case, the constant).<\/li>\n\n\n\n<li><strong>Token:<\/strong> The value. This is what the computer will interpret whenever the constant is encountered in code.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define SOME_IDENTIFICATION somevalue\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Once defined, the constant is available through the entirety of the script&#8217;s scope. In the case of animation scripts, the constant becomes available within the model&#8217;s text for use with <em>@script<\/em> and <em>@cmd<\/em>. You do not need a function to access a user defined constant. On startup, OpenBOR replaces all instance of the constant&#8217;s identifier with its defined value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define SOME_IDENTIFICATION somevalue\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Define and access static variables for use with script.<\/p>\n","protected":false},"author":1,"featured_media":1322,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[328,2],"tags":[141,139,500,5,4],"class_list":["post-181","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-openbor","category-script","tag-define","tag-constant","tag-obor_func_openborconstant","tag-manual","tag-openbor-script"],"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\/181","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=181"}],"version-history":[{"count":5,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":1328,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/posts\/181\/revisions\/1328"}],"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=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chronocrash.com\/obor\/wiki\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}