[SOLVED] "TypeError: Tried to load an unsafe native value into the interpreter:object"

I’m in the level “Sarven Brawl” in the Sarven Desert.

This is part of my code… :

while True:
    enemiesInSight = hero.findEnemies()
    for n in enemiesInSight:
        if isPartOf(enemiesInSight[n], doNotAttack) == True:
            break
        
        nDistance = hero.distanceTo(enemiesInSight[n].pos)
        if n < 1 :
            nearestEnemy = enemiesInSight[n]
        else:
            if nDistance < hero.distanceTo(nearestEnemy):
                nearestEnemy = enemiesInSight[n]
    enemy = nearestEnemy

…but the game returns me:

And i don’t understand why. Can someone help me ? :confounded:

EDIT: more info
It seems what the rubber duck says at the end of the error message (in the case of my screenshot, Vyle, but i’ve also seen Polyfemo once) is the name of… an enemy… I think :thinking:
So, does that mean the method hero.findEnemies() which should return a whole array of enemies only returns one, which turns into the wrong type of variable… which is a problem ?
What do you think ?

EDIT: even more info
The game gave me almost the same error message for a line of code i didn’t even write. The level is “Fair Battle” in Sarven Desert.
https://discourse.codecombat.com/uploads/short-url/lLeY7ORux9uTTrOd3OSmQ4Ww8R5.png

1 Like

Please give more information about what is happening, what level you are on, which world you are in? Any additional information would be extremely helpful.

1 Like

I’m on the Sarven Brawl level, in Sarven Desert.
The part that was highlighted (in yellow or green, i can’t say) was the line that goes enemiesInSight = hero.findEnemies().
Also, this is only part of my code for this level, i may add more if anyone thinks it might be interesting.
And i don’t know what more to say, because i don’t know what might be the source of the problem :confused:

1 Like

Try enemy = hero.findNearestEnemy() instead of enemiesInSight = hero.findEnemies()

1 Like

It will not work, because the point of the for loop after enemiesInSight = hero.findEnemies() is to sort through the newly stored array and find the nearest enemy that is not part of my custom array named doNotAttack.
Your solution will only find the nearest enemy, regardless if it’s part of the doNotAttack array

1 Like


Did you mean to put that n there?

1 Like

i can give you the isPartOf() definition but i’m not sure it will help, as it seems this method works…

def isPartOf(entity, array):
    index = 0
    while index < len(array):
        if entity.type == array[index]:
            return True
        index += 1
    return False
2 Likes

Yes i did :nerd_face:

2 Likes

So you have solved the problem?

1 Like

I am sorry if I haven’t been much help. But your issue and the error message is confusing. Especially the error

1 Like

No, I haven’t solved the problem, the method isPartOf() works, but what seems to not work is this specific line of code :
enemiesInSight = hero.findEnemies()

2 Likes

Could you send a link of the level that you are on so I can see your code in the game and run it? I think I know what the issue is

1 Like

The level is https://codecombat.com/play/level/sarven-brawl?
and i’ll send you the whole code i’m using as a PM.

1 Like

Can anybody else try to help me with this code ?

:face_with_monocle: It seems what the rubber duck says at the end of the error message (in the case of my screenshot, Vyle, but i’ve also seen Polyfemo once) is the name of… an enemy… I think :thinking:
So, does that mean the method hero.findEnemies() which should return a whole array of enemies only returns one, which turns into the wrong type of variable… which is a problem ?
What do you think ?

1 Like

Ok now the game threw me an error for a line of code i didn’t even write and which was in this exercise even before i started typing. Is it a bug now ? :weary:
The name of the exercise in english is “Fair Battle” in the Sarven Desert.

1 Like

You have the piece of code:

for n in units:
    totalHealth += units[n].health

##############################

for enemy in enemies: 
    hero.attack(enemy)  # this is right

##############################

for enemy in enemies:
    hero.attack(enemies[enemy])  # and this is wrong

In your code n isn’t an index, but an object ( enemy or friend ) so the message is appropriate. Same error is displayed in Mad Maxer strikes back topic. where an object is put into the while loop.

4 Likes

Ah, thank you so much :smiley: i couldn’t get my head around the problem , so much i thought it was a bug

I often forget the sole name of the object isn’t enough to get its properties, or that that the code needs a particular input. For example i’ve often times written hero.moveXY(coin) or even hero.moveXY(coin.pos).
Is it possible to simply get the index of any entry in an array ? something that would read like units[n].index ?

2 Likes

Please DO NOT give the code that worked for you!!! This goes against the CodeCombat forum
https://discourse.codecombat.com/faq

1 Like