Understanding Runtime Errors

Understanding Runtime Errors

> LeekScript Tutorial

You're coding your AI, happy as a clam. The lines scroll by, and the names of... No wait, we've already seen this one. This time, no more red messages in the editor: the compiler holds no more secrets for you. It's time for the duel! You watch your leeks in their natural habitat, like a Steve Irwin of the Garden. They wander around, they frolic, they machine-gun each other. How happy they look!

Little do they know that fearsome enemies lurk in the shadows...

Too many operations performed for this turn

Aaaaah, a classic!! This one isn't necessarily a mistake in your AI, but rather going over the maximum number of operations (which depends on your Cores: 1 million operations per core, available in your code through getMaxOperations). Check out the Operations tutorials for a detailed article.

My AI is tiny! How is that possible?

In that case, you can bet on a loop with no stopping condition, what's called an infinite loop.

A little example? Here's a small piece of code that uses the weapon on the opponent as long as we have TP:

Dead simple, right? Well, no! An error shows up at runtime: AI interrupted: too many operations consumed. ▶ AI Basic, line 13

What could the problem be... Well, quite simply, as long as the condition getTP() > 0 holds, the loop keeps running. But what happens when we can't use the weapon? Well, we don't lose any TP! And the condition stays true! So we have a loop that runs to infiniiiiiiiiiiiiiiiiiiiiiity (in other words, a lot).

To fix this, we can check the return value of the useWeapon function and analyze the result.

Possible fix:

My AI isn't that small!

Size isn't everything!

Without reading your AI, it's hard for me to fix it... Still, here are a few possibilities:

Unknown function

So this time I was careful: I put the right number of parameters in my function. Sure, but you can't pass just any parameter to native functions. These functions require certain input types. If you try to use them with the wrong parameters, you'll get something like this:

AI Basic, line 14 ▶ Unknown function: useWeapon(function) AI Basic, line 14 ▶ Unknown function: useWeapon(null)

Here, the useWeapon function takes a number as its argument (the id of the targeted entity; it's setWeapon that takes a WEAPON_... constant) and we're trying to give it a function, or a null. Of course, the generator grumbles at us.

AI Basic, line 11 ▶ Unknown function: sort(number, null) sort expects an array as its first parameter, and we're trying to give it a number.

AI Basic, line 8 ▶ Unknown function: getNearestEnemyTo(array) getNearestEnemyTo takes an entity id as its parameter, not an array.

This error can also happen because of forgotten parentheses:

Here we store the getNearestEnemy function in the enemy variable, rather than its result (see Functions). Fix: var enemy = getNearestEnemy();