Catspeak Reference


Overview

See the pages on Lexical Grammar, Statements, and Expressions for more in-depth information on the Catspeak language syntax.

§ Similarity to GMLtop ^

Catspeak is a simple imperative language with a vaguely similar syntax to GameMaker Language (GML) and JavaScript. Consider the following program written in GML to calculate the factorial of a number n:

GameMaker Language (.gml)
Copy// GML CODE function factorial(n) { var m = 1; if (n > 1) { m = n * factorial(n - 1); } return m; } factorial(5); // output: 120

The same code re-written in the Catspeak programming language looks like:

Catspeak (.meow)
Copy-- CATSPEAK CODE factorial = fun (n) { let m = 1 if (n > 1) { m = n * factorial(n - 1) } return m } factorial(5) -- output: 120

The "shape" of the code is similar to GML, but there are some immediately obvious differences here. For example: the m variable being declared using let instead of var; fun instead of function; and comments using -- instead of //. All of these and more are covered in the Pitfalls section.

§ Pitfallstop ^

Catspeak is not GML, and so you should not assume it will behave identically to GML. This section lists the common differences between GML and the Catspeak programming language:

  • Variables:

    • Local variables in Catspeak are declared using let, not var. (See Let Statements)

    • Local variables and function definitions are not hoisted; that is, using local variables and functions before they are defined. (See Let Statements and Function Expressions)

      Catspeak (.meow)
      Copyshow_mesage(my_var) -- does NOT print '2' let my_var = 2
    • Local variables can only be used inside of the block they are defined in. (See Let Statements)

      Catspeak (.meow)
      Copyif condition { let a = "hello chat" } return a -- does NOT refer to the local variable 'a' here
    • Global variables are not accessible by default. There is no built-in global object, this must be explicitly exposed by the modding API.

    • To access instance variables you need to explicitly write self. (See Self Expressions)

      Catspeak (.meow)
      Copylet a = item -- 'item' refers to a Catspeak variable here let b = self.item -- 'item' refers to a GML instance variable here
  • Syntax:

    • ++ and -- are not valid operators use i += 1 and i -= 1 instead. (See Operators)

    • Catspeak reserves -- for comments. (See Operators and Comments)

    • Catspeak reserves // for integer division. (See Operators and Comments)

    • The ternary operator condition ? a : b does not exist in Catspeak, instead the If Expression should be used if a > b { a } else { b }

    • for, repeat, and do loops do not exist. Instead you should use While Loops.

    • switch does not exist. Instead you should use Match Expressions.

    • There are no [@ and [$ accessors, both structs and arrays can be indexed using a[i]. (See Accessor Expressions)

    • There are no [#, [?, and [| accessors. Data structures should use functions like ds_list_get and ds_list_set, if exposed by the modding API.