Tutorial:Introduction to GeoGebraScript

From GeoGebra Manual
Jump to: navigation, search

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. GeoGebraScript is there to help you creating simple-to-use constructions with more sophisticated interactivity. It is a simple way to write scripts (small programs) in GeoGebra.

Common uses of scripting are:

  • 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..)

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.

Note: Scripting is a topic intended for experienced users of GeoGebra, we recommend you to take a look at the other tutorials 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.)

Background: Commands

There are not only tools to create an object in GeoGebra. You can do the same with commands and the input-bar.

Example: With the command Circle[(1,2),3] you'll get a circle with the center at (1,2) with a radius 3.

Even if you use the tools to create an object, the command to get this object via input bar can be found in the Properties Dialog behind Definition. 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 here.

But there are some commands that do not create an object. These commands are especially for scripting. See Scripting_Commands.

Every object can be used to start an objects. Clicking or moving a point can be used too. But there are some objects that are created specially to use it for scripts: buttons and textfields.

Basics of Scripting

Scripts are, at heart, just a sequence of GeoGebra commands executed in the following two situations:

  • Click: A script is executed after the user clicked on the object.
  • Update: 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.

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).

To add scripts to an object just right-click the object in either the Algebra or Graphics View, click the Object Properties… item and select the Scripting tab. Three more tabs will appear, On Click, On Update and Global JavaScript. The third one won't be of interest in this tutorial, but the two others can be used to add Click and Update scripts to the object you selected.

Click to see larger version


If you know something (a little) about scripting: You're help and participation is very welcome! You could add something for the following topics or think of something else:

  • More step-by-step instructions for beginners
  • Explanation of "scripting objects": Textfields (!!), buttons
  • More concrete examples (also links to GeoGebra files using scripting)


Increment/Decrement Buttons

Instead of using a showing a slider to go through a step-by-step explanation, you can use a button.

  • Create an integer slider, called Steps.
  • Create a Button using Button Tool
  • Enter for caption the visible label ob the button
  • Enter the script SetValue[Steps,Steps+1] or simply Steps=Steps + 1

To decrement change "+" to "-". To change the size of the steps change 1 to your value.

Note: The command Steps=Steps + 1 is only available in scripting. If you enter a command like that in the input bar, you'll get an error circular definition
© 2022 International GeoGebra Institute