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, notvar. (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 = 2Local 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' hereGlobal variables are not accessible by default. There is no built-in
globalobject, 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 usei += 1andi -= 1instead. (See Operators)Catspeak reserves
--for comments. (See Operators and Comments)Catspeak reserves
//for integer division. (See Operators and Comments)The ternary operator
condition ? a : bdoes not exist in Catspeak, instead the If Expression should be usedif a > b { a } else { b }for,repeat, anddoloops do not exist. Instead you should use While Loops.switchdoes not exist. Instead you should use Match Expressions.There are no
[@and[$accessors, both structs and arrays can be indexed usinga[i]. (See Accessor Expressions)There are no
[#,[?, and[|accessors. Data structures should use functions likeds_list_getandds_list_set, if exposed by the modding API.