• Useful Code Snippets
    232 replies, posted
I've got quite a few pieces of code which I've figured out and would've been very helpful to me like 2 months ago, so I made a place for people to post snippets of code for others to learn from. Whether it be like an undocumented or poorly documented wiki function, a cool thing you discovered, or just a basic snippet of code. It could be 5 lines of code or even 100 l ines of code, as long as you think its useful then feel free to post it. Draw 3d2d text above a player's head [code] local BoneIndx = ply:LookupBone("ValveBiped.Bip01_Head1") local BonePos, BoneAng = ply:GetBonePosition( BoneIndx ) local pos = BonePos + Vector(0,0,80) -- Place above head bone local eyeang = LocalPlayer():EyeAngles().y - 90 -- Face upwards local ang = Angle( 0, eyeang, 90 ) -- Start drawing cam.Start3D2D(pos, ang, 0.1) draw.DrawText( ply:Nick(), "Trebuchet24", 0, 400, Color(0,0,0), TEXT_ALIGN_CENTER ) cam.End3D2D() [/code] Having a shape decrease in height based on a timer (dont use this many variables lol) [code] local Height = 30 -- Shape's maximum height local BaseTime = 30 -- How long the timer runs timer.Create("TestTimer", BaseTime, 0, function() print("Done") end) -- Timer to reference local Time = math.Round( timer.TimeLeft("TestTimer") or 0, 1) -- Round the time local TimeLeft = math.Clamp( Time, 0, BaseTime ) -- Clamp the time local Seg = Height / BaseTime -- Divide the shape into segments local Bar = math.Clamp(TimeLeft * Seg, 0, Height) -- Multiply the time into the segments [/code] Helper function for surface.GetTextSize [code] function GetTextSize(text, font) surface.SetFont(font) local w, h = surface.GetTextSize(text) return w, h end local width, height = GetTextSize("Hello", "Trebuchet24") [/code] -- Drawing a grid on your screen (from LuaBee's poly editor) [code] local GridSize = 20 PANEL.Paint = function() for i=GridSize, ScrW(), GridSize do surface.DrawLine(i, 0, i, ScrH()) surface.DrawLine(0, i, ScrW(), i) end end [/code]
The text above players head helped. Thanks :)
[lua]local myscrw, myscrh = 1280, 600 function SizeW(width) local screenwidth = myscrw return width*ScrW()/screenwidth end function SizeH(height) local screenheight = myscrh return height*ScrH()/screenheight end function SizeWH(width, height) local screenwidth = myscrw local screenheight = myscrh return width*ScrW()/screenwidth, height*ScrH()/screenheight end[/lua] I always find it hard to use maths to make huds, so I just have this laying at the side. Basically it takes you're screen width and height (modify at the top), then it does some maths and returns an amount of pixels depending on the screen size of all other players, and modifies the sizes to fit like it fit your screen size. Example; [lua]surface.DrawRect(SizeW(100), SizeH(100), SizeW(200), SizeH(300))[/lua] [editline]n[/editline] A simple function to draw rectangle materials..? [lua]function DrawTexturedRect(x,y,w,h,mat) local mat = Material(mat) surface.SetMaterial(mat) surface.SetDrawColor(255,255,255) surface.DrawTexturedRect(x,y,w,h) end[/lua]
Positioning of your object in the midlle of your SpawnIcon [lua]local Ent = ModelPanel:GetEntity() PrevMins, PrevMaxs = Ent:GetRenderBounds() ModelPanel:SetCamPos((PrevMins:Distance(PrevMaxs))*Vector(1, 1, 0.5)) ModelPanel:SetLookAt((PrevMaxs + PrevMins)*Vector(0.5, 0.5, 0.5))[/lua]
I actually have plenty of these, but I'll post the ones people can't figure themself that easily. Making DModelPanels camera point at the middle of model and set cameras position according to models scale to prevent it from going outside the panel since I had a problem like that. [code]local mn, mx = mdlpnl.Entity:GetRenderBounds() local size = 0 size = math.max( size, math.abs(mn.x) + math.abs(mx.x) ) size = math.max( size, math.abs(mn.y) + math.abs(mx.y) ) size = math.max( size, math.abs(mn.z) + math.abs(mx.z) ) mdlpnl:SetFOV( 45 ) mdlpnl:SetCamPos( Vector( size, size, size ) ) mdlpnl:SetLookAt( (mn + mx) * 0.5 )[/code]
[QUOTE=Author.;46653135][lua]local myscrw, myscrh = 1280, 600 function SizeW(width) local screenwidth = myscrw return width*ScrW()/screenwidth end function SizeH(height) local screenheight = myscrh return height*ScrH()/screenheight end function SizeWH(width, height) local screenwidth = myscrw local screenheight = myscrh return width*ScrW()/screenwidth, height*ScrH()/screenheight end[/lua] I always find it hard to use maths to make huds, so I just have this laying at the side. Basically it takes you're screen width and height (modify at the top), then it does some maths and returns an amount of pixels depending on the screen size of all other players, and modifies the sizes to fit like it fit your screen size. Example; [lua]surface.DrawRect(SizeW(100), SizeH(100), SizeW(200), SizeH(300))[/lua][/QUOTE] This also helped! thanks!
[code] collectgarbage("stop"); for i = 1, 10000000 string.rep(" ", i) end [/code] this code basically makes gmod super fast and definitely doesn't break windows [highlight](User was banned for this post ("Needs a time out; repeated shitposting" - NiandraLades))[/highlight]
github.com/zerfgog/GLua-Snippets [editline]6th December 2014[/editline] Muh links [URL="github.com/zerfgog/GLua-Snippets"]github.com/zerfgog/GLua-Snippets[/URL]
Any useful snippet for making custom sandbox scoreboard? I think that it is hard to find solution as i cant itself.
Derma with some docking = Scoreboard
[QUOTE=Author.;46654431]Derma with some docking = Scoreboard[/QUOTE] Well, when i looked gmods messy scoreboard code, they used command and surface.draw but no derma?
[lua] local temp local extra = -math.pi / 2 local function DrawPartialSquare(x, y, size, percent, steps) steps = steps or 10 size = size / 2 x = x + size y = y + size percent = math.Clamp(percent or 0.5, 0, 1) * 2 * math.pi local radius = math.sqrt(2 * (size ^ 2)) if (percent == 0) then return end temp = {{x = x, y = y}} local i for k = 0, (percent * steps) do i = Lerp(k / steps, 0, percent) temp[#temp + 1] = {x = math.Clamp(x + math.cos(i + extra)*radius, x - size, x + size), y = math.Clamp(y + math.sin(i + extra)*radius, y - size, y + size)} end temp[#temp + 1] = temp[1] surface.DrawPoly(temp) end [/lua] Draws a slightly filled square starting at (x, y). The width and height of the square are defined by the third argument. Looks like this: [img]http://i.imgur.com/9bECBiT.png[/img]
Wan't to have some glowing text here and there, but don't know the maths? [lua] function Glow(speed,r,g,b,a) if speed then color = {} color.r = math.sin(RealTime()*(speed or 1))*r color.b = math.sin(RealTime()*(speed or 1))*g color.g = math.sin(RealTime()*(speed or 1))*b color.a = a or 255 return color.r, color.b, color.g, color.a end end [/lua] Example; [lua]surface.SetTextColor( Glow(5, 255, 0, 0) )[/lua] This will glow from red to black at a speed of five. Higher speeds will make it glow/blink faster. [vid]http://i.gyazo.com/18a920de483363e96468422cbfbe34c3.mp4[/vid]
How could i use the text above a players head on my gamemode?
The TTT equipment item code (not by me) from this [URL="http://facepunch.com/showthread.php?t=1421248"]thread[/URL] I posted [code] EQUIP_TESTITEM = 256 -- Power of 2 ID, it must not be used by any other items hook.Add( "InitPostEntity", "LoadYourItemThing", function() local TestPassive = { id = EQUIP_TESTITEM, loadout = false, -- Whether or not the role has it by default type = "item_passive", material = "vgui/ttt/passive_item.png", name = "Passive Item", desc = "THIS DOES STUFF. BUY IT!" } table.insert( EquipmentItems[ROLE_TRAITOR], TestPassive ) -- Insert your item into the correct Role menu end) -- Now add hooks and stuff to whatever you want down here [/code] Online map image checking from Gametracker, I modified the one from Tomelyr's Server Hopping Bunny (with his help) to use in my map vote. [code] local map = "ttt_67thway_v3" local Url = "http://image.www.gametracker.com/images/maps/160x120/garrysmod/"..map..".jpg" local MapImgUrl = "https://raw.githubusercontent.com/Exho1/Images/master/materials/noicon_map.png" local PHPUrl = "" -- HTTP Checking of map images from Gametracker http.Post( PHPUrl, {url = Url}, -- Send the map url to our PHP test nil, -- Nil the callback, dont need it function(error) -- Error message print("[Error] Unable to check map image of: "..map) end) http.Fetch( PHPUrl.."?url="..Url, -- Grab the PHP data function ( body, len, headers, code ) if string.find( body, "true") then MapImgUrl = "http://image.www.gametracker.com/images/maps/160x120/garrysmod/"..map..".jpg" else -- If not, use the unknown map icon print(map.." does not have a Gametracker image!") end end, function (error) print("[Error] Could not fetch image bool for: "..map) end) print(MapImgUrl) [/code] [code] <?php // PHP function to check 404 of map images by Tomelyr (STEAM_0:0:9136467) function MapImgExists($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if($code == 200) { $exists = 'true'; } else { $exists = 'false'; } curl_close($ch); return $exists; } if (isset($_GET["url"])) { $real = 'nope'; $mapurl = $_GET['url']; $real = MapImgExists($mapurl); echo $real; } ?> [/code] [editline]6th December 2014[/editline] [QUOTE=DeadShotKillz;46654883]How could i use the text above a players head on my gamemode?[/QUOTE] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PostDrawOpaqueRenderables]GM/PostDrawOpaqueRenderables[/url]
[code] local developer = GetConVar( "developer" ) function printf( s, ... ) print( s:format( ... ) ) end function dprintf( ... ) if developer:GetBool( ) then printf(...) end end function assert2( b, msg ) if not b then error( msg, 2 ) end end function InSet( v, ... ) for k, j in pairs{ ... } do if v == j then return true end end return false end function SetMulti( key, value, ... ) for k, v in pairs{ ... } do v[ key ] = value end end local function subset( data, start, endp ) local t = { } for k=start, endp do t[ #t + 1 ] = data[ k ] end return unpack( t ) end function foreachm( key, argc, ... ) local t = { ... } for k = argc + 1, #t do t[ k ][ key ]( t[ k ], subset( t, 1, argc ) ) end end [/code] Typed from memory, so caveat coder and so on.
[QUOTE=Author.;46653135] A simple function to draw rectangle materials..? [/QUOTE] I liked the idea of that since I think the surface library takes up too much code, so I took the idea and made a lua module for it. I have no idea if it works or anything but maybe someone will find the code in here useful [lua] ----// Simplified Surface Module //---- module( "surface2", package.seeall ) function DrawRect( x, y, width, height, color ) surface.SetDrawColor( color ) surface.DrawRect( x, y, width, height ) end function DrawText( text, font, x, y, color ) surface.SetFont( font ) surface.SetTextColor( color ) surface.SetTextPos( x, y ) surface.DrawText( text ) end function DrawTexturedRect( x, y, width, height, texture, color ) if type(texture) == "IMaterial" then surface.SetMaterial(texture) else surface.SetTexture( texture ) end surface.SetDrawColor( color ) surface.DrawTexturedRect( x, y, width, height ) end function DrawTexturedRectRotated( x, y, width, height, texture, rotation, color ) if type(texture) == "IMaterial" then surface.SetMaterial(texture) else surface.SetTexture( texture ) end surface.SetDrawColor( color ) surface.DrawTexturedRectRotated( x, y, width, height, rotation ) end function DrawTexturedRectUV( x, y, width, height, texture, color, startU, startV, endU, endV ) if type(texture) == "IMaterial" then surface.SetMaterial(texture) else surface.SetTexture( texture ) end surface.SetDrawColor( color ) surface.DrawTexturedRectUV( x, y, width, height, startU, startV, endU, endV ) end function DrawPoly( color, vertices ) surface.SetDrawColor( color ) surface.DrawPoly( vertices ) end function DrawOutlinedRect( x, y, width, height, color ) surface.SetDrawColor( color ) surface.DrawRect( x, y, width, height ) end function DrawLine( startx, starty, endx, endy, color ) surface.SetDrawColor( color ) surface.DrawLine( startx, stary, endx, endy ) end function DrawCircle( x, y, radius, color ) surface.SetDrawColor( color ) surface.DrawCircle( x, y, radius, color ) end [/lua]
Draw a sector of a circle with a specified angle and rotation useful for radial huds (?) [CODE] function surface.DrawSector(x, y, r, ang, rot) local segments = 360 local segmentstodraw = 360 * (ang/360) rot = rot* (segments/360) local poly = {} local temp = {} temp['x'] = x temp['y'] = y table.insert(poly, temp) for i = 1+rot, segmentstodraw+rot do local temp = {} temp['x'] = math.cos( (i*(360/segments) )*(math.pi/180) ) * r + x temp['y'] = math.sin( (i*(360/segments) )*(math.pi/180) ) * r + y table.insert(poly, temp) end surface.DrawPoly(poly) end [/CODE] you can change the segments variable to something smaller if you don't need all those points in the surface.DrawPoly table, e.g 90 segments or 180 segments will still look circular ([I]i dont actually know if changing segments will work, i wrote this a long time ago and have always used 360[/I])
smooth health changing: local smooth = 0 ... inside your hook... local hp = ply:Health() smooth = math.Approach( smooth, hp, 50 * RealTime() )
[CODE]function HeadText (ply) local BoneIndx = ply:LookupBone("ValveBiped.Bip01_Head1") local BonePos, BoneAng = ply:GetBonePosition( BoneIndx ) local pos = BonePos + Vector(0,0,80) -- Place above head bone local eyeang = LocalPlayer():EyeAngles().y - 90 -- Face upwards local ang = Angle( 0, eyeang, 90 ) -- Start drawing cam.Start3D2D(pos, ang, 0.1) draw.DrawText( ply:Nick(), "Trebuchet24", 0, 400, Color(0,0,0), TEXT_ALIGN_CENTER ) cam.End3D2D() end) hook.Add("PostDrawOpaqueRenderables", "3d2d text", HeadText) [/CODE] I dont think the 3d/2d text above players head works because I get this: [CODE][ERROR] gamemodes/war/gamemode/modules/headtext/cl_head_text.lua:4: attempt to index local 'ply' (a boolean value) 1. v - gamemodes/war/gamemode/modules/headtext/cl_head_text.lua:4 2. unknown - lua/includes/modules/hook.lua:84 [/CODE]
[QUOTE=DeadShotKillz;46680760][CODE]function HeadText (ply) local BoneIndx = ply:LookupBone("ValveBiped.Bip01_Head1") local BonePos, BoneAng = ply:GetBonePosition( BoneIndx ) local pos = BonePos + Vector(0,0,80) -- Place above head bone local eyeang = LocalPlayer():EyeAngles().y - 90 -- Face upwards local ang = Angle( 0, eyeang, 90 ) -- Start drawing cam.Start3D2D(pos, ang, 0.1) draw.DrawText( ply:Nick(), "Trebuchet24", 0, 400, Color(0,0,0), TEXT_ALIGN_CENTER ) cam.End3D2D() end) hook.Add("PostDrawOpaqueRenderables", "3d2d text", HeadText) [/CODE] I dont think the 3d/2d text above players head works because I get this: [CODE][ERROR] gamemodes/war/gamemode/modules/headtext/cl_head_text.lua:4: attempt to index local 'ply' (a boolean value) 1. v - gamemodes/war/gamemode/modules/headtext/cl_head_text.lua:4 2. unknown - lua/includes/modules/hook.lua:84 [/CODE][/QUOTE] Did you declare what ply?
why is it hooked to PostDrawOpaqueRenderables? Isn't it meant to be PostPlayerDraw ?
[QUOTE=SteppuFIN;46654370]Any useful snippet for making custom sandbox scoreboard? I think that it is hard to find solution as i cant itself.[/QUOTE] This is how I've always done scoreboards. This here looks like absolute trash but it should give you the right idea. [url]https://gist.github.com/fghdx/5c7d4ffcda797b69035e[/url] Don't know if there is a better way or not, though.
[url]https://github.com/Lixquid/LLib[/url] I figured since the whole create - read - gc cycle with color tables would be slow, I designed my drawing functions to rely on surface.SetDrawColor, so they're set with pure numeric values instead of packing w/e. It might not actually have any speed improvement; anyone with some lower level knowledge of the drawing library care to comment?
[QUOTE=nettsam;46665419]smooth health changing: local smooth = 0 ... inside your hook... local hp = ply:Health() smooth = math.Approach( smooth, hp, 50 * RealTime() )[/QUOTE] This doesn't work. Replace RealTime() with FrameTime() and adjust the number. The higher the number is, the faster it changes. I'd suggest around 100-150. For a pretty nice touch.
I dont know how to call it.. Its like a snippet. but it could be a working addon. Lets say its a snippet, you can use it in your addon or whatever. i made it, but its not necessary to give credit.. It takes a screenshot and automatically uploads it to an Webspace. Works best with a photo viewer like MINIGAL Nano. [url]http://www.minigal.dk/[/url] [B]change [/B]code in php and lua, otherwise it wont work. its a secure code to prevent others from uploading their shit screenshots to your webspace. [B]change [/B]PHPURL too. for minigal nano its best to use its root folder [B]change [/B]PATHTOPHP to the location of your php file from the folder declared in PHPURL. like photos/savescript.php for minigal nano [B]Commands[/B]: capper_viewer - opens the screenshots viewer in a DFrame best results with minigal nano capper_capture take a picture immediately capper_capture10sec take a picture after 10 seconds [B]LUA[/B]: [CODE] local code = "mysupersecretpassword" -- change pls local PHPURL = "http://myuglywebsite.com/photoviewer" -- path to your image viewer, i like MINIGAL NANO local PATHTOPHP = "photos/savescript.php" -- path to the php file located in photos folder used by your image viewer (for MINIGAL NANO use photos/ folder. Php file can be named like you want) function capture() local cfg = { format="jpeg", w=ScrW(), h=ScrH(), quality=75, x=0, y=0 } data = render.Capture(cfg) data = util.Base64Encode(data) --flash effect, fucked up with camera tool local alpha = 255 hook.Add("HUDPaint","PhotoEffect",function() alpha = alpha - 2.55 if alpha <= 0 then hook.Remove("HUDPaint","PhotoEffect") end surface.SetDrawColor(255,255,255,alpha) surface.DrawRect(0,0,ScrW(),ScrH()) end) ---- local Timestamp = os.time() local TimeStr = os.date( "%Y-%m-%d-%X" , Timestamp ) http.Post (PHPURL..PATHTOPHP, {f=data, n=TimeStr, t=code},function( body, len, headers, code) print(body) end, function() print(error) end) end function viewer() gui.EnableScreenClicker(true) local frame = vgui.Create("DFrame") frame:SetTitle("Capper Viewer") frame:SetPos(64,64) frame:SetSize(ScrW()-128,ScrH()-128) frame.OnClose = function() gui.EnableScreenClicker(false) end local framehtml = vgui.Create("HTML",frame) framehtml:Dock(FILL) framehtml:SetMouseInputEnabled(true) framehtml:SetPaintedManually(false) framehtml:OpenURL(PHPURL) end concommand.Add("capper_viewer",viewer) concommand.Add("capper_capture",capture) concommand.Add("capper_capture10sec",function() LocalPlayer():PrintMessage(HUD_PRINTTALK,"Capturing image in 10 sec") timer.Simple(10,capture) end) [/CODE] [B]PHP[/B]: Put it into the folder, where the photo gallery software is looking for photos. name it savescript.php [CODE] <?php $code = "mysupersecretpassword"; // change pls if(isset($_POST["f"]) and isset($_POST["n"]) and isset($_POST["t"])){ $f = $_POST["f"]; $n = $_POST["n"]; $t = $_POST["t"]; if($t == $code) { file_put_contents($n.'.jpg', base64_decode($_POST['f'])); echo "successfully saved image"; }else{ echo "wrong code nigga"; } }else{ echo "not all POST parameters set"; echo $_POST(["n"]); } ?>[/CODE] example: [url]http://gamerpaddy.bplaced.net/capper/index.php[/url] [video=youtube;fjPbI352MYA]http://www.youtube.com/watch?v=fjPbI352MYA[/video]
[QUOTE=gamerpaddy;46700661] -long post [/QUOTE] It wouldn't be hard to just take a look at your clientside code and spam the shit out of your site. You'd be better off querying a key to a database and sending it to the client for verification instead.
i dont have time to do that, make it yourself if this is a problem for you. this method is still better than no protection. Ok, server owners could take a look into my cl files to get the code. But who plays on servers with assholes as admins/owners? But you as a "Player", cant do that. So you have no chance to upload your screenshot to my Webserver. edit: you idiots, i was trying to give you a code to upload screenshots to a website. if you find some bugs, FIX IT YOURSELF im not your nigger [highlight](User was banned for this post ("Rude" - NiandraLades))[/highlight]
Just saying you could decompile the clientside files and grab the "code" very easily. Then spam the web-server. :v:
[QUOTE=TheEmp;46701708]Just saying you could decompile the clientside files and grab the "code" very easily. Then spam the web-server. :v:[/QUOTE] Want me to do it right now?
Sorry, you need to Log In to post a reply to this thread.