Creating and instantiating classes

In Studio, create a class by right-clicking a package name on the Project pane, and then clicking New > Gosu Class. Studio creates a simple class in which you build the functionality that you need. Studio creates the package name and class definition.

After creating a new class, add constructors, class variables, properties, and functions to the class. All of these components are optional. Within a class, functions are also called methods. This term is standard object-oriented terminology. This documentation refers to functions as methods if the functions are part of classes.

Add variables to a class with the var keyword:

var myStringInstanceVariable : String

You can optionally initialize the variable:

var myStringInstanceVariable = "Butter"

Define methods with the keyword function followed by the method name and parentheses that enclose a parameter list, which is empty if the method has no arguments. The parameter list uses commas to separate multiple parameters. Each parameter has the format:

parameterName : typeName

If the method returns a value, after the parentheses, put a colon (:) and the return type. If the method does not return a value, specifying void as the return type is optional.

For example, the following line declares a method that has a single String parameter. The method does not return a value:

function doAction(arg1 : String)

A Gosu class with one instance variable and one public method looks like the following:

class MyClass {
  var myStringInstanceVariable : String

  public function doAction(arg1 : String) {
    print("Someone just called the doAction method with arg " + arg1)
  }
}