Garry's Mod Wiki

Revision Difference

GM:OnLuaError#527806

<function name="OnLuaError" parent="GM" type="hook"> <ishook>yes</ishook> <description> Called when a Lua error occurs, only works in the Menu realm. <warning>Modify menu state Lua code at your own risk!</warning> </description> <realm>Menu</realm> <predicted>No</predicted> <file line="11">lua/menu/errors.lua</file> <file line="">lua/menu/errors.lua</file> <args> <arg name="error" type="string">The error that occurred.</arg> <arg name="realm" type="number">Where the Lua error took place</arg> <arg name="name" type="string">Title of the addon that is creating the Lua errors</arg>⤶ <arg name="name" type="string">Title of the addon that is creating the Lua errors.</arg>⤶ <arg name="id" type="number">Steam Workshop ID of the addon creating Lua errors, if it is an addon.</arg> </args> </function>⤶ ⤶ <example>⤶ <description>Code from garrysmod/lua/menu/errors.lua</description>⤶ <code>⤶ local Errors = {}⤶ ⤶ hook.Add( "OnLuaError", "MenuErrorHandler", function( str, realm, addontitle, addonid )⤶ ⤶ local text = "Something is creating script errors"⤶ ⤶ --⤶ -- This error is caused by a specific addon⤶ --⤶ if ( isstring( addonid ) ) then⤶ ⤶ --⤶ -- Down Vote⤶ --⤶ -- steamworks.Vote( addonid, false )⤶ ⤶ --⤶ -- Disable Naughty Addon⤶ --⤶ --timer.Simple( 5, function()⤶ -- MsgN( "Disabling addon '", addontitle, "' due to lua errors" )⤶ -- steamworks.SetShouldMountAddon( addonid, false )⤶ -- steamworks.ApplyAddons()⤶ --end )⤶ ⤶ text = "The addon \"" .. addontitle .. "\" is creating errors, check the console for details"⤶ ⤶ end⤶ ⤶ if ( addonid == nil ) then addonid = 0 end⤶ ⤶ if ( Errors[ addonid ] ) then⤶ ⤶ Errors[ addonid ].times = Errors[ addonid ].times + 1⤶ Errors[ addonid ].last = SysTime()⤶ ⤶ return⤶ end⤶ ⤶ local error = {⤶ first = SysTime(),⤶ last = SysTime(),⤶ times = 1,⤶ title = addontitle,⤶ x = 32,⤶ text = text⤶ }⤶ ⤶ Errors[ addonid ] = error⤶ ⤶ end )⤶ ⤶ local matAlert = Material( "icon16/error.png" )⤶ ⤶ hook.Add( "DrawOverlay", "MenuDrawLuaErrors", function()⤶ ⤶ if ( table.IsEmpty( Errors ) ) then return end⤶ ⤶ local idealy = 32⤶ local height = 30⤶ local EndTime = SysTime() - 10⤶ local Recent = SysTime() - 0.5⤶ ⤶ for k, v in SortedPairsByMemberValue( Errors, "last" ) do⤶ ⤶ surface.SetFont( "DermaDefaultBold" )⤶ if ( v.y == nil ) then v.y = idealy end⤶ if ( v.w == nil ) then v.w = surface.GetTextSize( v.text ) + 48 end⤶ ⤶ draw.RoundedBox( 2, v.x + 2, v.y + 2, v.w, height, Color( 40, 40, 40, 255 ) )⤶ draw.RoundedBox( 2, v.x, v.y, v.w, height, Color( 240, 240, 240, 255 ) )⤶ ⤶ if ( v.last > Recent ) then⤶ ⤶ draw.RoundedBox( 2, v.x, v.y, v.w, height, Color( 255, 200, 0, ( v.last - Recent ) * 510 ) )⤶ ⤶ end⤶ ⤶ surface.SetTextColor( 90, 90, 90, 255 )⤶ surface.SetTextPos( v.x + 34, v.y + 8 )⤶ surface.DrawText( v.text )⤶ ⤶ surface.SetDrawColor( 255, 255, 255, 150 + math.sin( v.y + SysTime() * 30 ) * 100 )⤶ surface.SetMaterial( matAlert )⤶ surface.DrawTexturedRect( v.x + 6, v.y + 6, 16, 16 )⤶ ⤶ v.y = idealy⤶ ⤶ idealy = idealy + 40⤶ ⤶ if ( v.last < EndTime ) then⤶ Errors[k] = nil⤶ end⤶ ⤶ end⤶ ⤶ end )⤶ </code>⤶ ⤶ </example></function>