Skip to content

Instantly share code, notes, and snippets.

@rveitch
Last active February 1, 2025 00:42
Show Gist options
  • Select an option

  • Save rveitch/a10ca644e83684e4094fb9f4ba4d0e98 to your computer and use it in GitHub Desktop.

Select an option

Save rveitch/a10ca644e83684e4094fb9f4ba4d0e98 to your computer and use it in GitHub Desktop.
Screeps Javascript Basics

Types of Things

String

Basically text.

var screepName = 'screepalicious';

Array: [ ]

Basically a list or group of things, which can be of varying types.

var myDumbArray = ['a', 'b', 'c', 1, 2, 3, screepName];

Each item in the array has an index number. Index numbers start at 0 (not 1), so if you wanted to reference the letter a (the first item in the array above), it would look like myDumbArray[0]. For example:

console.log(myDumbArray[0]); // 'a'
/* or */
var someNewThing = myDumbArray[3]; // 1
/* or */
var someOtherNewThing = myDumbArray[6]; // 'screepalicious' (the value of screepName)

Object: { }

It's...an object!

var myDumbObject = {
  temperature: 68,
  humidity: 40,
  name: 'bob',
};

Objects are made up of key/value pairs (properties). In the object above, temperature would be the key, 68 would be the value. You can access or reference properties like this:

var howColdIsIt = myDumbObject.temperature; // 68

Objects can have many levels of nested values.

var config = {
    roles: {
      harvesters: {
        population: 2,
        preferSource: 1,
      },
      upgraders: {
        population: 3,
        preferSource: 0,
      },
    }
};

If we wanted to get the value of population for the harvester role of the In the object above, it would be:

var harvesterPop = config.roles.harvesters.population; // 2

Logging to Console

Here's a few examples of ways you can use console log.

var screepName = 'screepalicious';
var creep = {
  name: screepName,
  role: 'harvester',
};

console.log(screepName); // screepalicious
console.log('The creeps name is: ', screepName); // The creeps name is: screepalicious
console.log(`The creeps name is: ${screepName}`); // The creeps name is: screepalicious
console.log(JSON.stringify(creep)); // NOTE: objects need to be converted to strings first to log.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment