2

There has been a lot of post about this kind of error and most people say it is related to table and array indexing problems. But I am not using tables at all, I am just trying to call a library function I made and I get this error. Here is the lua script called from java:

String script = new String (  
             "function event_touch (  )"  
            + "   pb.open_form ('view_monster');"
            + "   print ('I ran the lua script');"
            + "end");

PBLUAengine.run_script(script, "event_touch");

This gives me the following error when trapping the exception:

"function event_touch ( ) pb.open_form ('view_monster'); print ('I ran the lua script');end:1 attempt to index ? (a nil value)"

The run_script() function calls the script like this( I am using luaj):

public static LuaValue run_script ( String script )
    {
        try
        {
            LuaValue chunk = s_globals.load( script );
            return chunk.call();
        }
        catch ( Exception e)
        {
           Gdx.app.error ("PBLUAengine.run_script()", e.getMessage() );
        }   

        return null;
    }

The library method goes like this and the same piece of code works when called from java:

static class open_form extends OneArgFunction
{
        public LuaValue call (LuaValue formname)
        {           
            String tmpstr = (String ) CoerceLuaToJava.coerce(formname, java.lang.String.class );

            try
            {               
                PBscreen_game.hide_subscreen(PBscreen_game.MENU_SUBS); 
                PBscreen_game.show_subscreen ( PBscreen_game.FORM_SUBS);
                PBsubscreen_form.open_form ( new PBform_regular ( tmpstr ) );   
            }
            catch (Exception e)
            {
                Gdx.app.error("PBLUAlibrary.open_form", e.getMessage());
            }

            return valueOf ( 0 );
        }
    }

It basically convert the lua parameter to string, create a new from and pass in parameter the string.

The declaration of the library functions goes like this:

public LuaValue call( LuaValue modname, LuaValue env )
    {
        LuaValue library = tableOf();
        library.set( "open_form", new open_form() );
        library.set( "open_system_form", new open_system_form() );
        env.set( "pb", library );
        return library;
    }

Which could be the only "table" I can see in the whole system. This is generally used link the right class with the right function name.

Anybody have an idea?

larienna
  • 151
  • 4
  • 12
  • Where does that last `call` function get called to set the `pb` global in the lua state? Is that `run_script` function you showed the one that you are calling in the first code snippet? Where does that second argument go? Should you be setting objects into the library or functions? (I should mention that I don't know java or luaj at all.) – Etan Reisner Jul 28 '15 at 03:11

2 Answers2

2

most people say it is related to table and array indexing problems

It's related to table and array indexing. If you try to index an object and that object is nil, you'll get that error:

I am not using tables at all [..] Here is the lua script:

pb.open_form

pb is being indexed. It's probably nil.

Mud
  • 28,277
  • 11
  • 59
  • 92
0

I seems that I solved the problem by adding a require line to include the library. So the new script is:

String script = new String (
            "require 'com.lariennalibrary.pixelboard.library.PBLUAlibrary'"
            +  "function event_touch (  )"  
            + "   pb.open_form ('view_monster');"
            + "   print ('I ran the next button lua script');"
            + "end");

It ask to include my library class which will add all the "pb.*" functions. I probably deleted the line by error, or managed to make it work somehow without it. Since this library will be required by all script, I might append it by default before each script I try to run.

Thanks Again

larienna
  • 151
  • 4
  • 12