Making your own console functions

From Screeps Wiki
Jump to navigation Jump to search

Being able to call functions/commands from the console can be useful, there are a few ways to go about it.

Global

By assigning a function to global, it is possible to access the function from everywhere in your script including console. This does however, mean that the name you choose may cause conflicts down the line if you plan on trying to use it in another part of your script, so choose carefully.

global.someFunctionName(someVarName) = { someCode }; then simply call it in console with someFunctionName(someVarName).

global will persist between ticks, however it will go away on a global reset, which normally happens when uploading new code or during server side resets / issues. Its up to you to keep it refreshed if you want it presistent.

You can learn more about global here: https://docs.screeps.com/contributed/caching-overview.html#Global

Prototype

By setting a prototype of an existing object in game (that can take it) you can call the prototype from console off the object.

GameObject.prototype.functionName = function(someVarName){ someCode}; and then you can call it with console GameObject.functionName(someVarName) For a more specific example, you could prototype room, then call off a room.

Room.prototype.functionName = function(varName){ someCode }; and in console Game.rooms['roomNameHere'].functionName(varName)

You can learn more about prototyping in screeps here: https://docs.screeps.com/contributed/modifying-prototypes.html