Thread to post your various observations in developing and playing Garry's Mod that others might find interesting.
TIL you can fix map leak tearing by filling the pixel buffer with black before every rendering.
hook.Add("PreRender", "MapTearFix", function()
cam.Start2D()
surface.SetDrawColor(0, 0, 0, 255)
surface.DrawRect(0, 0, ScrW(), ScrH())
cam.End2D()
end)
Before:
After:
It might sound obvious to others but I learned that you can use http.Fetch to download an audio file to the clients data folder and use sound.PlayFile to play it to the client whenever you want.
http.Fetch('http://www.site.com/audiofile.mp3/', function(data)
file.Write('audiofile.txt', data)
end)
sound.PlayFile( 'data/audiofile.txt', '', function( station )
if ( IsValid( station ) ) then station:Play() end
end )
Does this use more data on the user though because it's forcing them to download additional sound files versus it being streamed like that? I would assume not since it's already streamed, but I figured I'd clarify.
I'm still very new to Lua and have been trying my best to experiment. A couple days ago I wanted to make clients get materials faster, so to speak, and update materials live for all players. I was really inspired by it because a lot of users don't like waiting in the loading screen, and I thought why not download it while they're in the server and able to walk around? That idea didn't really work out since I couldn't figure out why it kept becoming a missing texture after you rejoin.
I don't believe it would use more data compared to downloading it from the workshop. You could make them download it as soon as they load into the server and usually it doesn't take long to download multiple files with the method I posted. They'd only have to download it once, like I believe how cache/workshop works when you join a server unless the files are deleted. It was just something interesting I found while working on something else and I figured I'd post it on this thread.
Yeah. I do this along with a few other 'hacky' things to create create a thick fog on any map, no matter the skybox (this is for one of my boss fight rounds). if you match the fog color with the clear colors, and change the z clipping plane in CalcView, it makes it more optimized, and creates that nice thick fog effect, on anymap.
I updated the wiki ages ago when it said PreRender was not working as intended, with pretty much the exact same snippet, but it seems you (or someone) overwrote it now. No biggie though.
I also use this along side some other stuff to 'disable skyboxes' to slightly improve performance on some maps (it's an option for my players).
As for downloading sounds from a webserver and saving as a .txt.
https://github.com/Brassx/Brass-Sound-System/blob/master/br_soundsystem/lua/br_soundsystem/client/cl_br_sndsystem.lua
BR_SOUND_SYSTEM:DownloadSound(url) --Downloads a sound and caches it in data folder.
BR_SOUND_SYSTEM:PlaySound(url, flags, func) --Works exactly the same as http://wiki.garrysmod.com/page/sound/PlayURL, but will download the sound from a URL if it doesn't exist and cache it, otherwise it plays from the .txt cache file. (Which is named as a crc of the URL)
I wrote a handy small system few months back to do exactly that. It generates a cache folder in their data folder, stores sounds in there. It looks for a sound in that cache folder, plays it, otherwise downloads it from the URL provided, then plays it. Nothing complicated, just a nice little system. Feel free to use it
if needed!
Like I said it's nothing complicated, just something that takes care of stuff for you and lets you use it the exact same way as PlayFile or PlayURL without worrying.
I do this for a lot of things on my server. But that's because Material() is relative to the Materials folder. Since that's the case, you can't use it for files in the data folder (as far as I'm aware).
The solution is to load the image into an HTML frame from your cached file in Data, and get the HTML material.
There's a few ways you can do this. You can either A: Convert the image to a base64, save it as a .txt file in data, then load it as a base64 image into your HTML panel , or B, use "asset://garrysmod/data/yoursavedpng.png" for an image in the panel.
Streaming will send data over time instead of having to wait for the download to complete, but in the end (ignoring differences in compression and structure), the same amount of data will be sent.
CreateMaterial("data_material", "VertexLitGeneric", {
["$basetexture"] = Material("data/willy.png"):GetName(),
["$model"] = 1,
})
for k, v in pairs(ents.GetAll()) do
v:SetMaterial("!data_material")
v:SetSubMaterial( 0, "!data_material")
end
You can use this to apply a png image in the data folder to an entity. However, it will stop working after you rejoin a second time and you'd have to restart your game for it to work again. I'm posting it here because maybe it will help someone in the future. Thank you for your reply!
That's very interesting. According to the Wiki, it's relative to the Materials folder, which implies anything outside of said folder should not be able to be reached.
Pretty interesting that you can indeed reach files in your data folder.
However, with my system(using HTML panels, I should note it also supports images that aren't a power of 2) I have never had the issue you are having. Very weird!
Perhaps try http://wiki.garrysmod.com/page/ITexture/Download
on the basetexture of the material right before applying it?
Like so:
local m = CreateMaterial("data_material", "VertexLitGeneric", {
["$basetexture"] = Material("data/willy.png"):GetName(),
["$model"] = 1,
})
m:GetTexture("$basetexture"):Download();
No idea if that would actually work, but now I'm curious as to why that's an issue.
If you accidentally deleted a Lua file that you ran on your client before, it will most likely be in your ~/cache/lua/ folder. You can decrpyt files with this.
Thanks oub for saving my ass <3
Sadly, that doesn't fix it. When you run the code you provided it will work fine until you rejoin and be presented with -
[ERROR] lua/brusx.lua:6: attempt to index a nil value
1. unknown - lua/brusx.lua:6
Yeah, it won't work since Material is relative to materials/. You'll have to use a DHTML to read materials from the data folder (core example here).
I know this isn't the proper thread and I don't believe I can private message you, but why does it work the first time you load it? And thank you for the example!
I still get the error even while loading in; tested on local multiplayer on dev branch.
Really ? It would've save me a fcking week !
I'm not on the dev branch but am testing on local multiplayer. I don't want to break the rules and clutter this thread so I'll start doing alternative methods. It's just weird to be because it works the first time and doesn't. Thank you!
You can pass arguments to files and raw code using CompileFile's or CompileString's generating functions. Ex.
local func = CompileFile("foo.lua")
func("foo", "bar")
-- In foo.lua:
local tArgs = {...}
print(tArgs[1] .. tArgs[2]) -- Outputs "foobar"
local func = CompileString("print(...)")
func("foo", "bar") -- Outputs "foobar"
Sorry, you need to Log In to post a reply to this thread.