5

Is there any way to call a module or function from the console? For example a module that creates worker creeps, called from the console with my_module.add_creep?

Ben
  • 53
  • 1
  • 3

5 Answers5

5

For the given example, use require('my_module').add_creep(); The require function returns whatever is assigned to module.exports in that module.

@cho: require doesn't have to return a function. It rather returns a function because the module contains a function. If the module assigns as follow module.export = {test: 'test'}, follow snippet require('that_module') will return {test: 'test'} etc...

Note: Not sure if others have this issue, but sometimes the console isn't returning a result at all... In that case, try reloading the page. Just make sure the game isn't pauzed while executing commands...

avdg
  • 328
  • 1
  • 12
4

I just figured this out today. But there is a nice way to access your methods from the console

Just import your methods to the Game object (in your script)

Game.creepManager = require('CreepManager');

you can now use the console by typing

Game.creepManager.yourFunctionHere();
Igand
  • 1,161
  • 1
  • 15
  • 25
Raggy
  • 76
  • 3
  • 3
    @Andrew you should assign the thing you want accessible from Game in the game loop (as Game appears to get overwritten every tick with a new state, so you have to re-add your property) – Skintkingle Jun 21 '16 at 20:10
2

Others have mentioned the use of require and assigning to Game, but you can also create new top level commands and objects by assigning to global.

Main loop:

global.rebuildMemory = () => console.log('Memory rebuilt.');
global.Commands = require('consoleCommands');

Then from the console:

rebuildMemory();
Commands.printRoomReport('W3N4');
Daniel
  • 582
  • 8
  • 15
0

I've managed to call the lodash module from the console by calling it like this require('lodash').pluck(characters, 'name'). Not sure if it works with user created modules.

Lucas Azevedo
  • 1,097
  • 15
  • 36
0

The require function returns a function so you can call it direclty in the console like this:

require('harvester')(Game.creeps.harvester1);
cho
  • 121
  • 1
  • 5