lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

two ways i can think of:
Just return a boolean from your C function, and have your Lua script check this value against true:
if not Entity:SendText ("Hello!") then
    print ("SendText failed!");
Or, use lua_error in your C function and call SendText in a protected environment:
local success, error_msg = pcall (Entity:SendText, "Hello!");
if not success then
    print ("SendText failed:  ", error_msg);


From: on behalf of John Klimek
Sent: Fri 12/30/2005 9:05 AM
To: Lua list
Subject: How do I check for errors returned from a C function?

For my game I plan to have lots of objects running scripts (players,
npcs, rooms, items, etc).  Whenever an object runs a script I create a
global lua userdata called "Entity".  The lua script can then check
for the actual entity type by using Entity:GetType.

Now suppose that only player entities can use the SendText C function.
 The following code would be invalid:

if (Entity:GetType() == 'item') then
  Entity:SendText("Hello!"); // invalid, item Entity's cannot use SendText

So what I'd like to do is for the C function SendText is throw back an
error.  I'm aware that I can push the error message to the stack and
then use lua_error but how do I check for the error inside of my lua

Also, does my above script engine logic seem OK?