Difference between revisions of "Making your own console functions"

From Screeps Wiki
Jump to navigation Jump to search
(categorized)
m (added links, fixed some typos)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Being able to call functions/commands from the console can be useful, there are a few ways to go about it.
+
Being able to call functions/commands from the console can be useful. There are a few ways to go about it.
   
 
== Global ==
 
== 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.
+
By assigning a function to [[Heap#Global|global]], it is possible to access the function from everywhere in your script including [[Interface#Console|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.
   
 
<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 [[Tick|ticks]], however it will go away on a [[Global reset|global reset]], which normally happens when uploading new code or during server side resets / issues. It's 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 check out the global's [https://docs.screeps.com/contributed/caching-overview.html#Global official docs] entry to learn more.
   
 
== 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 in the console like <code>GameObject.functionName(someVarName)</code>. For a more specific example you could prototype room, then call the method off any room object.
   
 
<code>Room.prototype.functionName = function(varName){ someCode };</code> and in console <code>Game.rooms['roomNameHere'].functionName(varName)</code>
 
<code>Room.prototype.functionName = function(varName){ someCode };</code> and in console <code>Game.rooms['roomNameHere'].functionName(varName)</code>
   
You can learn more about prototyping in screeps here: https://docs.screeps.com/contributed/modifying-prototypes.html
+
You can learn more about prototyping in screeps from their [https://docs.screeps.com/contributed/modifying-prototypes.html docs' prototype entry].
 
[[Category:Development]]
 
[[Category:Development]]

Latest revision as of 23:11, 5 December 2021

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

Global[edit | edit source]

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. It's up to you to keep it refreshed if you want it persistent.

You can check out the global's official docs entry to learn more.

Prototype[edit | edit source]

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 in the console like GameObject.functionName(someVarName). For a more specific example you could prototype room, then call the method off any room object.

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

You can learn more about prototyping in screeps from their docs' prototype entry.