Global Entity

Hello,
I’m creating a “Phone Booth” for RP purposes,
It will be a Global Entity (Anyone can use) that will spawn on map start.
I have a Frame appear (DFrame) when I press “Use” on the Phone Booth. That is all and well.
I try adding a “DTextEntry” or a “DLabel” and then my script dies.
It will shutdown the Frame and spam me with the following error;



vgui.Create: Error when calling 'DLabel':Init (vgui/DLabel.lua:47: attempt to call field 'ApplySchemeSettings' (a nil value))
vgui.Create: Error when calling 'DFrame':Init (includes/extensions/vgui_sciptedpanels.lua:134: attempt to index local 'panel' (a nil value))
entities/phone_booth/cl_init.lua:23: attempt to index upvalue 'DFrame' (a nil value)
skins/default.lua:157: attempt to index field 'lblTitle' (a nil value)
vgui.Create: Error when calling 'DLabel':Init (vgui/DLabel.lua:47: attempt to call field 'ApplySchemeSettings' (a nil value))
vgui.Create: Error when calling 'DFrame':Init (includes/extensions/vgui_sciptedpanels.lua:134: attempt to index local 'panel' (a nil value))
entities/phone_booth/cl_init.lua:23: attempt to index upvalue 'DFrame' (a nil value)
skins/default.lua:157: attempt to index field 'lblTitle' (a nil value)
vgui.Create: Error when calling 'DLabel':Init (vgui/DLabel.lua:47: attempt to call field 'ApplySchemeSettings' (a nil value))
vgui.Create: Error when calling 'DFrame':Init (includes/extensions/vgui_sciptedpanels.lua:134: attempt to index local 'panel' (a nil value))
entities/phone_booth/cl_init.lua:23: attempt to index upvalue 'DFrame' (a nil value)
skins/default.lua:157: attempt to index field 'lblTitle' (a nil value)
vgui.Create: Error when calling 'DLabel':Init (vgui/DLabel.lua:47: attempt to call field 'ApplySchemeSettings' (a nil value))
vgui.Create: Error when calling 'DFrame':Init (includes/extensions/vgui_sciptedpanels.lua:134: attempt to index local 'panel' (a nil value))
entities/phone_booth/cl_init.lua:23: attempt to index upvalue 'DFrame' (a nil value)
skins/default.lua:157: attempt to index field 'lblTitle' (a nil value)


This is my first entity from scratch, I am hoping to gain experience doing it myself. It’s just this one error I can not wrap my mind around.

Following is cl_init.lua
[lua]
include(“shared.lua”)

function ENT:Initialize()
end

function ENT:Draw()
self.Entity:DrawModel()
end

function ENT:Think()
end

local DFrame
local LastToggleTime = 0
function PhoneBoothMenu()

if DFrame and DFrame:IsValid() and LastToggleTime < CurTime() then
    DFrame:SetVisible( not DFrame:IsVisible())
    LastToggleTime = CurTime() + 2
elseif not DFrame or not DFrame:IsValid() then

    DFrame = vgui.Create("DFrame")
    DFrame:SetSize(250, 150)
    DFrame:Center()
    DFrame:SetTitle("Phone Menu")
    DFrame:SetDraggable(true)
    DFrame:ShowCloseButton(true)
    DFrame:SetBackgroundBlur(true)
    DFrame:MakePopup()
    
    LastToggleTime = CurTime() + 2
end

end
usermessage.Hook(“vgui_PhoneBoothMenu”, PhoneBoothMenu)
[/lua]

This is without the DTextEntry, I am just hoping I can be shown a way to use DTextEntry without it killing the Frame.

Thank you in advance.

What’s the “local DFrame” supposed to do?

Declare a local variable.

[editline]05:26PM[/editline]

Out of any function scope so it persists after the function ends.

isent he supposed to add something after it?

No, it’s fine.

Okay, but now I’m confused.

In his code:


if DFrame and DFrame:IsValid()

How does it know what DFrame is?

[editline]06:29PM[/editline]

If you care to explain :stuck_out_tongue:

It knows what it is because it is defined before that, out of any function so it exists until that function ends ( I think).

Scope. Anything inside of that specific script below the declaration of DFrame can access that specific upvalue. Anything outside shouldn’t be able to read that value.

[lua]
local x

function printx( )
print( x )
end

function setx( n )
x = n
end

printx( )
setx( “oh” )
printx( )
[/lua]



nil
oh


I didnt really understand any of that :stuck_out_tongue:

DFrame will be nil until the first function call to PhoneBoothMenu, where it is set to a vgui panel. every other time after this when PhoneBoothMenu is called DFrame will be a valid panel.

Sorry to of wasted any of your time.
After much debugging, I restarted GMOD and voila… fixed.
Perhaps it was due to a past error that didn’t exit in lua_reloadents.

Thank you.
Might have another error to come back with soon, working on setting up a voice function between 2 clients.

Look at the code in DarkRP.

The /phone thingi.