Siege of Stonehold Help

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

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

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.

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)

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

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

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

@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.

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

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?

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.

Ok thanks I think I get it. :smile:

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:

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?

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

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).

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.

Thanks for the clarification! :smile: :smile: