{"id":382,"date":"2012-12-28T19:48:42","date_gmt":"2012-12-28T18:48:42","guid":{"rendered":"http:\/\/www.valcon.it\/gamemaker\/?p=382"},"modified":"2012-12-28T19:48:42","modified_gmt":"2012-12-28T18:48:42","slug":"script","status":"publish","type":"post","link":"http:\/\/www.valcon.it\/gamemaker\/script\/","title":{"rendered":"Script"},"content":{"rendered":"<p>Game Maker has a built-in programming language.<br \/>\nOnce you become more familiar with Game Maker and want to use it to its fullest extend, it is advisable to start learning to use this language.<br \/>\nThere are two ways to use the language.<\/p>\n<ul>\n<li>First of all you can create scripts.<\/li>\n<\/ul>\n<div>These are pieces of code to which you give a name.<br \/>\nThey are shown in the resource tree and can be saved to a file and loaded from a file.<br \/>\nThey can be used to form a library that extends the possibilities of Game Maker.<\/div>\n<ul>\n<li>Alternatively, you can add a code action to some event and type a piece of code there.<\/li>\n<\/ul>\n<div>Adding code actions works in exactly the same way as adding scripts except for two differences.<br \/>\nCode actions don&#8217;t have a name and cannot use arguments.<br \/>\nAlso they have the well-known field to indicate to what objects the action should apply.<br \/>\nFor the rest you enter code in exactly the same way as in scripts.<\/div>\n<p>So we further concentrate on scripts in this chapter.<\/p>\n<p>As stated before, a script is written with code in GML (the built-in programming language) and is meant to perform a particular task.<br \/>\nScripts can takes input-variables called arguments (sometimes called parameters).<br \/>\nTo execute a script from any event, you can use either the script action or you can use code.<br \/>\nIn the Script-action you specify the script you want to execute, together with the up to five arguments.<br \/>\nIf you use a script from within a piece of code it is done the same way you call a GM-function.<br \/>\nIn that case you can use up to 16 arguments.<\/p>\n<p>Scripts can return one value.<br \/>\nThis is often used to build calculating methods (mathematical methods).<br \/>\nThe return keyword is used for this.<br \/>\n<strong>No code after the return keyword is executed!<\/strong><br \/>\nWhen a script returns a value, you can also use it as a function when providing values in other actions.<\/p>\n<p>To create a script in your game, choose\u00a0<kbd>Create Script<\/kbd>\u00a0from the\u00a0<kbd>Resources<\/kbd>\u00a0menu.<br \/>\nThe following form will pop up (in the example we already added a little script that computed the product of the two arguments).<\/p>\n<p>(Actually, this is the built-in script editor. In the preferences you can also indicate that you want to use an external editor.)<br \/>\nAt the top right you can indicate the name of the script.<br \/>\nYou have a little editor in which you can type the script.<br \/>\nNote that at the bottom a list is shown of all functions, built-in variables, and constants.<br \/>\nThis helps you find the one you need.<br \/>\nYou can double click one to add it (or use &lt;Ctrl&gt;P for the same purpose.)<br \/>\nShowing this list can be switched off in the preferences.<br \/>\nThe editor has a number of useful properties, many available through buttons (press the right mouse button for some additional commands):<\/p>\n<p>Multiple undo and redo either per key press or in groups (can be changed in the preferences)<\/p>\n<p>Intelligent auto indent that aligns with the previous line (can be set in the preferences)<\/p>\n<p>Intelligent tabbing that tabs till the first non space in the previous lines (can be set in the preferences)<\/p>\n<p>Use &lt;Ctrl&gt;I to indent selected lines and &lt;Shift&gt; &lt;Ctrl&gt;I to unindent selected lines<\/p>\n<p>Cut and paste<\/p>\n<p>Search and replace<\/p>\n<p>Use &lt;Ctrl&gt; + up, down, page-up, or page-down to scroll without changing the cursor position<br \/>\nUse F4 to open the script or resource whose name is at the cursor position (does not work in the code action; only in scripts)<\/p>\n<h2 id=\"hn_Saving_and_loading_the_script_as_a_text_file\">Saving and loading the script as a text file<\/h2>\n<p>Also there is a button with which you can test whether the script is correct.<br \/>\nNot all aspects can be tested at this stage but the syntax of your script will be tested, together with the existence of functions used.<br \/>\nAs you might have noticed, parts of the script text are colored.<br \/>\nThe editor knows about existing objects, built-in variables and functions, etc.<br \/>\nColor- coding helps a lot in avoiding mistakes.<br \/>\nIn particular, you see immediately if you misspelled some name or use a keyword as a variable.<br \/>\nColor-coding is though a bit slow.<br \/>\nIn the preferences in the file menu you can switch color- coding on and off.<br \/>\nHere you can also change the color for the different components of the programs.<br \/>\n(If something goes wrong with color coding, press F12 twice, to switch it off and back on.)<br \/>\nAlso you can change the font used in scripts and code.<\/p>\n<p>Scripts are extremely useful to extend the possibilities of Game Maker.<br \/>\nThis does though require that you design your scripts carefully.<br \/>\nScripts can be stored in libraries that can be added to your game.<br \/>\nTo import a library, use the item Import scripts from the file menu.<br \/>\nTo save your scripts in the form of a library use Export scripts.<br \/>\nScript libraries are simple text files (although they have the extension .gml).<br \/>\nPreferably don&#8217;t edit them directly because they have a special structure.<br \/>\nSome libraries with useful scripts are included.<br \/>\n(To avoid unnecessary work when loading the game, after importing a library, best delete those scripts that you don&#8217;t use.)<\/p>\n<p>When creating scripts you can easily make mistakes.<br \/>\nAlways test the scripts by using the appropriate button.<br \/>\nWhen an error occurs during the execution of a script this is reported, with an indication of the type of error and the place.<br \/>\nRarely you will see a popup with the text &#8220;Unexpected error occurred during the game&#8221;<br \/>\nThis error message indicate that some problem occurred in windows or in the hardware.<br \/>\nOften the reason for this is infinite recursion, lack of memory or insufficient hardware, drivers or firmware.<br \/>\nGenerally speaking, these errors have to do with problems outside the gm-environment.<br \/>\nIf you need to check things more carefully, you can run the game in debug mode.<br \/>\nNow a form appears in which you can monitor lots of information in your game.<\/p>\n<p>Under the Run menu you can pause the game, run it step by step and even restart it.<br \/>\nUnder the Watch menu you can watch the value of certain expressions.<br \/>\nUse Add to type in some expression whose value is shown in each step of the game.<br \/>\nIn this way you can see whether your game is doing things the right way.<br \/>\nYou can watch many expressions.<br \/>\nYou can save them for later use (e.g. after you made a correction to the game).<br \/>\nUnder the Tools menu you find items to see even more information.<br \/>\nYou can see a list of all instances in the game, you can watch all global variables (well, the most important ones) and the local variables of an instance (either use the object name or the id of the instance).<br \/>\nYou can also view messages which you can send from your code using the function show_debug_message(str).<br \/>\nFinally you can give the game commands and change the speed of the game.<br \/>\nIf you make complicated games you should really learn how to use the debug options.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Game Maker has a built-in programming language. Once you become more familiar with Game Maker and want to use it to its fullest extend, it is advisable to start learning to use this language. There are two ways to use the language. First of all you can create scripts. These are pieces of code to &#8230; <a title=\"Script\" class=\"read-more\" href=\"http:\/\/www.valcon.it\/gamemaker\/script\/\" aria-label=\"Leggi di pi\u00f9 su Script\">Leggi tutto<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/posts\/382"}],"collection":[{"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":0,"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.valcon.it\/gamemaker\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}