Game

The main global game object containing all the game play information.

Game.constructionSitesobject<string, ConstructionSite>

A hash containing all your construction sites with their id as hash keys.

Game.cpuobject

An object containing information about your CPU usage with the following properties:

parametertypedescription
limitnumber

Your assigned CPU limit for the current shard.

tickLimitnumber

An amount of available CPU time at the current game tick.
Usually it is higher than Game.cpu.limit. Learn more

bucketnumber

An amount of unused CPU accumulated in your bucket.

shardLimitsobject
<string,number>

An object with limits for each shard with shard names as keys. You can use setShardLimits method to re-assign them.

unlockedboolean

Whether full CPU is currently unlocked for your account.

unlockedTimenumber

The time in milliseconds since UNIX epoch time until full CPU is unlocked for your account. This property is not defined when full CPU is not unlocked for your account or it's unlocked with a subscription.

Game.creepsobject<string, Creep>

for(const i in Game.creeps) {
    Game.creeps[i].moveTo(flag);
}

A hash containing all your creeps with creep names as hash keys.

Game.flagsobject<string, Flag>

creep.moveTo(Game.flags.Flag1);

A hash containing all your flags with flag names as hash keys.

Game.gclobject

Your Global Control Level, an object with the following properties :

parametertypedescription
levelnumber

The current level.

progressnumber

The current progress to the next level.

progressTotalnumber

The progress required to reach the next level.

Game.gplobject

Your Global Power Level, an object with the following properties :

parametertypedescription
levelnumber

The current level.

progressnumber

The current progress to the next level.

progressTotalnumber

The progress required to reach the next level.

Game.mapobject

A global object representing world map. See the documentation below.

Game.marketobject

A global object representing the in-game market. See the documentation below.

Game.powerCreepsobject<string, PowerCreep>

Game.powerCreeps['PC1'].moveTo(flag);

A hash containing all your power creeps with their names as hash keys. Even power creeps not spawned in the world can be accessed here.

Game.resourcesobject

An object with your global resources that are bound to the account, like pixels or cpu unlocks. Each object key is a resource constant, values are resources amounts.

Game.roomsobject<string, Room>

A hash containing all the rooms available to you with room names as hash keys. A room is visible if you have a creep or an owned structure in it.

Game.shardobject

An object describing the world shard where your script is currently being executed in.

parametertypedescription
namestring

The name of the shard.

typestring

Currently always equals to normal.

ptrboolean

Whether this shard belongs to the PTR.

Game.spawnsobject<string, StructureSpawn>

for(const i in Game.spawns) {
    Game.spawns[i].createCreep(body);
}

A hash containing all your spawns with spawn names as hash keys.

Game.structuresobject<string, Structure>

A hash containing all your structures with structure id as hash keys.

Game.timenumber

console.log(Game.time);

System game tick counter. It is automatically incremented on every tick. Learn more

Game.cpu.getHeapStatistics()

let heap = Game.cpu.getHeapStatistics();
console.log(`Used ${heap.total_heap_size} / ${heap.heap_size_limit}`);

This method is only available when Virtual machine is set to Isolated in your account runtime settings.

Use this method to get heap statistics for your virtual machine. The return value is almost identical to the Node.js function v8.getHeapStatistics(). This function returns one additional property: externally_allocated_size which is the total amount of currently allocated memory which is not included in the v8 heap but counts against this isolate's memory limit. ArrayBuffer instances over a certain size are externally allocated and will be counted here.

Return value

Returns an objects with heap statistics in the following format:

{
  "total_heap_size": 29085696,
  "total_heap_size_executable": 3670016,
  "total_physical_size": 26447928,
  "total_available_size": 319649520,
  "used_heap_size": 17493824,
  "heap_size_limit": 343932928,
  "malloced_memory": 8192,
  "peak_malloced_memory": 1060096,
  "does_zap_garbage": 0,
  "externally_allocated_size": 38430000
}

Game.cpu.getUsed()

if(Game.cpu.getUsed() > Game.cpu.tickLimit / 2) {
    console.log("Used half of CPU already!");
}
for(const name in Game.creeps) {
    const startCpu = Game.cpu.getUsed();

    // creep logic goes here

    const elapsed = Game.cpu.getUsed() - startCpu;
    console.log('Creep '+name+' has used '+elapsed+' CPU time');
}

Get amount of CPU time used from the beginning of the current game tick. Always returns 0 in the Simulation mode.

Return value

Returns currently used CPU time as a float number.

Game.cpu.halt()

Game.cpu.halt();

This method is only available when Virtual machine is set to Isolated in your account runtime settings.

Reset your runtime environment and wipe all data in heap memory.

Game.cpu.setShardLimits(limits)

Game.cpu.setShardLimits({shard0: 20, shard1: 10});

Allocate CPU limits to different shards. Total amount of CPU should remain equal to Game.cpu.shardLimits. This method can be used only once per 12 hours.

parametertypedescription
limitsobject<string, number>

An object with CPU values for each shard in the same format as Game.cpu.shardLimits.

Return value

One of the following codes:
constantvaluedescription
OK0

The operation has been scheduled successfully.

ERR_BUSY-4

12-hours cooldown period is not over yet.

ERR_INVALID_ARGS-10

The argument is not a valid shard limits object.

Game.cpu.unlock()

if(Game.cpu.unlockedTime && ((Game.cpu.unlockedTime - Date.now()) < 1000*60*60*24)) {
    Game.cpu.unlock();
}

Unlock full CPU for your account for additional 24 hours. This method will consume 1 CPU unlock bound to your account (See Game.resources). If full CPU is not currently unlocked for your account, it may take some time (up to 5 minutes) before unlock is applied to your account.

Return value

One of the following codes:
constantvaluedescription
OK0

The operation has been scheduled successfully.

ERR_NOT_ENOUGH_RESOURCES-6

Your account does not have enough cpuUnlock resource.

ERR_FULL-8

Your CPU is unlocked with a subscription.

Game.cpu.generatePixel()

if(Game.cpu.bucket == 10000) {
    Game.cpu.generatePixel();
}

Generate 1 pixel resource unit for 10000 CPU from your bucket.

constantvaluedescription
OK0

The operation has been scheduled successfully.

ERR_NOT_ENOUGH_RESOURCES-6

Your bucket does not have enough CPU.

Game.getObjectById(id)

creep.memory.sourceId = creep.pos.findClosestByRange(FIND_SOURCES).id;
const source = Game.getObjectById(creep.memory.sourceId);

Get an object with the specified unique ID. It may be a game object of any type. Only objects from the rooms which are visible to you can be accessed.

parametertypedescription
idstring

The unique identificator.

Return value

Returns an object instance or null if it cannot be found.

Game.notify(message, [groupInterval])

if(creep.hits < creep.memory.lastHits) {
    Game.notify('Creep '+creep+' has been attacked at '+creep.pos+'!');
}
creep.memory.lastHits = creep.hits;
if(Game.spawns['Spawn1'].energy == 0) {
    Game.notify(
        'Spawn1 is out of energy',
        180  // group these notifications for 3 hours
    );
}

Send a custom message at your profile email. This way, you can set up notifications to yourself on any occasion within the game. You can schedule up to 20 notifications during one game tick. Not available in the Simulation Room.

parametertypedescription
messagestring

Custom text which will be sent in the message. Maximum length is 1000 characters.

groupIntervalnumber

If set to 0 (default), the notification will be scheduled immediately. Otherwise, it will be grouped with other notifications and mailed out later using the specified time in minutes.