Skip to content

Latest commit

 

History

History
106 lines (80 loc) · 1.36 KB

EXAMPLES.md

File metadata and controls

106 lines (80 loc) · 1.36 KB

Examples

Importing external files

helpers.gs

function add(number x, number y) {
  return x + y;
}

main.gs

import "helpers.gs" as helpers

number value = helpers.add(5, 3);

print(value);

Matrix math

number w = 640;
number h = 480;
number aspect = w / h;

mat4 model = identity();
mat4 view = identity();
mat4 projection = perspective(radians(60.0), aspect, 0.1, 100.0);

mat4 mvp = projection * view * model;

vec3 point = vec3(1, 2, 3);
vec4 p = mvp * vec4(point.xyz, 1);

Writing a file

file f = file.open("test.txt", "w+");

f.write("hello!");

f.close();

Reading a file

file f = file.open("bible.txt", "r+");

iterator it = f.readLines();

while (string x = it.next()) {
  print(x);
}

f.close();

Struct with function

typedef struct {
  function bark() {
    print("Woof woof!");
  }
} Dog;

Dog dog = Dog();

dog.bark();

Struct and self keyword

typedef struct {
  string name;

  function changeName(string newName) {
    self.name = newName;
  }
} Person;


Person p = Person("John Doe");

print(p.name);

p.changeName("David Doe");

print(p.name);

Arbitrary objects

object x = {
  "hello": 123,
  "nested": {
    "yo": 42,
    "other": {
      name: "John"
    }
  }
};

x.hello = 33;
number y = x.hello;
print(y);
print(x.nested.other.name);