Errors List

From SA-MP Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 15:17, 7 August 2015
Vince (Talk | contribs)
(004)
← Previous diff
Revision as of 16:54, 4 September 2015
Vince (Talk | contribs)
(055)
Next diff →
Line 122: Line 122:
destination = msg; destination = msg;
</pawn> </pawn>
 +
 +===055: start of function body without function header===
 +This error usually indicates an erroneously placed semicolon at the end of the function header.
==Common Fatal Errors== ==Common Fatal Errors==

Revision as of 16:54, 4 September 2015

Contents

General Pawn Error List

This pages contains the most common errors and warnings produced by the pawn compiler when creating SA:MP scripts.

When the compiler finds an error in a file, it outputs a message giving, in this order:

  • the name of the file
  • the line number were the compiler detected the error between parentheses, directly behind the filename
  • the error class (error, fatal error or warning)
  • an error number
  • a descriptive error message

For example:

hello.pwn(3) : error 001: expected token: ";", but found "{"

Note: The error may be on the line ABOVE the line that is shown, since the compiler cannot always establish an error before having analyzed the complete expression.

Error categories

Errors are separated into three classes:

Errors

  • Describe situations where the compiler is unable to generate appropriate code.
  • Errors messages are numbered from 1 to 99.

Fatal errors

  • Fatal errors describe errors from which the compiler cannot recover.
  • Parsing is aborted.
  • Fatal error messages are numbered from 100 to 199.

Warnings

  • Warnings are displayed for unintended compiler assumptions and common mistakes.
  • Warning messages are numbered from 200 to 299.


Common Errors

001: expected token

A required token is missing.

Example:

error 001: expected token: ";", but found "return"
main()
{
    print("test") // This line is missing a semi-colon. That is the token it is expecting.
    return 1; // The error states that it found "return", this is this line it is referring to,
    // as it is after the point at which the missing token (in this case the semi-colon) should be.
}

002: only a single statement (or expression) can follow each “case”

Every case in a switch statement can hold exactly one statement.
To put multiple statements in a case, enclose these statements
between braces (which creates a compound statement).

Example:

error 002: only a single statement (or expression) can follow each "case"
main()
{
    switch(x)
    {
        case 0: print("hello"); print("hello");
    }
    return 1;
}

The above code also produces other warnings/errors:

error 002: only a single statement (or expression) can follow each "case"
warning 215: expression has no effect
error 010: invalid function or declaration

Fixed:

main()
{
    switch(x)
    {
        case 0:
        {
            print("hello");
            print("hello");
        }
    }
    return 1;
}

004: function "x" is not implemented

Most often caused by a missing brace in the function above.

035: argument type mismatch (argument x)

An argument passed to a function is of the wrong 'type'. For example, passing a string where you should be passing an integer.

Example:

error 035: argument type mismatch (argument 1)
Kick("playerid"); // We are passing a STRING, we should be passing an INTEGER

Fixed:

Kick(playerid);

047: array sizes do not match, or destination array is too small

For array assignment, the arrays on the left and the right side of the assignment operator must have the same number of dimensions. In addition:

  • for multi-dimensional arrays, both arrays must have the same size;
  • for single arrays with a single dimension, the array on the left side of the assignment operator must have a size that is equal or bigger than the one on the right side.
new destination[8];
new msg[] = "Hello World!";
 
destination = msg;

In the above code, we try to fit 12 characters in an array that can only support 8. By increasing the array size of the destination, we can solve this. Note that a string also requires a null terminator so the total length of "Hello World!" plus the null terminator is, in fact, 13.

new destination[13];
new msg[] = "Hello World!";
 
destination = msg;

055: start of function body without function header

This error usually indicates an erroneously placed semicolon at the end of the function header.

Common Fatal Errors

100: cannot read from file: "<file>"

The compiler cannot find, or read from, the specified file. Make sure that the file you are trying to include is in the proper directory (default: <server directory>\pawno\include).

Tip

Image:Light_bulb_icon.png

Multiple copies of pawno can lead to this problem. If this is the case, don't double click on a .pwn file to open it. Open your editor first, then open the file through the editor.


Common Warnings

202: number of arguments does not match definition

The description of this warning is pretty self-explanatory. You've passed either too few or too many parameters to a function. This is usually an indication that the function is used incorrectly. Refer to the documentation to figure out the correct usage of the function.

203: symbol is never used: "symbol"

You have created a variable or a function, but you're not using it. Delete the variable or function if you don't intend to use it. This warning is relatively safe to ignore.

The stock keyword will prevent this warning from being shown, as variables/functions with the stock keyword are not compiled unless they are used.

stock SomeFunction()
{
    // Blah
}
 
// There will be no warning if this function is never used

204: symbol is assigned a value that is never used: "symbol"

Similar to the previous warning. You created a variable and assigned it a value, but you're not using that value anywhere. Use the variable, or delete it. This warning, too, is relatively safe to ignore.

211: possibly unintended assignment

The assignment operator (=) was found in an if-statement, instead of the equality operator (==). If the assignment is intended, the expression must be wrapped in parentheses. Example:

if(vehicle = GetPlayerVehicleID(playerid)) // warning
if(vehicle == GetPlayerVehicleID(playerid)) // no warning
if((vehicle = GetPlayerVehicleID(playerid))) // no warning; the value returned by the function will be assigned to the variable and the expression is then evaluated.

217: loose indentation

The compiler will issue this warning if the code indentation is 'loose', example:

Good:

if(condition)
{
    action();
    result();
}

Bad:

if(condition)
{
    action();
  result();
}

Indentation means to push (indent) text along from the left of the page (by pressing the TAB key). This is common practice in programming to make code easier to read. This warning also exists to avoid dangling-else problem.

219:local variable "foo" shadows a variable at a preceding level

A local variable, i.e. a variable that is created within a function or callback, cannot have the same name as a global variable, an enum specifier, a function, or a variable declared higher up in the same function. The compiler cannot tell which variable you're trying to alter.

It is customary to prefix global variables with 'g' (e.g. gTeam). However, global variables should be avoided where possible.

new team[MAX_PLAYERS]; // variable declared in the global scape
 
function(playerid)
{
    new team[MAX_PLAYERS]; // declared in the local scope, shadows the variable in the global scope, warning 219
    team[playerid] = random(5); // which variable are we trying to update here?
}

External Links