我的脚本部分必须获取源ID并将其存储到内存中,但仍然无法正常工作,请帮帮我。
for(var name in Game.spawns)
{
var source1 = Game.spawns[name].room.find(FIND_SOURCES)
for(var i in source1)
{
Memory[source1[i].id] ={};
Memory[source1[i].id].workers = 0;
}
}
答案 0 :(得分:6)
对于那些仍在寻找答案的人。
您可以轻松地将新内存部件添加到任何对象。
大多数项目应该是特定于房间的,因此在大多数情况下,您应该使用房间内存来添加对象。对于此示例,我们为房间中的每个源添加内存:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/rentalProperties">
<html>
<body>
<table border="1">
<tr>
<th>Availability</th>
<th>Contact</th>
<th>Type</th>
<th>Price</th>
<th>Address</th>
<th>Bedrooms</th>
<th>Bathrooms</th>
<th>Garage</th>
<th>Description</th>
</tr>
<xsl:for-each select="property">
<tr>
<xsl:apply-templates select="@* | *"/>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="@available | @contact | type | price | numberOfBedrooms | numberOfBathrooms | garage | description">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
<xsl:template match="address">
<td>
<xsl:value-of select="concat(streetNo/text(), ',', street/text())"/>
</td>
</xsl:template>
</xsl:stylesheet>
&#13;
在此代码之后,所有来源都有内存。
让我们用收割机尝试:( //Lets first add a shortcut prototype to the sources memory:
Source.prototype.memory = undefined;
for(var roomName in Game.rooms){//Loop through all rooms your creeps/structures are in
var room = Game.rooms[roomName];
if(!room.memory.sources){//If this room has no sources memory yet
room.memory.sources = {}; //Add it
var sources = room.find(FIND_SOURCES);//Find all sources in the current room
for(var i in sources){
var source = sources[i];
source.memory = room.memory.sources[source.id] = {}; //Create a new empty memory object for this source
//Now you can do anything you want to do with this source
//for example you could add a worker counter:
source.memory.workers = 0;
}
}else{ //The memory already exists so lets add a shortcut to the sources its memory
var sources = room.find(FIND_SOURCES);//Find all sources in the current room
for(var i in sources){
var source = sources[i];
source.memory = this.memory.sources[source.id]; //Set the shortcut
}
}
}
是模块的变量)
creep
&#13;
答案 1 :(得分:0)
有一个类似的问题,答案很好; https://stackoverflow.com/a/30150587/5857473
我希望它是房间的属性,所以我改变了这样的代码:
Object.defineProperty(Source.prototype, 'memory', {
get: function() {
if(_.isUndefined(this.room.memory.sources)) {
this.room.memory.sources = {};
}
if(!_.isObject(this.room.memory.sources)) {
return undefined;
}
return this.room.memory.sources[this.id] = this.room.memory.sources[this.id] || {};
},
set: function(value) {
if(_.isUndefined(this.room.memory.sources)) {
Memory.sources = {};
}
if(!_.isObject(this.room.memory.sources)) {
throw new Error('Could not set source memory');
}
this.room.memory.sources[this.id] = value;
}
});
这样可以避免循环遍历所有房间以设置快捷方式等。
答案 2 :(得分:0)
我使用以下代码保存当前房间中的所有资源以及一个允许我稍后将矿工蠕动分配给源的属性。
if(!spawns.memory.roomSources){
spawns.memory.roomSources=[];
var energySources = spawns.room.find(FIND_SOURCES);
for(var i in energySources){
spawns.memory.roomSources[i] = {sourceId: energySources[i].id, currentMinerId: null};
}
}
这是循环内存并查看每个源的代码。
for(var x in spawns.memory.roomSources){
}