[SOLVED] User function not finishing before next methods run

So I’m attempting to make custom functions for Wakka Maul Arena and I’ve noticed that whenever I run them, they kind of zip through and then to the next method. In this case, I’ve made a function for reducing the number of lines needed to move around, but when I use it, it goes through it and then instantly starts trying to attack something that’s not in range yet.

def multiMove(steps, movements = []):
    for x in range(steps):
        counter = 0
        if movements.index(counter) == u:
            hero.moveUp()
        elif movements.index(counter) == d:
            hero.moveDown()
        elif movements.index(counter) == l:
            hero.moveLeft()
        elif movements.index(counter) == r:
            hero.moveRight()
        else:
            break
        counter += 1

multiMove(2, [u, l])
hero.attack("enemy")

I’ve tried using the time library but it doesn’t appear to be implemented. I’ve looked online and the only thing I could find that might work would be a callback function, but I don’t really understand them and how to implement in python(codecombat).

Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time! :partying_face:

Hello! I’m not sure how to help you, so you’ll have to wait for some others. :confused:

1 Like

Thank you for the welcome nonetheless!

1 Like

Umm… the thing about this is that u doesn’t have a value. And you have to make those things strings. In most cases, u would be used as a variable. Right now, u and u really don’t have the same value. It;s a bit tricky to understand, but you have to use u as 'u'
Change the u, d, l, and r to ‘u’, ‘d’, ‘l’, and ‘r’. I’m not sure but ‘u’ == ‘u’ will return True and u == u, not so much. u doesn’t have a value and there is no way to compare these, so I think it’ll just skip all the statements.
If you define u = 3, r = 2, and set values for the variables, I think it would work since they have to have a value. If you could assign one, it’d work. A variable is considered equal if they compare values. At the start they have u = undefined. Also, u, d, l, and r are considered local, so say local and global are two dicts. local has all the variables in their dataset, called dicts in python. If you suddenly said u outside, it would be global_u , and global_u and local_u aren’t equal. Again, local will use outside variables if they’re defined so assign u, d, l, and r a name, and it will be fixed. Or say global u, global d, global l, and global r. It’s like there’s a local for every function, and the variables will be different there then the outside. It’s private, since it is only in the function.
Correct code to to this is:

def multiMove(steps, movements = []):
    for x in range(steps):
        counter = 0
        if movements.index(counter) == 'u':
            hero.moveUp()
        elif movements.index(counter) == 'd':
            hero.moveDown()
        elif movements.index(counter) == 'l':
            hero.moveLeft()
        elif movements.index(counter) == 'r':
            hero.moveRight()
        else:
            break
        counter += 1

multiMove(2, [u, 'l'])
hero.attack("enemy")

If that doesn’t work, try changing the movements = to movements, because it might say movements as .
Just saying, is the enemy’s name enemy?

1 Like

Sorry about the squares, they meant to be brackets.

1 Like

Do you want something like this:

def multiMove(steps, movements = []):
    for x in range(steps):
        for movement in movements:
            if movement == 'u':
                print('Up')
            elif movement == 'd':
                print('Down')
            elif movement == 'l':
                print('Left')
            elif movement == 'r':
                print('Right')
multiMove(2, ['u', 'r', 'd'])

# the output: 
Up
Right
Down
Up
Right
Down

replace print(‘Up’) with hero.moveUp() etc…

1 Like

As far as the values go, I have them declared at the top of my code, which I didn’t add. Sorry about that. I’ve also now tried changing the values to strings. It looks like it may also be getting stuck on break

def multiMove(steps, movements = []):
    for x in range(steps):
        counter = 0
        if movements.index(counter) == 'u':
            hero.moveUp()
        elif movements.index(counter) == 'd':
            hero.moveDown()
        elif movements.index(counter) == 'l':
            hero.moveLeft()
        elif movements.index(counter) == 'r':
            hero.moveRight()
        else:
            pass
        counter += 1

multiMove(3, ['u', 'l', 'l'])

And the enemy in my case is a door ‘d’ but I don’t believe that would change how the code acts with targeting and attacking an enemy.

Thank you! This worked great. My only question is where the movement is coming from since I’m not declaring it? Is that just how lists in python work or? The steps variable acts interestingly now; repeating the full list before looping.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.