Introduction to GeoGebraScript
Sometimes the ways students can interact with your worksheets using the basic features provided by GeoGebra are not powerful enough or too difficult to be used by your students, especially younger ones. [b]GeoGebraScript is there to help you creating simple-to-use constructions with more sophisticated interactivity.[/b] It is a simple way to write [url=https://wiki.geogebra.org/en/Scripting]scripts[/url] (small programs) in GeoGebra.
Common uses of scripting are:[list][*]Checking student's answers by letting them enter their answers into textfields and using scripts to check if they are correct.[/*][*]Creating new tasks / challenges after the student pressed a button, either randomly generated or using a set of challenges defined by you.[/*][*]Starting animations or revealing new parts of the construction after the student pressed a button or finished his task.[/*][*](and so on..)[/*][/list]
There are two scripting languages in GeoGebra, GeoGebraScript and JavaScript, but this introduction focuses on GeoGebraScript as it uses the GeoGebra command syntax and is thus much easier to understand and learn for GeoGebra users. There are some differences between the scripting languages feature-wise, but GeoGebraScript is powerful enough for most situations.
[b]Note:[/b] Scripting is a topic intended for experienced users of GeoGebra, we recommend you to take a look at the [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Tutorial:Main_Page]other tutorials[/url] before you dive into this topic if you're not that familiar with GeoGebra at the moment. (The basics about how scripting works and how you can add scripts to your constructions are explained here of course.)
There are not only tools to create an object in GeoGebra. You can do the same with [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Commands]commands[/url] and the input-bar.[br][br][b]Example:[/b] With the command [code]Circle((1,2),3)[/code] you'll get a circle with the center at (1,2) with a radius 3.[br][br]Even if you use tools to create an object GeoGebra uses commands in the background to actually create the object. To see the command used to create an object can be found in the [url=https://wiki.geogebra.org/en/Properties_Dialog]Properties Dialog[/url] under [b]Definition[/b]. So tools are more or less only a help to enter those commands with the mouse. A list of all commands to create an object can be found [url=https://wiki.geogebra.org/en/Category:Commands]here[/url].[br][br]We've only spoken about commands that create objects so far. The majority of commands create objects, but there are some commands intended to be used in scripts, something we will talk about in more detail later (for the curious, see [url=https://wiki.geogebra.org/en/Scripting_Commands]Scripting Commands[/url]).
Scripts are, at heart, just a sequence of GeoGebra commands executed in the following two situations:[list][*][i]Click:[/i] A script is executed after the user clicked on the object.[/*][*][i]Update:[/i] A script is executed when the value or properties of the object changed. This may happen if the user moves elements of the construction around, changes sliders, etc.[/*][/list]Scripting in GeoGebra is object-centered, which means that you can define scripts for each object –one script for clicks, one for updates–, but no script can exist without an object (there is one exception for scripting using JavaScript).[br][br]To add scripts to an object just right-click the object in either the [url=https://wiki.geogebra.org/en/Algebra_View]Algebra[/url] or [url=https://wiki.geogebra.org/en/Graphics_View]Graphics View[/url], click the [i]Object Properties…[/i] item and select the [i]Scripting[/i] tab. Three more tabs will appear, [i]On Click[/i], [i]On Update[/i] and [i]Global JavaScript[/i]. The third one won't be of interest in this tutorial, but the two others can be used to add [i]Click[/i] and [i]Update[/i] scripts to the object you selected.
Now it's time to actually create a script, we will create a silly example using an [i]Update[/i] script first:[br][br][b]Example:[/b] (Update script, you can also find the whole construction in [url=https://www.geogebra.org/material/show/id/1519]GeoGebra[/url])[br][br]1. First of all, open a blank GeoGebra 4 window.[br][br]2. Now create a point "A" and a line "a". Try out the [url=https://wiki.geogebra.org/en/Line_Command]Line(..) command[/url] if you're not familiar with creating objects from the [url=https://wiki.geogebra.org/en/Input_Bar]Input Bar[/url].[br][br]3. Open the [i]On Update[/i] tab of the scripting panel as described in the paragraph above.[br][br]4. Now enter[br][br][code]SetLineThickness(a, Distance(A, a)*2)[br]SetPointSize(A, Distance(A,y=0))[/code][br][br]as a script, click the [i]OK[/i] button (very easy to miss!) and close the dialog.[br][br]5. Try to figure out what the script does, then check your assumption by moving the point A around!
As you may have noticed we assigned the [i]update[/i] script to the point, not e.g. the line. This means that the script is executed every time you move the point. It doesn't mean, however, that the script must modify any attribute of the point itself.[br][br]We've been using the [url=https://wiki.geogebra.org/en/SetLineThickness_Command]SetLineThickness[/url] and [url=https://wiki.geogebra.org/en/SetPointSize_Command]SetPointSize[/url] commands, two of those commands intended for scripting we've talked about earlier. At some point you should at least skim the [url=https://wiki.geogebra.org/en/Scripting_Commands]scripting command list[/url] to get an idea what's possible with these commands. You're not restricted to this kind of command though, you can e.g. use [code]f(x) = x^2[/code] or [code]c = Circle((0,0), 5)[/code] in your scripts as well.[br][br]Apart from some special features, described in more detail below, executing a GeoGebraScript has the same effect as entering the single lines of the script into the input bar. You now basically know how to write scripts in GeoGebra, the rest of this tutorial will be about some special objects for scripting like textfields or buttons. Apart from the scripting commands the most useful one is probably [url=https://wiki.geogebra.org/en/If_Command]If(...)[/url], you should look at the other [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Logic_Commands]logic[/url] and [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/List_Commands]list commands[/url] as well if you did not already.[br][br][b]Note:[/b] Many scripting commands just perform an action once and do not form a permanent connection as you may know them from [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Free,_Dependent_and_Auxiliary_Objects]dependent objects[/url], so e.g. [code]SetLineThickness(a, Distance(A, a)*2)[/code] will just copy the current value of [code]Distance(A,a)*2[/code] at the time of the execution, afterwards that value will not change unless you execute the command again or change the thickness manually. Our example may make the impression that there is some kind of connection, but that's just because the script is executed every time the point is moved. You can see that by moving the line instead. The line thickness will not change unless you move the point again.[br][br][b]Note:[/b] If you're familiar with other programming languages you may have noticed that there is no need for a semicolon at the end of a line in GeoGebraScript.
Buttons are great because all your students know that they can click them and will try to do that. Apart from that buttons don't have any special behavior, so a [i]click[/i] script of a button is as good as a [i]click[/i] script of any other object. You can create a button using the [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/File:Tool_Insert_Button.gif][img width=32,height=32]https://web.archive.org/web/20221031030255im_/https://wiki.geogebra.org/uploads/4/44/Tool_Insert_Button.gif[/img][/url] [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Button_Tool]Button Tool[/url].
Input boxes (also called textfields), see the [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/File:Tool_Insert_Textfield.gif][img width=32,height=32]https://web.archive.org/web/20221031030255im_/https://wiki.geogebra.org/uploads/4/4e/Tool_Insert_Textfield.gif[/img][/url] [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Input_Box_Tool]Input Box Tool[/url], are maybe the most powerful objects in GeoGebra. They allow your students to enter text, numbers, functions and every other input you can (manually) convert from text to some GeoGebra objects.There are multiple ways how you can use input boxes in your construction, you can either link them with objects, associate a click script to them or process them later using another object (e.g. a button). Don't worry, everything will be explained in detail right now.
[i]Linked input boxes[/i] are input boxes which are linked to an existing GeoGebra object: That means that the input box shows the current value of the object and if you change the value in the input box the original object will change its value as well. You can use that to let your students change function definitions or numbers without using sliders. Basically, they are input boxes which can just change a single object and thus much easier to use and without any danger of students damaging their worksheets.[br][br]The following example illustrates how you can create linked input boxes in GeoGebra.[br][br][b]Example:[/b] (Linked input boxes, the construction can be found at [url=https://web.archive.org/web/20221031030255/http://www.geogebra.org/material/show/id/1522]GeoGebra[/url] again)[list=1][*]Open a blank GeoGebra window[/*][*]Enter a function, e.g. [code]f(x) = x^2[/code], into the input bar[/*][*]Select the [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/File:Tool_Insert_Textfield.gif][img width=32,height=32]https://web.archive.org/web/20221031030255im_/https://wiki.geogebra.org/uploads/4/4e/Tool_Insert_Textfield.gif[/img][/url] [url=https://web.archive.org/web/20221031030255/https://wiki.geogebra.org/en/Input_Box_Tool]Input Box Tool[/url] in the toolbar (in the second tool group from the right).[/*][*]Now click on some spot in your worksheet, a dialog will pop out. For the caption enter "f(x) =" and as linked object select your function f.[/*][*]You're done. Move the graph of f to see how the textfield will automatically display the new definition of your function. You can also change the textfield and press enter to see how you can change the function's definition using the textfield.[br][/*][/list][b]Note:[/b] As mentioned above you can not only link functions, but also numbers and basically every other object, just try it out.
In contrast to the first example we will now not link the input box to a specific object but use its [i]Click[/i] script to apply its value to other objects (the reason why this is called click script is unknown to the author ;-)). The script will be very similar to the one in the first example, the only new question is how to use the value of the textfield in the script. The answer is [code]%0[/code]. Seriously.[br][br][b]Example:[/b] (Input box with click script, see [url=https://www.geogebra.org/material/show/id/VZBT2U4t]GeoGebra[/url])[br][br]1. Open a blank GeoGebra window[br][br]2. Create a point [code]A[/code] and a line [code]a[/code].[br][br]3. Select the input box tool and click onto the canvas (as above), but this time use the label "Size:" and leave the "Linked Object" field empty.[br][br]4. Go to the [i]Click[/i] script tab of the new input box (instructions how to do that way above).[br][br]5. Now add the following script (note the [code]%0[/code]):[br][br]SetPointSize(A, %0/2)[br]SetLineThickness(a, %0)[br][br]6. Click "Ok" again and close the dialog.[br][br]7. Now enter a new value into the input box and press enter.
[b]Buttons: Increment/Decrement Buttons[/b][br][br]Instead of using a slider to go through a step-by-step explanation, you can use a button.[br][br][b]Example:[/b][list][*]Create an integer slider, called [code]Steps[/code].[/*][*]Create a Button using Button Tool[/*][*]Enter for caption the visible label on the button[/*][*]Enter the script [code]SetValue(Steps,Steps+1)[br][/code][/*][/list][br]To decrement change "+" to "-". To change the size of the steps change 1 to your value.
If you know something (a little) about scripting: Your help and participation is very welcome! You could add something for the following topics or think of something else:[list][*]More step-by-step instructions for beginners[/*][*]More concrete examples (also links to GeoGebra files using scripting)[/*][/list]