Difference between revisions of "Making your own console functions"

From Screeps Wiki
Jump to navigation Jump to search
m (Added why prototypes work, and not to use Game.)
Line 7: Line 7:
 
<code>global.someFunctionName(someVarName) = { someCode };</code> then simply call it in console with <code>someFunctionName(someVarName)</code>.
 
<code>global.someFunctionName(someVarName) = { someCode };</code> then simply call it in console with <code>someFunctionName(someVarName)</code>.
   
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.
+
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 persistent.
   
 
You can learn more about global here: https://docs.screeps.com/contributed/caching-overview.html#Global
 
You can learn more about global here: https://docs.screeps.com/contributed/caching-overview.html#Global
Line 13: Line 13:
 
== Prototype ==
 
== 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.
+
By setting a prototype of an existing object in game (that can take it) you can call the prototype from console off the object. This works because the game objects ''are'' global objects (<code>global.Creep === Creep</code>). Note that <code>Game</code> does not have a prototype, and is recreated each tick.
   
 
<code>GameObject.prototype.functionName = function(someVarName){ someCode};</code> and then you can call it with console <code>GameObject.functionName(someVarName)</code> For a more specific example, you could prototype room, then call off a room.
 
<code>GameObject.prototype.functionName = function(someVarName){ someCode};</code> and then you can call it with console <code>GameObject.functionName(someVarName)</code> For a more specific example, you could prototype room, then call off a room.

Revision as of 06:41, 11 June 2021

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 persistent.

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. This works because the game objects are global objects (global.Creep === Creep). Note that Game does not have a prototype, and is recreated each tick.

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