在遊戲中,我嘗試從另一個文件中調用一個變量。從我的role.builder文件Screeps從另一個文件訪問'主'變量?
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var roleVoyager = require('role.voyager');
var roleAttacker = require('role.attacker');
var roleTransporter = require('role.transporter');
var roleHealer = require('role.healer');
var roleRepairer = require('role.repairer');
module.exports.loop = function() {
for(var name in Memory.creeps) {
if(!Game.creeps[name]) {
delete Memory.creeps[name];
console.log('Clearing non-existing creep memory:', name);
}
}
var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
console.log('Harvesters: ' + harvesters.length);
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
console.log('Upgraders: ' + upgraders.length);
var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
console.log('Builders: ' + builders.length);
var voyagers = _.filter(Game.creeps, (creep) => creep.memory.role == 'voyager');
console.log('Voyagers: ' + voyagers.length);
var attackers = _.filter(Game.creeps, (creep) => creep.memory.role == 'attacker');
console.log('Attackers: ' + attackers.length);
var transporters = _.filter(Game.creeps, (creep) => creep.memory.role == 'transporter');
console.log('Transporters: ' + transporters.length);
var healers = _.filter(Game.creeps, (creep) => creep.memory.role == 'healer');
console.log('Healers: ' + healers.length);
var repairers = _.filter(Game.creeps, (creep) => creep.memory.role == 'repairer');
console.log('Repairers: ' + repairers.length);
if(harvesters.length < 3) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE], creepName = Creep.getRandomName("[Harvester]"), {role: 'harvester'});
console.log('Spawning new harvester: ' + newName);
}
if(upgraders.length < 3 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,CARRY,CARRY,MOVE,MOVE], creepName = Creep.getRandomName("[Upgrader]"), {role: 'upgrader'});
console.log('Spawning new upgrader: ' + newName);
}
if(builders.length < 2 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,CARRY,MOVE,MOVE], creepName = Creep.getRandomName("[Builder]"), {role: 'builder'});
console.log('Spawning new builder: ' + newName);
}
if(voyagers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([MOVE], creepName = Creep.getRandomName("[Voyager]"), {role: 'voyager'});
console.log('Spawning new voyager: ' + newName);
}
if(attackers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([ATTACK,ATTACK,MOVE], creepName = Creep.getRandomName("[Attacker]"), {role: 'attacker'});
console.log('Spawning new Attacker: ' + newName);
}
if(transporters.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,MOVE], creepName = Creep.getRandomName("[Transporter]"), {role: 'transporter'});
console.log('Spawning new Transporter: ' + newName);
}
if(healers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([HEAL,MOVE,MOVE], creepName = Creep.getRandomName("[Healer]"), {role: 'healer'});
console.log('Spawning new Healer: ' + newName);
}
if(repairers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([CARRY,CARRY,WORK,MOVE,MOVE], creepName = Creep.getRandomName("[Repairer]"), {role: 'repairer'});
console.log('Spawning new Repairer: ' + newName);
}
for(var name in Game.creeps) {
var creep = Game.creeps[name];
if(creep.memory.role == 'harvester') { roleHarvester.run(creep); }
if(creep.memory.role == 'upgrader') { roleUpgrader.run(creep); }
if(creep.memory.role == 'builder') { roleBuilder.run(creep); }
if(creep.memory.role == 'voyager') { roleVoyager.run(creep); }
if(creep.memory.role == 'attacker') { roleAttacker.run(creep); }
if(creep.memory.role == 'transporter') { roleTransporter.run(creep); }
if(creep.memory.role == 'healer') { roleHealer.run(creep); }
if(creep.memory.role == 'repairer') { roleRepairer.run(creep); }
}
}
現在我嘗試寫出具有檢查從主變量的if-else語句: 這就是我的主文件
if (main.harvesters.length > 2){...}
,但我得到的錯誤'主要操作系統沒有定義' 我該如何調用主要文件?