Skip to content

Instantly share code, notes, and snippets.

@OmegaExtern
Last active August 29, 2015 14:23
Show Gist options
  • Save OmegaExtern/e383730274b13ed3c551 to your computer and use it in GitHub Desktop.
Save OmegaExtern/e383730274b13ed3c551 to your computer and use it in GitHub Desktop.
Patched type function to fix 'no value' type bug: http://facepunch.com/showthread.php?t=1472820 (preview here: http://i.imgur.com/c7ikBsJ.png)
-- Revision 4. Added parentheses on if-statements.
-- Since I have experienced a strange bug in singleplayer on the serverside (clean Garry's Mod dev-branch install):
-- lua_run type( player.GetAll()[1]:SteamID64() )
-- Above command should return "nil" or throw an error, as function call is given an argument. But it does NOT.
-- I have decided to fix it. Here is my second attempt at it (also updated script style to: C language without semicolon):
if ( BRANCH:upper() != 'DEV' ) then
-- Just because I can. This can be ignored (doesn't throw error).
print( "Warning: dev branch not detected, this may not work as expected." )
end
if ( !game.SinglePlayer() || game.MaxPlayers() < 2 ) then
-- Throw error, if not in singleplayer mode.
error( "type patch failed due it is only meant to fix bug in singleplayer." )
end
if ( !SERVER || CLIENT ) then
-- Throw error as this is not necessary for the client, it works fine there.
error( "type patch failed due not run on the serverside. Use lua_openscript console command." )
end
if ( !isfunction( _G.type2 ) && isfunction( _G.type ) ) then
-- Such if-condition is necessary (to keep a copy of original function untouched, in case if a script is run more than once).
-- Store original type function.
_G.type2 = _G.type
end
-- Patch original type function.
_G.type = function( ... )
-- varargs = variadic function (PHP) :D
local args = { ... }
if ( #args < 1 || args[1] == nil ) then
return "nil"
end
return _G.type2( ( args[1] ) ) -- Otherwise, return whatever the original function returns.
end
print( "Global type function patched successfully. You can still access non-patched type function using 'type2'." )
-- You could actually define a new global function with different name if you don't want to patch original declaration..
-- Whatever. In my case, I have patched original declaration of the global type function.
-- Make sure that patch is applied where (and before it is) needed.
-- Feel free to modify, do whatever, to fit your needs. Need help? Search me and then ask.
print( type( player.GetAll()[1]:SteamID64() ), type2( ( player.GetAll()[1]:SteamID64() ) ) )
-- You can also read a whole drama about this, on the following thread: http://facepunch.com/showthread.php?t=1472820
@JamesxX
Copy link

JamesxX commented Jun 26, 2015

Try again.

@OmegaExtern
Copy link
Author

Improved it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment