Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

How do I name my units?

I want to name members of an army, but I do not know how to. any advice?

I don’t think you can? Also, what exactly do you wish to achieve by naming members of your army?

1 Like

to specify which ones perform actions

To that point, all of the characters do have names that you can identify. When you click on a specific unit it will give the name and the type at the bottom.

When you assign the object to a variable like enemy = hero.findNearestEnemy() you can check
names by enemy.id or the type of enemy by enemy.type. You can do this with friends too. Then use an if statement to identify them and give the code specific to that enemy or enemy type.

if enemy.id == "Upfish":  # need to be in quotes when names or types
if enemy.type == "munchkin": # or use != for not equal
    hero.attack(enemy) 

type

thanks, i will remember this.

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