Screeps: World
Hiyosup Oct 26, 2017 @ 3:52pm
Help understanding code
I am using tutorial/the guy-who-does-tutorial's script, potentially modified. I have never been able to tackle two sources at once and was curious what (sources[0]) is. Specifically what that 0 is doing for me.

run: function(creep) {
if(creep.carry.energy < creep.carryCapacity) {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});

10/27/2017:

I read that || is OR (except I read it does the first true value) and ^ is sort of like XOR (which I read Javascript doesn't truely have). My code below still functions the same as before, I need to find a more functional OR.

var roleHarvester = {

/** @param {Creep} creep **/
run: function(creep) {
if(creep.carry.energy < creep.carryCapacity) {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0||1]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0||1], {visualizePathStyle: {stroke: '#06873e'}});
}
}
else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&
structure.energy < structure.energyCapacity;
}
});
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#06873e'}});
}
}
}
}
};

module.exports = roleHarvester;
Last edited by Hiyosup; Oct 27, 2017 @ 4:59am
< >
Showing 1-1 of 1 comments
Ghoul Oct 26, 2017 @ 10:32pm 
[0] indicates the array index. Array indexes start at 0. So, the first index is 0, the second index is 1, so on.

Basically what it's saying is that "sources" is an array. An array is basically a single variable that stores multiple values. For example, a simple array in JavaScript would be:

var array = ['one', 'two', 'three', 'four', 'five'];

Each array index indicates a value in our array. For example:

console.log(array[0]);

The above command would write "one" to the console.

console.log(array[4]);

The above command would write "five" to the console.

sources[0] indicates the first source found when you run:
var sources = creep.room.find(FIND_SOURCES);

It finds all the sources in the room and puts them in an array. I'm not sure what implications it has if you use "sources" instead of "sources[0]", but my personal code uses "sources" and does not have an index mentioned. My creeps will use whatever source is available in the room. Indicating "sources[0]" means that whichever creep runs that code will only ever harvest from one source; the source indicated by "source[0]".

Example:
var sources = [source1, source2];
if you want your creep to only harvest from "source1" then you must say:
creep.harvest(sources[0]);

If you still don't understand, it's either because I'm pretty nooby to JavaScript, or because you don't know enough.

Either way, I highly recommend you check out this course (it can be taken for free and provides everything you need to understand Screeps at a basic level (including arrays)):
https://www.codecademy.com/learn/introduction-to-javascript
Last edited by Ghoul; Oct 26, 2017 @ 10:34pm
< >
Showing 1-1 of 1 comments
Per page: 1530 50