KAREL programming variables file

  • I am rather new to KAREL and I was advised by fanuc that a good approach to a more complex karel program is to split the program into separate files. One file is the main program with references to the other project files, one program for routines & functions, and one program for variables. The issue I am having is when I try to create a new karel program for just the variable declaration. When I try to compile a program file with only variable declarations I get a compilation error stating that the program must have a begin statement. If add in a begin statement with no code or just simple variable initializations, it will compile ok; but, when I reference the variable program from another project file with %include K_variables, I get an error that says there is an error with the VAR command in the variable file and that an ID is expected. Should I even be compiling the program for variable declaration? What is the proper way to create and reference a file for just variables?


    My variable file is something like:
    Program K_Vars
    VAR
    int1:INTEGER
    int2:INTEGER
    END K_Vars


    The main program would look like this:
    Program k_Main
    %INCLUDE K_Vars
    Begin
    do stuff here...
    if(int1 = 0) THEN
    do more stuff...
    ENDIF
    END K_Main

  • When you %INCLUDE something, the contents of the included file will be merged with the main file, replacing the %INCLUDE directive.


    Your K_Vars.kl should become:

    Code
    VAR
     int1:INTEGER
     int2:INTEGER


    Then your k_Main.kl will compile correctly.


    Beware the %INCLUDE order and position, global variables declarations are on top, routines must be defined before beeing used and so on.

  • Hello,


    I tried the codes above with no sucess, can someone please explain.


    PROGRAM k_Main

    %INCLUDE K_Vars

    BEGIN

    --do stuff here...

    IF(int1 = 0) THEN

    --do more stuff...

    ENDIF

    END K_Main



    File name K_Vars.kl as below:


    VAR

    int1:INTEGER

    int2:INTEGER



    Robogiude V9(Rev N)


    Errors after build all:


    Set Extra Includes

    pasted-from-clipboard.png


    \TestInclude\SubProgs\k_vars.kl(1)


    1 VAR "PROGRAM" expected

    1 VAR Id expected

    2 int1:INTEGER "BEGIN" expected.

    2 int1:INTEGER ";" or new line expected.

    2 int1:INTEGER Invalid statement or "ENDxxx" or "UNTIL" expected.

    3 int2:INTEGER Invalid statement. ";" or new line expected.

    3 int2:INTEGER Invalid statement. ";" or new line expected

  • Hermann,


    My apology, I found my problems.


    In general, the sequence of creating and building Karel using %INCLUDE files in robogiude confused me.

    After trials and errors, learning the software, I got my codes working.


    Thanks.

Advertising from our partners