SOLVED: Problems with commands item.pos and enemy.type


#1

I have a free subscription. My equipment is as follows:

Programmaticon III
Leather belt
Simple wristwatch
Darksteel blade
Leather boots
Enchanted lenses
Quartz sense stone
Steel ring
Basic flags
Boss star II
Obsidian helmet
Painted steel breastplate
Steel shield

In several different levels, commands that include item.pos or enemy.type refuse to execute. Instead, they pop up an error message that says, “tmp[number] is undefined”. In each case, I made sure that I had defined “item” or “enemy”. The help is blank. Can you tell me whether the problem is my code or CodeCombat?


#2

Give us an example. A screenshot with your code in it, the levels that it happens, and the error that follows, for example. Does this problem show up on some levels, but not others? Help us help you!


#3

Certainly.

Sarven Brawl:

    loop:
    enemies = self.findEnemies()
    enemy = enemies[enemyIndex]
    enemyIndex = 0
    while enemyIndex <= len(enemies):
        if self.isReady("power-up"):
            self.powerUp()
        if enemy.type != 'sand-yak':
            while enemy.health > 0:
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        enemyIndex = enemyIndex + 1

I get the error message, “Line 8: tmp48 is undefined”, with “enemy.type” in “if enemy.type != ‘sand-yak’:” highlighted.

Shine Getter:

    loop:
    coinIndex = 0
    coins = self.findItems()
    loop:
        coin = coins[coinIndex]
        if coin.value == 3:
            x = coin.pos.x
            y = coin.pos.y
            self.moveXY(x, y)
        coinIndex = coinIndex + 1        

The error is, “Line 6: tmp29 is undefined”, with " coin.value" in “if coin.value == 3:” highlighted.

Wild Horses:

loop:
    # How do you find the nearest friendly unit?
    # horse = ?
    horses = self.findFriends()
    horse = horses[horseys]
    horseys = 0
    while horseys < len(horses):
        x1 = horse.pos.x - 7
        x2 = horse.pos.x + 7
        if x1 >= 1:
            # Move to the horse's y position but use x1 for the x position.
            hy = horse.pos.y
            self.moveXY(x1, hy)
        elif x2 <= 79:
            # Move to the horse's y position but use x2 for the x position.
            hy = horse.pos.y
            self.moveXY(x2, hy)
        distance = self.distanceTo(horse)
        if distance <= 10:
            self.say("Whoa")
            # Move to the red x to return the horse to the farm.
            self.moveXY(28, 53)
        horseys = horseys + 1
            # Move back out into the pasture to begin looking for the next horse.

The error message is, “Line 8: tmp42 is undefined”. “Horse.pos” in “x1 = horse.pos.x - 7” is highlighted.


#4

In the future, please put triple backticks, or three of `, around you code, so it will format properly.

In Sarven Brawl, you use enemyIndex before you define it, which would explain the error.

With the weird indentation, it’s hard to tell on Shine Getter. However, why do you have two loops? You will never leave the first one without a break statement…

Similar thing to Sarven Brawl on Wild Horses. You use horseys before you define it.


#5

Thanks much. I’ll move the indexes tonight.

Now that I look at it again, I’m not sure what’s up with my Shine Getter code. It looks as though I stopped in the middle of editing it. :confused:


#6

in the case of the shine getter code it looks like Lyndon is writing a loop within a loop:

loop:
    set coinIndex and coins
    loop:
        collect coins

So it is his second loop that needs a break or needs to be a “while loop”


#7

I have yet to try the others, but I have solved Wild Horses. Thanks much, ChronistGilver!

Vlevo, I’ll make sure to do that. I know my Shine Getter code needs major rewriting.


#8

I fixed my Shine Getter code as well. I’m still having problems on Sarven Brawl. Here’s my code:

loop:
    enemies = self.findEnemies()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if self.isReady("power-up"):
            self.powerUp()
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                if self.canElectrocute(enemy):
                    self.electrocute(enemy)
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        enemyIndex = enemyIndex + 1

Tharin kills about two enemies, then just sits there with no target while enemies pound on him.


#9

Is a Sand Yak the nearest enemy? That could explain such behavior.


#10

No, it is not. If it were, though, wouldn’t increasing the enemyIndex whether the enemy is attacked or not avoid problems with sand yaks?


#11

Hm. Yes, it would indeed. Your problem is the canElectrocute. That tests whether the enemy is not inanimate, i.e a Beam Tower or a Palisade, not whether electrocute is ready. Add an isReady("electrocute").


#12

Thank you. I fixed that, but Tharin still has no target.


#13

Hm…maybe an if enemy: statement after you define enemy? If you don’t have glasses that can see through walls, that might do it.


#14

“If enemy” didn’t help. (Might that be saying the same thing as “while enemyIndex < len(enemies)”?)

I only have 1099 gems, whereas glasses that see through walls require 1500. Besides, the enemy Tharin refuses to attack is not on the other side of a wall.


#15

I found it! You did not define enemyIndex. Therefore, you can’t add one to it, for it doesn’t exist.


#16

Actually, I did. Sorry, but I neglected to leave a line break after the first three backticks. For clarity, here is my current code:

enemyIndex = 0
loop:
    enemies = self.findEnemies()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if self.isReady("power-up"):
            self.powerUp()
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                if self.isReady("electrocute"):
                    self.electrocute(enemy)
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        enemyIndex = enemyIndex + 1

#17

There is a flag that appears after killing the first ogre (presumably because of my placing it there last time), but I don’t think that’s affecting Tharin since I have no code concerning flags.


#18

Huh. That’s weird. I just ran your code, and it worked perfectly fine for me.


#19

I have already beaten the level once. I’ll try submitting the code and watch what happens.


#20

Nope. Tharin died. :confused: He killed an ogre and a scout, waited a few seconds while another scout attacked him, killed that scout, then stood there until decked by two more scouts and a thrower, all within easy fighting range.