[TTT] How would I make a lua script (any script) optional via setting in the F1 menu?
15 replies, posted
I have several addons that are single lua scripts in the autorun folder. I'd like to make a few of them optional via checkbox in the F1 settings menu, like some other addons come preconfigured with.
I know the basics behind it, set a convar in the script and make a function surrounding the whole thing that either has the code for the addon, or nothing in it. That i understand.
I don't know how to do that though. Can somebody help me and tell me how to create a convar that would work like that, and how to add it to the F1 menu? I'm kinda clueless, and google didn't really help me
Thanks in advance!!
It's not just put a whole script into an "if then end" construction.
[QUOTE=Robotboy655;43466648]It's not just put a whole script into an "if then end" construction.[/QUOTE]
I know it's not that simple. But i dont know how to do it, that is why i asked for guidance
[editline]8th January 2014[/editline]
Bump, I figured out myself how to do a derma option by looking at some random code I found in an addon.
[CODE] local CheckboxExample = vgui.Create( "DCheckBoxLabel" )
CheckboxExample:SetText("Check this for an option")
CheckboxExample:SetConVar("example_convar")
CheckboxExample:SetValue(1)[/CODE]
Ok, that I understand, but a couple things I DONT understand are
Where the file is for the F1 settings menu in TTT
How to create a convar that enables/disables a script
Any help?
[editline]8th January 2014[/editline]
Another bump, I figured out how to add it to the F1 menu succesfully, and I created a convar which should enable it. Last step is to make it so this convar actually toggles the script. Any help?
i am trying to do something like
[CODE]
local gta_dmsg_enabled = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
local run_dmsg
run_dmsg = function()
--checks if it's enabled
if not gta_dmsg_enabled:GetBool() then
return
end
--rest of the code
end
--here is where i add it to the menu
[/CODE]
would that work?
[editline]8th January 2014[/editline]
Update, that didn't work.
You would add it to the F1 menus by using the [URL="http://ttt.badking.net/guides/hooks"]TTTSettingsTabs[/URL] hook or editing core TTT files.
[QUOTE=The Android1;43468555]You would add it to the F1 menus by using the [URL="http://ttt.badking.net/guides/hooks"]TTTSettingsTabs[/URL] hook or editing core TTT files.[/QUOTE]
Yeah, I figured that much out
[CODE]
return hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
do
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end
end)
[/CODE]
That all works.
I have the cvar, and a button to change the Boolean value of the cvar. But I don't know how to use that boolean value to enable or disable the script from running.
[QUOTE=MONGLER;43468686]Yeah, I figured that much out
[CODE]
return hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
do
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end
end)
[/CODE]
That all works.
I have the cvar, and a button to change the Boolean value of the cvar. But I don't know how to use that boolean value to enable or disable the script from running.[/QUOTE]
You may need to revise your syntax >.>
[QUOTE=crazyscouter;43468699]You may need to revise your syntax >.>[/QUOTE]
That part of it worked. It created a button on the F1 menu, just as it should.
[editline]8th January 2014[/editline]
[QUOTE=MONGLER;43468686]
I have the cvar, and a button to change the Boolean value of the cvar. But I don't know how to use that boolean value to enable or disable the script from running.[/QUOTE]
Bump
[editline]8th January 2014[/editline]
Bump
[editline]8th January 2014[/editline]
Does this make sense? I'm kinda grasping at air here
[CODE]
local gta_dmsg_enabled = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
local dmsg_cvar_value = GetConVarNumber("dmsg_enabled")
if dmsg_cvar_value == 0 then
return
end
[/CODE]
[code]
CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
if GetConVar("dmsg_enabled"):GetBool() then
return
end
[/code]
However if you want to use variables, like you're example is, I suppose you could do something like this.
[code]
local dmsg_convar = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
local dmsg_convar_enabled = dmsg_convar:GetBool()
if dmsg_convar_enabled then
return
end
[/code]
[QUOTE=crazyscouter;43470322][code]
CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
if GetConVar("dmsg_enabled"):GetBool() then
return
end
[/code]
So if I used your second example, and just put it above all the actual death message code where all the variables are being set etc, that would work as the boolean switch I am looking for?
I dont fully understand the logic here which is probably why this is so difficult for me
However if you want to use variables, like you're example is, I suppose you could do something like this.
[code]
local dmsg_convar = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
local dmsg_convar_enabled = dmsg_convar:GetBool()
if dmsg_convar_enabled then
return
end
[/code][/QUOTE]
So if i use your second example, and put it after my convar being set, up above all the actual death message code, this would work as the boolean switch i'm trying to achieve?
i dont fully understand the logic behind this which is probaly why i'm struggling so much with it.
No, because if dmsg_convar_enabled returned true, then it would end, which we don't want. In order to only display the death message when the script is enabled, we'd want to do something like this:
[code]
local dmsg_convar = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
local dmsg_convar_enabled = dmsg_convar:GetBool()
if !dmsg_convar_enabled then
return
end
[/code]
[QUOTE=crazyscouter;43470413]No, because if dmsg_convar_enabled returned true, then it would end, which we don't want. In order to only display the death message when the script is enabled, we'd want to do something like this:
[code]
local dmsg_convar = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
local dmsg_convar_enabled = dmsg_convar:GetBool()
if !dmsg_convar_enabled then
return
end
[/code][/QUOTE]
Ok thank you, I'll try that
[editline]9th January 2014[/editline]
Ok well, now, my code that was successfully adding it to the settings F1 menu YESTERDAY, no longer works. I don't know why. I didn't even touch the file. I even took out the part you just told me to add, and it still doesn't work.
This is very frustrating.
bump i was tryin to figure out how to do this too but whatever i did nothing
This: [code]
return hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
do
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end
end)
[/code]
Is what you had in there, correct? If so, try using this:
[code]
hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end)
[/code]
[QUOTE=crazyscouter;43478257]This: [code]
return hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
do
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end
end)
[/code]
Is what you had in there, correct? If so, try using this:
[code]
hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end)
[/code][/QUOTE]
That is what I had, I looked in another script of mine which was written by a friend, and already had a check box in the F1 menu, I took it from there and replaced the convar to match the one I made for my death message. It was working.. now it's not, but I just added your updated version, and it's still not working.
[editline]9th January 2014[/editline]
Here is the whole thing. yesterday i had it so that there was a working convar, and a working button the F1 menu, but couldn't figure out how to disable the script... now my problems are reversed. Nothing is happening. No F1 button, nothing is happening.
[code]
if SERVER then
resource.AddFile("resource/fonts/pricedown bl.ttf")
resource.AddFile("resource/fonts/raavi_0.ttf")
resource.AddFile("resource/fonts/raavib_0.ttf")
AddCSLuaFile("autorun/dmsg.lua")
hook.Add("PlayerDeath", "DMSG.SV", function(vic, wep, att)
umsg.Start("DMSG.Umsg", vic)
umsg.Entity(att)
if att:IsPlayer() then
umsg.Char(att:GetRole())
else
umsg.Char(-1)
end
umsg.End()
end)
else
CreateClientConVar("ttt_dmsg_seconds", 5, FCVAR_ARCHIVE)
--creates a clientside convar to enable the script
local gta_dmsg_enabled = CreateConVar("dmsg_enabled", "1", FCVAR_ARCHIVE)
--creates a local variable set to the boolean value of the convar, then tests if it is true
local dmsg_convar_enabled = gta_dmsg_enabled:GetBool()
if !dmsg_convar_enabled then
return
end
local index = {
[-1] = {Color(160,160,160), Color(160,160,160), "World"},
[0] = {Color(33, 177, 33), Color(27, 146, 27), "Innocent"},
[1] = {Color(226, 43, 43), Color(178, 34, 34), "Traitor"},
[2] = {Color(9, 101, 203), Color(14, 69, 148), "Detective"}
}
hook.Add("InitPostEntity", "CreateFontsDMSG", function()
surface.CreateFont( "Raavi", {
font = "Raavi",
size = 35.5,
weight = 420,
blursize = 0,
scanlines = 0,
antialias = true
})
surface.CreateFont( "pricedown", {
font = "pricedown bl",
size = 110,
weight = 400,
blursize = 0,
scanlines = 0,
antialias = true
})
local death = Material( 'death.png' )
local HOLDER = {}
function HOLDER:Paint(w, h)
surface.SetDrawColor(0, 0, 0)
surface.SetMaterial( death )
surface.DrawTexturedRect( 0, 0, w, h )
end
vgui.Register("DMSG.Holder", HOLDER, "Panel")
local TXT = {}
function TXT:Init()
self.FirstColor = color_white
self.SecondColor = self.FirstColor
self.Text = ""
self.Font = "Raavi"
end
function TXT:SetFont(font)
self.Font = font
end
function TXT:SetText(txt)
self.Text = txt
surface.SetFont(self.Font)
local _, h = surface.GetTextSize("A")
local w, _ = surface.GetTextSize(txt)
self:SetSize(w, h)
end
function TXT:SetFirstColor(col)
self.FirstColor = col
end
function TXT:SetSecondColor(col)
self.SecondColor = col
end
function TXT:Paint(w, h)
surface.SetFont(self.Font)
surface.SetTextColor(self.SecondColor)
surface.SetTextPos(0, 0)
surface.DrawText(self.Text)
local x, y = self:LocalToScreen(0, 0)
render.SetScissorRect(x, y, x + w, y + h / 2, true)
surface.SetTextColor(self.FirstColor)
surface.SetTextPos(0, 0)
surface.DrawText(self.Text)
render.SetScissorRect(x, y, x + w, y + h, false)
end
vgui.Register("DMSG.Label", TXT, "Panel")
end)
function MakeDMSG(ply, role)
holder = vgui.Create("DMSG.Holder")
holder:SetPos(0 , ((ScrH() / 2)-98))
holder:SetSize(1920, 196)
holder:SetVisible(true)
if ply:IsPlayer() then
local lbl = vgui.Create("DMSG.Label", holder)
lbl:SetPos(((ScrW()/2)-139), 110)
lbl:SetFirstColor(Color(235, 235, 235))
lbl:SetSecondColor(Color(235, 235, 235))
lbl:SetText(ply == LocalPlayer() and "You dumb bastard" or "You were killed by")
lbl:SetVisible(true)
end
if ply:IsPlayer() then
local lbl2 = vgui.Create("DMSG.Label", holder)
lbl2:SetFont("Raavi")
lbl2:SetPos(((ScrW()/2)+15), 110)
lbl2:SetFirstColor(index[role][1])
lbl2:SetSecondColor(index[role][1])
--lbl2:SetText(ply == LocalPlayer() and "Yourself" or (ply:IsValid() and ply:Nick() or ""))
lbl2:SetText(ply == LocalPlayer() and "" or ply:Nick() .. " (" .. index[role][3]:upper() .. ")")
lbl2:SetVisible(true)
else
local lbl2 = vgui.Create("DMSG.Label", holder)
lbl2:SetFont("Raavi")
lbl2:SetPos(((ScrW()/2)-219), 110)
lbl2:SetFirstColor(index[role][1])
lbl2:SetSecondColor(index[role][1])
lbl2:SetText("world damage")
lbl2:SetVisible(true)
end
if ply:IsPlayer() then
local lbl3 = vgui.Create("DMSG.Label", holder)
lbl3:SetFont("pricedown")
lbl3:SetPos(((ScrW()/2)-140), 15)
lbl3:SetFirstColor(index[role][1])
lbl3:SetSecondColor(index[role][1] )
lbl3:SetText(ply == LocalPlayer() and "suicide" or "wasted")
lbl3:SetVisible(true)
else
local lbl3 = vgui.Create("DMSG.Label", holder)
lbl3:SetFont("pricedown")
lbl3:SetPos(((ScrW()/2)-220), 15)
lbl3:SetFirstColor(index[role][1])
lbl3:SetSecondColor(index[role][1] )
lbl3:SetText("life is a bitch")
lbl3:SetVisible(true)
end
if ply:IsPlayer() then
Msg("You were killed by " .. ply:Nick() .. ", he was a " .. index[role][3]:upper() .. ".\n")
else
Msg("You were killed by the world.\n")
end
timer.Simple(GetConVar("ttt_dmsg_seconds"):GetFloat(), function() holder:Remove() end)
end
usermessage.Hook("DMSG.Umsg", function(um)
MakeDMSG(um:ReadEntity(), um:ReadChar()) end)
end
--adds the option to use the script to the F1 menu
hook.Add("TTTSettingsTabs", "GTADmsgSettings", function(dtabs)
local dsettings = dtabs.Items[2].Panel
local dgui = vgui.Create("DForm", dsettings)
dgui:SetName("GTA5 Style Death Message")
dgui:CheckBox("Enable Death Message", "dmsg_enabled")
return dsettings:AddItem(dgui)
end)
[/code]
Sorry, you need to Log In to post a reply to this thread.