Team Spartan Cookies & Milk Forums
xpcall failed - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: xpcall failed (/thread-728.html)



xpcall failed - LocInt - 14-02-2015

Hey,

I have all my functions inside xpcall for debug.

local Result = xpcall(broadcastMessage(Type, Message, ply),function(Error) return end)
saveError(Result)

I added few errors inside broadcastMessage for debug.

Doesn't seem to work:
sv: SFConfusedtarfall/ENCOM_mainframe.txt:98: bad key to string index (number expected, got string)
sv: SFConfusedtarfall/ENCOM_mainframe.txt:105: attempt to index global 'cht' (a nil value)


RE: xpcall failed[BUG] - Person8880 - 14-02-2015

You're calling broadcastMessage, which returns presumably nothing, which means you then pass nil to xpcall.

xpcall then calls nil because that's what the function call returned.

This is what it should be:
local function ErrorHandler( Error )
print( "Oh noes! An error:", Error )
end
local Result = xpcall( broadcastMessage, ErrorHandler, Type, Message, ply )

Can you see why yours didn't work now?


RE: xpcall failed - LocInt - 14-02-2015

(14-02-2015, 04:48 PM)Person8880 Wrote: You're calling broadcastMessage, which returns presumably nothing, which means you then pass nil to xpcall.

xpcall then calls nil because that's what the function call returned.

This is what it should be:
local function ErrorHandler( Error )
print( "Oh noes! An error:", Error )
end
local Result = xpcall( broadcastMessage, ErrorHandler, Type, Message, ply )

Can you see why yours didn't work now?

Should my line crash the whole chip?


RE: xpcall failed - Person8880 - 14-02-2015

Of course, xpcall can't catch something before it's even received its arguments.

You called broadcastMessage(), and it errored.

Note the difference. xpcall takes a function, you performed a function call whose result was passed to xpcall (or in this case, the function errored before it even got that far).


RE: xpcall failed - LocInt - 14-02-2015

(14-02-2015, 04:53 PM)Person8880 Wrote: Of course, xpcall can't catch something before it's even received its arguments.

You called broadcastMessage(), and it errored.

Note the difference. xpcall takes a function, you performed a function call whose result was passed to xpcall (or in this case, the function errored before it even got that far).

Then I didn't really understund how this function works. Thanks I will try this.


RE: xpcall failed - Person8880 - 14-02-2015

It's a very common mistake for people new to Lua. I did it too when I was starting out!

Just have to see functions more as variables than anything special and it starts to make more sense.


RE: xpcall failed - LocInt - 14-02-2015

(14-02-2015, 04:58 PM)Person8880 Wrote: It's a very common mistake for people new to Lua. I did it too when I was starting out!

Just have to see functions more as variables than anything special and it starts to make more sense.

T-16 and me were thinking if there could be real example for every function.


RE: xpcall failed - Person8880 - 14-02-2015

That would take quite a long time to do!

The docs do support it (see the preprocessor directives), so we'll see.


RE: xpcall failed - LocInt - 14-02-2015

(14-02-2015, 05:15 PM)Person8880 Wrote: That would take quite a long time to do!

The docs do support it (see the preprocessor directives), so we'll see.

I know bunch of those functions as well. You could always let me help. Big Grin