GM/OnLuaError

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Updated code to the newer version)
 
Line 5: Line 5:
 
|Realm=Menu
 
|Realm=Menu
 
|Predicted=No
 
|Predicted=No
|HidePredictionWarning=No
 
|Name=OnLuaError
 
|Parent=GM
 
 
|File=lua/menu/errors.lua
 
|File=lua/menu/errors.lua
 
|Line=11
 
|Line=11
Line 33: Line 30:
 
{{Example
 
{{Example
 
|Description=Code from garrysmod/lua/menu/errors.lua
 
|Description=Code from garrysmod/lua/menu/errors.lua
|Code=hook.Add( "OnLuaError", "MenuErrorHandler", function( str, realm, addontitle, addonid )
+
|Code=local Errors = {}
 +
 
 +
hook.Add( "OnLuaError", "MenuErrorHandler", function( str, realm, addontitle, addonid )
  
 
local text = "Something is creating script errors"
 
local text = "Something is creating script errors"
Line 60: Line 59:
 
end
 
end
  
if ( addonid == nil ) then addonid = 0 end
+
if ( addonid == nil ) then addonid = 0 end
  
 
if ( Errors[ addonid ] ) then
 
if ( Errors[ addonid ] ) then
Line 70: Line 69:
 
end
 
end
  
local error =
+
local error = {
{
+
 
first = SysTime(),
 
first = SysTime(),
 
last = SysTime(),
 
last = SysTime(),
Line 81: Line 79:
  
 
Errors[ addonid ] = error
 
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 )
 
end )
 
}}
 
}}

Latest revision as of 09:14, 8 September 2019

 GM:OnLuaError( )

Contents

Description

Called when a Lua error occurs, only works in the Menu realm.

WARNING

Modify menu state Lua code at your own risk!

Arguments

string error

The error that occurred.

Arguments

number realm

Where the Lua error took place

Arguments

string name

Title of the addon that is creating the Lua errors

Arguments

number id

Steam Workshop ID of the addon creating Lua errors, if it is an addon.

Examples

Example

Code from garrysmod/lua/menu/errors.lua

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 )


Personal tools
Navigation