Siege of Stonehold Help


#1

Hey everyone I’m having some problems with Siege of Stonehold. The first time I wrote my code it said “missing : after if self.isReady(“cleave”)”.

I fixed the error and it still said the same thing. Does anyone know what is wrong?
Here is my code:

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        distance = self.distanceTo(enemy)
        if distance < 10:
            self.attack(enemy)
            
    flag = self.findFlag("black")
    if flag:
        if flag.color is "black":
            self.pickUpFlag(flag)
            
    distance = self.distanceTo(enemy) :
    if distance < 5:
        if self.isReady("cleave") :
            self.cleave(enemy)
            self.attack(enemy)

I would really appreciate the help.

Thanks! - @Luke10


#2

Remove the : after the assignment in this line: distance = self.distanceTo(enemy) :


#3

I tried that and now it is saying “Line 18: distanceTo target is null. Does it exist (use if)”. I don’t know what to do about it.


#4

Well, let’s examine the message:

distanceTo target is null.

What is the target (argument) that you are passing to distanceTo? It is the enemy variable.

Does it exist (use if)

This means you should check if the target exists using an if block—that is, if enemy:

It should be like how you did in the distance < 10 part (inside an if enemy: block)


#5

You have 2 self.attack(enemy) blocks of code. Combine them together inside the

if enemy:

block.

You cannot compute the distance to an enemy if the enemy does not exists. So

distance = self.distanceTo(enemy)

must be always be inside an if enemy: block


#6

@AdrianCgw and @UltCombo, sorry for the delay, but haven’t I already put in the if statement?


#7

Indeed you have, but you use distanceTo outside of it.


#8

@Luke10 You did put the first distanceTo inside an if statement, but not the second distanceTo in your code.

It should be better to combine both of those inside a single if statement, as @AdrianCgw suggested.


#9

Ok thanks guys for your help. I’ll try to fix that.


#10

Ok I hope bringing this topic back up isn’t offensive or a violation to the FAQ, but I don’t understand what you mean by I didn’t put an if statement after the second one. What do you mean by that?


#11

There are technically no rules set on stone that disallow bumping old threads. It is just a good forum practice to not bump “dead” or inactive threads, but as long as you still need help or have something meaningful to add, it is not a dead thread at all. Well, I guess the “rule” would be to just use common sense. :smile:

Now, back to the topic.

Every time you work with the enemy variable, you should make sure its value is not empty (None/null). Around the 13th line in your first post’s code, you are trying to get the distance to the enemy using self.distanceTo(enemy), but you didn’t check if the enemy variable is not empty—it should be guarded by an if enemy: like you did earlier in your code.

Though, it is probably better to put together all the code that deals with the enemy inside a single if enemy:, as Adrian has suggested above.


#12

Ok thanks I think I get it. :smile:


#13

Ok sort of got the second if statement, but now on line 19 it is not accepting what I put in.
Here’s the code:

loop:
enemy = self.findNearestEnemy()
if enemy:
distance = self.distanceTo(enemy)
if distance < 10:
self.attack(enemy)
            
flag = self.findFlag("black")
if flag:
if flag.color is "black":
self.pickUpFlag(flag)
            
if distance = self.distanceTo(enemy)(
if self.distance < 10:
if self.isReady("cleave"):
self.cleave(enemy)
self.attack(enemy)

hope you can find the problem! :smile:


#14

The code you have posted is not indented (missing spaces in the beginning of the lines), can you please try to fix it or copy it from inside the game again?


#15

I think he did indent @UltCombo otherwise he would have another error instead of that one.


#16

Also, I believe that on line 12 you have an extra if when you define distance and you have an extra parenthese after self.distanceTo(enemy).


#17

Note my specific wording, with emphasis added:

I’m referring to the code shown in the post above which is clearly missing indentation, not his in-game code.


#18

Thanks for the clarification! :smile: :smile: