Basics: Compilation

The first time a function, handler, state etc. is created, it must be compiled (F7) to be taken into account by the game. In case of compilation errors, your mistakes will be highlighted in the Code Editor as well as the Log Output. Here is a list of the most common error messages.


Table of Contents

Coding means making mistakes and tinkering with the script until it works. You will come across the message "AI Error: check log for details" more often than anything else in ShiVa. Therefor, it is important to be able to interpret the error messages our compiler gives you.

Locating your Errors

This is what a typical error report looks like:

---------------------------------------------------------------
AI Runtime error : Bad argument type #0
---------------------------------------------------------------
AI Stack Traceback :
[Internal] : Function 'getRotation'
[Line 12] : Function 'updateObject'
[Line 11] : Function
---------------------------------------------------------------
AI Error : check log for details


The message is divided into two main parts: the error type and the error location in the script (call stack). The call stack tells you where to look for your mistake:

First line: The error was in getRotation function.
Second line: This faulty function you seek is located at line 12 of the updateObject function.
Third line: The updateObject function is line 11 of something not a function (so in a handler or in a state).

If the call stack is composed of only one function, the call stack disappears and the function is directly shown in the error type line:

---------------------------------------------------------------
AI Runtime error : [Handler] MyAIModel.onEnterFrame (line 12): Bad argument type #0
---------------------------------------------------------------
AI Error : check log for details

Common Errors

When an error occurs, the ShiVa Editor automatically opens the script and highlights the line where the error occurred.

Reserved Words

local bool = false

Warning : 'bool' is a C++ keyword

This is not an error but a warning to inform you that there might be problems later on when exporting the game to native code (C++). Do not use reserved words to declare your variables like 'bool', instead follow the variable naming scheme from our API: 'bBool' in this case.

Undeclared Variable

nCount = 5

undeclared variable : nCount

It happens when the "local" keyword has not been written before declaring a variable. Write instead: local nCount = 5

It also happens when writing parentheses in a for loop like that: for ( i =0, 5 )
The for loop must not have parentheses: for i = 0, 5

Expected Symbol

This error occurs when a symbol is missing, which means the syntax of a statement is wrong. The following errors are some examples of symbols missing:

for i=0, 5
end

'do' expected near 'end'

The "do" keyword is missing between the "for" declaration and the end of the loop.
for i=0, 5 [...] do [...] end

if ( bOk )
else
end

'then' expected near 'else'

The "then" keyword is missing before "else". A "then" keyword between the "if" and the "else" is required.
if ( bOk ) then [...] else [...] end

repeat
do
until bOk

'end' expected (to close 'do' at line 12) near 'until'

There is no "do" keyword in the repeat syntax.
repeat [...] until bOk

Unexpected Symbol

This error also occurs when he syntax of a statement is wrong, but in this case, a symbol is written directly after another one that cannot precede it.

if ( bOk ) then
else then
end

unexpected symbol near 'then'

The "then" keyword cannot follow "else". "then" can only follow "if":
if ( bOk ) then [...] else [...] end

if then
end

unexpected symbol near 'then'

The "then" keyboard cannot directly follow "if", the condition is missing.
if ( bCondition ) then [...] end

Bad Argument Type #number

local hObject = this.getObject ( )
object.getRotation ( hObject, object.kGlobalSpace )

---------------------------------------------------------------
AI Runtime error : Bad argument type #0
---------------------------------------------------------------

Here the message says "Bad argument type #0". That means something passed as parameter #0 to a function is not valid. Each parameter is indexed, starting from index 0. So "Bad argument type #0" means that it was the very first parameter of this function which was wrong.

Most of the time when there is a "Bad argument type" error, it is because a variable was nil, not initialized, or of the wrong type (number instead of xml for instance). If your first parameter is an object handle, make sure the handle is valid.

Bad Argument Count

object.translateTo ( this.getObject ( ),    3, 3, 3, object.kGlobalSpace )

---------------------------------------------------------------
AI Runtime error : Bad argument count
---------------------------------------------------------------

The number of parameters passed to the function did not match the number of parameters the function accepts. In the example above, the last parameter has been forgotten.

Concatenation

local sName = "Monster"
local nNumber
local sFullName = sName..nNumber

---------------------------------------------------------------
AI Runtime error : [Handler] MyAIModel.onEnterFrame (line 13): attempt to concatenate local 'nNumber' (a nil value)
---------------------------------------------------------------

You can only concatenate non-nil strings and numbers (converted to strings) together. Variables must have a value before you can concatenate them, otherwise they will be nil. It is impossible to convert a boolean to a string, the same is true for handles (objects, tables, hashtables, xml, scenes, users...).

Attempt to call a nil value

---------------------------------------------------------------
AI Runtime error : attempt to call a nil value
---------------------------------------------------------------

This error happens when parentheses have been forgotten, or when calling a variable of an AIModel like:
this.nCount = 3
--or
local count = this.nCount

Attempt to call field (a nil value)

---------------------------------------------------------------
AI Runtime error : [Handler] MyAIModel.onEnterFrame (line 20): attempt to call field 'settranslation ' (a nil value)
---------------------------------------------------------------

This error happens when calling a function that does not exist, either calling a undefined AIModel function or an unknown API function. Usually, you just have a typo in your function call.

Logging for Debugging

You can use the "Log Reporter" module to check certain runtime values of your script for debugging.

-- This function writes a line in the Log Reporter in grey:
log.message ( ... )

-- "Warning" is the same as the previous function, but logs it all in yellow:
log.warning ( ... )

-- Displays a line in the Log Reporter in a red color, creates an error window and stops the engine:
log.error ( ...  )

For each of these functions, many parameters can be logged at the same time, for instance:

local a = "Hello"
local b = 3      
log.message ( a.." "..b )