Search code examples
c++luacalltolua++

Make a call in Lua, on a potentially not exposed Function in tolua++


I have a Lua script that uses some functions of my C++ application. I use this script with 2 different C++ application, and I would like to be able to protect a call for not being bound through tolua.

For example: - Let's say that I have two C++ applications: MyApp1 and MyApp2 - I made a wrapper of MyApp1 with tolua++ and I call one of its functions like this in a Lua script:

MyApp1:MyFunc()

Now I use this same script with MyApp2, that doesn't have any tolua++ binding of this type. And I then get an error of this type:

[string "MyApp2.lua"]:157: attempt to index global 'MyApp1' (a nil value)

So I would like Lua not to send me an error when calling this method. I tried to use:

pcall( MyApp1:MyFunc() )

But the script keeps on crashing on this line.


Solution

  • if MyApp1 then 
      MyApp1:MyFunc() 
    end