I have this object
Memory.creepsConf = {
//The role by which we will refer to the creep
roles: {
harvester: "Harvester",//Harvests energy and gives it to the spawn
upgrader: "Upgrader",//Harvests energy and gives it to the Controller
builder: "Builder",// Harvests energy and builds stuff
healer: "Healer"// Harvests energy and heals
},
//the maximum number of creeps. Used by ControllerCreeps
maximum: {
harvester: 100,
upgrader: 100,
builder: 100,
healer: 100
},
//The bare minimum needed. Used by ControllerCreeps
minimum: {
harvester: 20,
upgrader: 10,
builder: 5,
healer: 2,
},
//Since not all creeps roles will be filled the Colony needs to know
//which creeps are a priority.
priority: {
harvester: 10,
upgrader: 20,
builder: 8,
healer: 7
},
build: {
harvester: [CARRY,WORK,MOVE],
upgrader: [CARRY,WORK,MOVE],
builder: [CARRY,WORK,MOVE],
healer: [MOVE,HEAL,MOVE]
}
}
As you can notice in roles
I define the roles and in other parts I refer to each creep by role.
Another thing you can notice is that I am always using the key defined in roles
and never the value. This is a problem, because if somebody provides me with "Harvester" i need to get the key out of roles
and then use the key...making the value obsolete.
What I want to do is instead of saying harvester
which is a key in roles
I want to call the value of that key as a key in other objects
Something like this
Memory.creepsConf = {
//The role by which we will refer to the creep
roles: {
harvester: "Harvester",//Harvests energy and gives it to the spawn
upgrader: "Upgrader",//Harvests energy and gives it to the Controller
builder: "Builder",// Harvests energy and builds stuff
healer: "Healer"// Harvests energy and heals
},
//the maximum number of creeps. Used by ControllerCreeps
maximum: {
Memory.creepsConf.roles.harvester: 100,
Memory.creepsConf.roles.upgrader: 100,
Memory.creepsConf.roles.builder: 100,
Memory.creepsConf.roles.healer: 100
},
//The bare minimum needed. Used by ControllerCreeps
minimum: {
Memory.creepsConf.roles.harvester: 20,
Memory.creepsConf.roles.upgrader: 10,
Memory.creepsConf.roles.builder: 5,
Memory.creepsConf.roles.healer: 2,
},
//Since not all creeps roles will be filled the Colony needs to know
//which creeps are a priority.
priority: {
Memory.creepsConf.roles.harvester: 10,
Memory.creepsConf.roles.upgrader: 20,
Memory.creepsConf.roles.builder: 8,
Memory.creepsConf.roles.healer: 7
},
build: {
Memory.creepsConf.roles.harvester: [CARRY,WORK,MOVE],
Memory.creepsConf.roles.upgrader: [CARRY,WORK,MOVE],
Memory.creepsConf.roles.builder: [CARRY,WORK,MOVE],
Memory.creepsConf.roles.healer: [MOVE,HEAL,MOVE]
}
}
What I want to end up with is the value of Memory.creepsConf.roles.*
as a key represented in other objects so that if somebody provides me with the value Harvester
I can actually use it as a key to get all needed information.
However this second piece of code doesn't work. I get
Unexpected token .
Is there a way to use the value of Memory.creepsConf.roles.*
as a key in Memory.creepsConf.maximum
, Memory.creepsConf.minimum
, Memory.creepsConf.priority
and Memory.creepsConf.build
?
In case this example is too big and hard to follow I will try to simplify it
var obj = {
foo:"Foooo",
obj.foo: "Wohooo"
}
This object now should have a key which is Foooo
and obj['Foooo']
should return "Wohooo"
Why not use the roles as keys of the main configuration object:
Memory.config.creeps = {
"Harvester": {
maximum: 100,
minimum: 20,
priority: 10,
build: [CARRY, WORK, MOVE]
}, {
"Upgrader": {
maximum: 100,
minimum: 10,
priority: 20,
build: [CARRY, WORK, MOVE]
}
///...
};
Now you can just access the properties like this:
function getMinimumForRole(role){
if (role in Memory.config) {
return Memory.config[role].minimum;
}
throw "role " + role + " not found";
}
If you still want to go on the way you were working, then you could do it with two assignments using some ES6 syntax features:
const roles = { // temporary variable for keeping the rest short
harvester: "Harvester",
upgrader: "Upgrader",
builder: "Builder",
healer: "Healer"
};
Memory.creepsConf = {
roles, // ES6 short notation
maximum: {
[roles.harvester]: 100, // ES6 computed property syntax
[roles.upgrader]: 100,
[roles.builder]: 100,
[roles.healer]: 100
},
// ...etc
};