My Derma won't work :(

Ok, I’m just trying to create a simple derma panel and then play with it. However when I type the console command I get this error
“autorun/dermatest.lua:25: attempt to index global ‘frame’ (a nil value)”

[lua] function frame_create() --Note that I changed the function name. You can change the fucntion name to anything you want
frame = vgui.Create(“DFrame”)
frame:SetPos(50,50)
frame:SetSize(500,350)
frame:SetTitle(“Testing Derma Controls”)
frame:SetVisible(false) --This is important to set false or your frame will pop-up every time you join to server or a single player game.
frame:SetDraggable(true)
frame:ShowCloseButton(true)
frame:SetBackgroundBlur(true)
frame:MakePopup()

button = vgui.Create("DButton",frame) --Note that I have changed the variables to button and frame. This is important to you.
button:SetText("Suicide")
button:SetPos(5,27)
button:SetSize(150,50)
button.DoClick = function()
	error("Derma Is Da Best! (Left Click)")
end
button.DoRightClick = function()
	error("Woohoo! (Right Click)")
end

end

function frame_open()
frame:SetVisible(true)
end

function frame_close()
frame:SetVisible(false)
end

concommand.Add("-our_concommand",frame_close)
concommand.Add("+our_concommand",frame_open)
hook.Add(“Initialize”,“frame_create”,frame_create)
[/lua]

Another one of those coding concepts you really ought to know before starting with Lua is scope. You declared the “frame” variable within the scope of the frame_create function. That means you CANNOT access it outside of that function, unless you returned it or assigned it to a global variable.

I’ll assume the derma library hadn’t been loaded when the initialise hook was called.

[lua]
local frame = vgui.Create(“DFrame”)
frame:SetPos(50,50)
frame:SetSize(500,350)
frame:SetTitle(“Testing Derma Controls”)
frame:SetVisible(false) --This is important to set false or your frame will pop-up every time you join to server or a single player game.
frame:SetDraggable(true)
frame:ShowCloseButton(true)
frame:SetBackgroundBlur(true)
frame:MakePopup()

local button = vgui.Create(“DButton”, frame ) --Note that I have changed the variables to button and frame. This is important to you.
button:SetText(“Suicide”)
button:SetPos(5,27)
button:SetSize(150,50)
button.DoClick = function()
error(“Derma Is Da Best! (Left Click)”)
end
button.DoRightClick = function()
error(“Woohoo! (Right Click)”)
end

function frame_open()
frame:SetVisible(true)
end
concommand.Add("+our_concommand",frame_open)
function frame_close()
frame:SetVisible(false)
end
concommand.Add("-our_concommand",frame_close)[/lua]

[editline]11:50PM[/editline]

It is a global variable.

I thought insofar as it was defined in the function it, regardless of the whether the “local” modifier was applied, it couldn’t be accessed by other functions.


23:54:15 > function abc() avar = true end abc() print(avar)...
         true

The scope of a global variable is the Lua environment it was defined in - game or menu. The scope of a local variable, is as you say, held to the file or function it was defined in.

Wait, so variables are only defined within functions? I thought that it was within the file.

Global variables are only restricted to a Lua environment. Local variables are restricted to the scope they are defined in - whether that be a function or file.

You can define a variable anywhere, but if its a local variable, where you define it is important.