This is not an very well structured post but you can get the idea how I name my minions and I gather them in units. I never use structures as hero.attack(“Unit name”) despite this being so much promoted in many CodCombat levels. I use extensively the console for debugging and often to ‘name’ units.
This excerpt are from Gas Attack - codecombat [ SOLVED ] - you can see how the code works the video from the topic.
You don’t need books to use the functions, they are available in all CoCo levels.
var friends = hero.findFriends();
////////////////////// custom function //////////////////////////
function selectById(units, id){
for (var i = -1, len = units.length; ++i < len;)
if (units[i] == id) return units[i];
}
var regan1 = selectById(friends, 'Regan');
console.log('regan1: ' + regan1.id); // replace with actual code
////////////////////// find - will work in CoCo python - don't know if in 'pure' python
function isRegan (unit){
return unit.id == 'Regan' ;
}
var regan3 = friends.find(isRegan);
console.log('regan3: ' + regan3.id); // replace with actual code
////////////////////// filter will work in CoCo python - don't know if in 'pure' python
function isTypeAndWhere(unit){
return unit.pos.x > 25 && unit.type == 'peasant';
}
var peasants = friends.filter(isTypeAndWhere);
for (var i = -1, len = peasants.length; ++i < len;)
console.log('peasant[' + i + '] is ' + peasants[i].id); // replace with actual code
//////////////////////////////////////////////////////////
for (var i = -1, len = friends.length; ++i < len;) console.log(i + ':' + friends[i].id);
// browser console output - I grab the names from there
// 0:Thad
// 1:Duan
// 2:Brom
// 3:Matilda
// 4:Regan
// 5:Artillery
// creating an array of units
var thad = friends[0], Thad = 0,
duan = friends[1], Duan = 1,
regan = friends[4], Regan = 2,
saps = [thad, duan, regan];
// creating an object of units
var sappers = {
thad : friends[0],
duan : friends[1],
regan : friends[4]
};
///// commanding an object of units
for (var i in sappers)
console.log('sappers.'+ i + ' is '+ sappers[i]); // put actual code here
/// commanding an array of units
for (var j = -1, len = sappers.length; ++j < len;)
console.log(j + ':' + sappers[j].id); // put actual code here
// commanding single unit
hero.command(sappers.duan, 'buildXY', 'fire-trap', x , y); // unit from object
hero.command(thad, 'buildXY', 'fire-trap', x, y); // individual unit
hero.command(saps[Regan], 'buildXY', 'fire-trap', x, y); // unit from array with index=2