Custom addon question

Hello people of facepunch,

So I’ve got an addon for my deathrun server (Which modifies the core files) which I don’t really like, I want to be able to make it into a addon, but at the same time… I wanna make it easier to configure, to have a settings.lua file to do this, would the directories look something like this…? If i’m wrong please correct me, would be greatly appreciated… and what would I include() or AddCSLuaFile()?


addons/CustomUI/lua/autorun/client/UI.lua
addons/CustomUI/lua/autorun/settings.lua

You gotta name your files something unique so they don’t get overridden by other addons, but the structure is correct.

Alright, what would I be using, include or AddCSLuaFile? And in which file? settings.lua?

Normally, both. If those are the only two files you are going to use, you don’t need to use them though.

Yeah, I figured it out, i’m having one problem I can’t seem to figure out…
I have this code, when i’m alive it gives me a nil error, pointing to line 121 (Obviously because i’m alive and it’s trying to get the observertarget value while i’m alive.)
The thing is, I can’t seem to figure out what to add to check, I tried



if not IsValid( ply ) then return end


But that doesn’t seem to have fixed anything… this is what it looks like atm…



	if SpectatorUI == true then
	local client = LocalPlayer()
	local ply = LocalPlayer():GetObserverTarget()
	if not IsValid( ply ) then return end
	if LocalPlayer():GetObserverMode() == OBS_MODE_CHASE then
		draw.RoundedBox(0, ScrW() / 2 - 230, ScrH() - 95, 460, 90, Color(0, 0, 0, 185)) -- Draw the grey bar behind the orange one.
	
		local color = ply:Team() == TEAM_RUNNER and Color( 52, 152, 219 ) or Color( 231, 76, 60 )
		draw.RoundedBox(0, ScrW() / 2 - 232, ScrH() - 120, 464, 45, color )
		draw.SimpleText( ply:Nick(), "Deathrun_SmoothBig", ScrW() / 2, ScrH() - 115, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
		draw.SimpleText( "Health: ", "Deathrun_SmoothBig", ScrW() / 2 - 173, ScrH() - 69, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER )
		draw.SimpleText( "Rank: ", "Deathrun_SmoothBig", ScrW() / 2 - 183, ScrH() - 40, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER ) 
		draw.SimpleText( "Team: ", "Deathrun_SmoothBig", ScrW() / 2 + 60, ScrH() - 69, Color( 255, 255, 255), TEXT_ALIGN_CENTER )
		draw.SimpleText( "Ping: ", "Deathrun_SmoothBig", ScrW() / 2 + 55, ScrH() - 40, Color( 255, 255, 255), TEXT_ALIGN_CENTER )

		draw.SimpleText( ply:Health() .. "%", "Deathrun_SmoothBig", ScrW() / 2 - 65, ScrH() - 69, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER ) 
		draw.SimpleText( ply:GetUserGroup():gsub("^%l", string.upper), "Deathrun_SmoothBig", ScrW() / 2 - 75, ScrH() - 40, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER ) 
		draw.SimpleText( team.GetName ( ply:Team() ), "Deathrun_SmoothBig", ScrW() / 2 + 110, ScrH() - 69, Color( 255, 255, 255 ) )
		draw.SimpleText( ply:Ping(), "Deathrun_SmoothBig", ScrW() / 2 + 110, ScrH() - 40, Color( 255, 255, 255 ) )
	end
	else
		draw.AAText( LocalPlayer():GetObserverTarget():Nick(), "Deathrun_SmoothBig", ScrW()/2, 5, Color(255,255,255,255), TEXT_ALIGN_CENTER)
	end


What is the exact error and exact line the error happens on?



[ERROR] addons/customui/lua/autorun/client/customui.lua:121: attempt to index a nil value
  1. fn - addons/customui/lua/autorun/client/customui.lua:121
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183




->  draw.AAText( LocalPlayer():GetObserverTarget():Nick(), "Deathrun_SmoothBig", ScrW()/2, 5, Color(255,255,255,255), TEXT_ALIGN_CENTER)


Had it as ply:Nick(), thought if I changed it to this it would change then realised what the error actually is.

draw.AAText isn’t a valid function?

draw.AAText is fine, it’s a function within the deathrun gamemode, the error is only showing when i’m alive, therefore trying to find something that isn’t there when I’m alive, which goes back to why I attempted to use



if not IsValid( ply )


You aren’t adding that check for that function call.

Here’s properly indented code:



if SpectatorUI == true then
	local client = LocalPlayer()
	local ply = LocalPlayer():GetObserverTarget()
	if not IsValid( ply ) then return end
	if LocalPlayer():GetObserverMode() == OBS_MODE_CHASE then
		draw.RoundedBox(0, ScrW() / 2 - 230, ScrH() - 95, 460, 90, Color(0, 0, 0, 185)) -- Draw the grey bar behind the orange one.
	
		local color = ply:Team() == TEAM_RUNNER and Color( 52, 152, 219 ) or Color( 231, 76, 60 )
		draw.RoundedBox(0, ScrW() / 2 - 232, ScrH() - 120, 464, 45, color )
		draw.SimpleText( ply:Nick(), "Deathrun_SmoothBig", ScrW() / 2, ScrH() - 115, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
		draw.SimpleText( "Health: ", "Deathrun_SmoothBig", ScrW() / 2 - 173, ScrH() - 69, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER )
		draw.SimpleText( "Rank: ", "Deathrun_SmoothBig", ScrW() / 2 - 183, ScrH() - 40, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER ) 
		draw.SimpleText( "Team: ", "Deathrun_SmoothBig", ScrW() / 2 + 60, ScrH() - 69, Color( 255, 255, 255), TEXT_ALIGN_CENTER )
		draw.SimpleText( "Ping: ", "Deathrun_SmoothBig", ScrW() / 2 + 55, ScrH() - 40, Color( 255, 255, 255), TEXT_ALIGN_CENTER )

		draw.SimpleText( ply:Health() .. "%", "Deathrun_SmoothBig", ScrW() / 2 - 65, ScrH() - 69, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER ) 
		draw.SimpleText( ply:GetUserGroup():gsub("^%l", string.upper), "Deathrun_SmoothBig", ScrW() / 2 - 75, ScrH() - 40, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER ) 
		draw.SimpleText( team.GetName ( ply:Team() ), "Deathrun_SmoothBig", ScrW() / 2 + 110, ScrH() - 69, Color( 255, 255, 255 ) )
		draw.SimpleText( ply:Ping(), "Deathrun_SmoothBig", ScrW() / 2 + 110, ScrH() - 40, Color( 255, 255, 255 ) )
	end
else
	draw.AAText( LocalPlayer():GetObserverTarget():Nick(), "Deathrun_SmoothBig", ScrW()/2, 5, Color(255,255,255,255), TEXT_ALIGN_CENTER)
end

None of your checks actually affect the draw.AAText function call.

I’ll give that a try later, the one main thing i’m trying to figure out, is pretty much I want to disable the default deathrun hud when the hud is enabled, and when it’s disabled, enable it, but I can’t figure out how to do it without modifying the core deathrun files, which defeats the purpose of what i’m attempting to do here, do you have any tips that can point me in the right direction to doing this? thanks.

Unless deathrun has proper API for it, which I am sure it doesn’t, you can’t do that without modifying core files.

So I just have to take the part out of the core file and modify it in the part of the UI i’m working on?

[editline]20th June 2014[/editline]

If I were to do something like this… inside the settings file


MaxRounds = 15

Would this be incorrect here?


draw.SimpleText( "of ..MaxRounds..", "default", ScrW() / 2 + 30, 29, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER )

If so, what would I change it to…? Thanks.

bump…

Don’t bump within 10 hours … if you are impatient hier someone at: http://coderhire.com/

If you’re alive the script will still be called … check it with what Robotboy said:


if LocalPlayer():GetObserverMode() == OBS_MODE_CHASE then

and if you want to be sure throw a !client:Alive() in.

MaxRounds will only work if settings file is sent and ran on the client. If not: http://wiki.garrysmod.com/page/Net_Library_Usage

If you want to show the data properly:


"of " ..MaxRounds

My bad, I thought it was longer then 10 hours, was tired af, and went to sleep… I figured the stuff out, just one small problem (draw.RoundedBox is making the polygon I have drawn invisible) Ideas?