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

[Solved] How do you figure out object from string(

I’m feeling sorry because I look like spamming posts on this forum, but I’m currently trying my best.

I got stuck at Desert Delta mission.

# Only attack the enemies in the enemyNames array.
# Be sure to attack in order! 0 -> 1 -> 2 -> 3
enemyNames = ["Kog", "Godel", "Vorobun", "Rexxar"]

# Attack enemyNames[2]:
# Attack the last element:

I know this map is for teaching the basic of basic arrange stuff. Choosing melee heroes, you’ll write only this much of code lines above, and you are done. But, when it comes to range heroes, the system says you should get closer to the targets before killing them.

And here I thought, just writing something like “hero.move(enemyNames[3])” would work. But, unlike attack() method, move methods didn’t accept strings (enemyNames[3] = “Rexxar”) at all. I tried as far as I could for over an hour but didn’t manage it.

How can you specify an object from its string name?
If I had to imagine… something like…

realTarget = enemy.whoseNameIs(enemyNames[3])

Data Types:
a number
a String
those are not monsters

enemyNames = [“Kog”, “Godel”, “Vorobun”, “Rexxar”]
This is just some piece of text.

just like saying
a = Kog
b = Godel
c = Vorobun

What is a monster? What is a Hero or a friend.
All the same thing, an object with different properties.

what happen when you do: friends = hero.findFriends()?
a friend is an object, but friends here is an Array of objects
and object have properties.

What is name? some Text? or the property of an Object? both perhaps?

Thank you. I sure know the ‘enemyNames’ there is nothing but a gathering of strings. But it’s also fact that some of orges have the exact same names as the ones in the ‘enemyNames’.

But, please look at my code and the #s here.

enemyNames = ["Kog", "Godel", "Vorobun", "Rexxar"]
#I want to kill at least one of them, Kog, here.

enemies = hero.findEnemies()
#Just to be sure if the first enemy is "Kog" or not.
#And the hero said "Kog". Confirmed.

if enemies[0] == "Kog":
#Nothing happens... why.. just why??
#Actually I know the answer. 
#It didn't work because it's like 'enemies[0] == "nice to meet you" '

#But how come this works perfectly fine??

I simply want to figure out how to find a specific object from a certain string, Kog in this case, like the ‘hero.attack’ command did in this. If the attack command consisted of two phases, (which I don’t know at all actually it’s just a metaphor,) the first step must be the pocess of matching the string ‘Kog’ with the actual object, the ogre named Kog. And I want that process. If I once find it, I could have my range hero get close to the enemies before killing them as intended.

Are you familiar with the property? It’s quite simple, it’s just the name of an object. e.g. == "Kog".
Using this, you can create quite a nifty function to complete this level:

def findMatching(enemy_id, enemies):
    chosen_enemy = None
    for enemy in enemies:
        if == enemy_id:
            chosen_enemy = enemy
    return chosen_enemy

Then using for i in range(0, 4) you can move to each of the ogres’ position and shoot it.
I’m afraid this slightly longwinded function is the best that you can do in codecombat. (as far as i’m aware)


I don’t know how should I thank you!! Now I can sleep in peace :smiley: