Updating Scripts From GMod 12 to GMod 13

From Garry's Mod
Jump to: navigation, search

“Why your scripts are now broken”

Several changes to the Lua API were made in the transition between GMod 12 to GMod 13. This page is an attempt to highlight them. It was originally created in the form of a Google Document created by Garry. The old method of doing things is listed on the left, while the new is on the right side.

Changes

  • The PANEL:Paint hook now receives a width and height parameter. This shouldn't affect much in itself - but if you're calling a derma.SkinHook you will need to pass the width and height along, as it needs these values too now. You could start doing this now in your code without any negative effects.
Old New
function PANEL:Paint()
	derma.SkinHook( "Paint", "Frame", self )
	return true
end
function PANEL:Paint( w, h )
	derma.SkinHook( "Paint", "Frame", self, w, h )
	return true
end
  • Entity:SetColor and Entity:GetColor now deal with Color structures only. Not with single variables. This is something that is always mentioned - and since we're breaking stuff anyway now seems to be a good time to make the change. SetColor will set the color to purple if the passed variable isn't a Color.
Old New
Entity:SetColor( r, g, b, a )
local r, g, b, a = Entity:GetColor()
Entity:SetColor( Color( r, g, b, a ) )
local c = Entity:GetColor()
local r,g,b,a = c.r, c.g, c.b, c.a
  • KeyValuesToTable and TableToKeyValues are now in the util library.
Old New
KeyValuesToTable( blah )
TableToKeyValues( blah )
util.KeyValuesToTable( blah )
util.TableToKeyValues( blah )
  • SetMaterialOverride, cam.StartMaterialOverride (which did the same thing) are removed and replaced with render.MaterialOverride.
Old New
cam.StartMaterialOverride( blah )
SetMaterialOverride( blah )
render.MaterialOverride( blah )
  • DMultiChoice is replaced by DComboBox. When I originally made them they were named the wrong way around. DMultiChoice is now removed. This is because DListView was basically recreating all the functionality of DMultiChoice. DListView has extra settings to remove colums and headers - which make it exactly like the old DComboBox. Complicated eh?
NOTE

SetEditable(boolean) doesn't exist on DComboBox

  • DSysButton was removed. This panel used the Marlett font to create icons such as the "close" button for windows. This isn't needed anymore since we're using GWEN skins. Plus it didn't work properly on Mac anyway.
  • Datastream module is removed. Datastream was less than optimal when it came to networking stuff. We now have the new net library - which doesn't work in exactly the same way, but is much more optimized to do this stuff. You should use the net library instead of datastream.
  • DBevel is removed. This panel was purely decorational - so it shouldn't be hard for you to replace.
  • utilx no longer exists. Please replace all ocurances of utilx in your code to the util library.
  • entity.Classname. Caps is now enforced properly. Use entity.ClassName instead. (N is upper case)
Old New
function TOOL:RenderToolScreen()
	cam.Start2D()
	...
	cam.End2D()
end
function TOOL:DrawToolScreen( w, h )
	cam.Start2D()
	...
	cam.End2D()
end
  • file.Find now returns two tables - files and folders. The second argument is the search path.
    • "LUA" searches the lua files (in /lua/, in your gamemodes, in all the addons). This replaces file.FindInLua.
    • "GAME" searches all the mounted content (main folder, addons, mounted games etc).
    • "MOD" searches only the garrysmod folder.
    • "DATA" searches in the data folder.

Type "path" in the console to see the paths.

Old New
file.FindInLua("entities/*.lua")
file.Find("entities/*.lua","LUA")
file.Find("models/model.mdl")
file.Find("models/model.mdl", "GAME")
file.FindDir("exampledir")
file.Find("example", "DATA")
(2nd return)
file.Read("settings/users.txt", true)
file.Read("settings/users.txt", "GAME")
file.Read("example.txt")
file.Read("example.txt", "DATA")
file.Exists("example.txt")
file.Exists("example.txt", "DATA")
file.Exists("models/models.mdl", true)
file.Exists("models/models.mdl", "GAME")
  • file.ReadString removed, use file.Read
Old New
file.ReadString( ... )
file.Read( ... )
  • DColorMixer.RGBBar is now DColorMixer.RGB. GMod 13 uses Grocel's DColorMixer.
  • STool language phrases changed.
Old New
languge.Add("Tool_toolname_name", "Text")
languge.Add("tool.toolname.name", "Text")
Old New
http.Get( url, headers, callback, ... )
http.Fetch( url, onsuccess, onfailure )
http.Post( url, params, onsuccess, onfailure )
Old New
timer.Simple( 10, MyFunc, "hello", 10, 10 )
timer.Simple( 10, function() MyFunc( "hello", 10, 10) end )

or

local func = function()
	MyFunc( "hello", 10, 10)
end

timer.Simple( 10, func )
  • Angle functions have been unified. Before some were Set/GetAngles and some were Set/GetAngle. Now they're all Set/GetAngles()
Old New
blahblah:SetAngle()
blahblah:GetAngle()
blahblah:SetAngles()
blahblah:GetAngles()
  • timer.IsTimer is now timer.Exists. Sounds better, yeah.
Old New
timer.IsTimer("derp")
timer.Exists("derp")
  • math library duplicate functions removed (Deg2Rad, Rad2Deg)
Old New
math.Rad2Deg(1)
math.Deg2Rad(1)
math.deg(1)
math.rad(1)
  • server_settings module removed, use cvars instead.
  • Renamed Material:[Get|Set]Material* to Material:[Get|Set]* (ie, :SetVector instead of :SetMaterialVector)
Old New
GetWorldEntity()
game.GetWorld()
isDedicatedServer()
game.IsDedicated()
SinglePlayer()
game.SinglePlayer()
MaxPlayers()
game.MaxPlayers()
  • WorldSound has been replaced with sound.Play.
Old New
WorldSound( "zinger/items/darthit.mp3", tr.HitPos, 80, 120 )
sound.Play( "zinger/items/darthit.mp3", tr.HitPos, 80, 120 )
Old New
Player:GetCursorAimVector()
Player:GetAimVector()
  • Entity Colour Alpha Changes. In previous version of GMod setting the Alpha of an entity would also change its render mode. In 13 this behaviour was stopped - as it interfered with other things (like entities showing as invisible in some maps). This means that in 13 you need to change the render mode too - if you want your entity to show as transparent.
Old New
ent:SetColor( 255, 255, 255, 100 )
ent:SetColor( Color( 255, 255, 255, 100 ) )
ent:SetRenderMode( RENDERMODE_TRANSALPHA )
  • GM:EntityTakeDamage has been changed. The arguments
    attacker, inflictor, amount
    have been removed as they're available by querying the passed damageinfo.
Old New
GM:EntityTakeDamage( ent, attacker, inflictor, amount, damageinfo )
GM:EntityTakeDamage( ent, dmginfo )

and

local attacker = dmginfo:GetAttacker()
local inflictor = dmginfo:GetInflictor()
local amount = dmginfo:GetDamage()
  • The bitwise operators have been removed. I know the old way was nicer - but we took a vote - sorry!
Old New
local a = CONTENTS_SOLID | CONTENTS_MOVEABLE
local a = bit.bor( CONTENTS_SOLID, CONTENTS_MOVEABLE )
  • ValidEntity has been removed. Use IsValid instead.
Old New
if ( ValidEntity( ent ) ) then
if ( IsValid( ent ) ) then
Personal tools
Navigation