Can't use networked integer "self"

Run into a problem, I’ve got an entity set up and in the function ENT:Initialize() I have a self:SetNWInt(“count”, 25).

When I call this in the client side in my derma menu, it says “attempt to index global ‘self’ (a nil value)”

[lua]AddCSLuaFile();

ENT.Type = “anim”
ENT.PrintName = “Grenade Crate”
ENT.Spawnable = true

util.PrecacheModel(“models/items/item_item_crate.mdl”);

function ENT:Initialize()
if (SERVER) then
self:SetModel(“models/items/item_item_crate.mdl”)
self:SetSolid(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
self:SetNWInt(“Funds”, 200)
self:SetColor(Color(255, 0, 0, 255))
self:SetMaterial(“models/wireframe”)
self:SetNWInt(“grenade_count”, 25)
end
end

if (SERVER) then
function ENT:Use(activator, caller)
if ( (self.nextUse or 0) < CurTime() ) then
if (self:GetNWInt(“Funds”) == 0) then
umsg.Start(“CrateGUI”)
umsg.End()
else
if (activator:CanAfford(50)) then
if (self:GetNWInt(“Funds”) - 50 > 0) then
activator:TakeMoney(50)
self:SetNWInt(“Funds”, self:GetNWInt(“Funds”) - 50)
activator:Notify(“You’ve added $50 to the crate!”)
elseif (self:GetNWInt(“Funds”) - 50 == 0) then
activator:Notify(“Funding complete!”)
activator:TakeMoney(50)
self:SetColor(Color(0, 255, 0, 255))
self:SetMaterial("")
self:SetNWInt(“Funds”, self:GetNWInt(“Funds”) - 50)
end
else
activator:Notify(“Insufficient funds!”)
end
end
self.nextUse = CurTime() + 1
end
end
end

if (CLIENT) then
function ENT:Draw()
self:DrawModel()
cam.Start3D2D( self:LocalToWorld( self:OBBCenter() ) + Vector( 0, 0, 20 ), Angle( 0, ( self:GetPos() - EyePos() ):Angle().y - 90, 90 ), 0.3 )
if self:GetNWInt(“Funds”) > 0 then
draw.DrawText( “$”…self:GetNWInt(“Funds”)…".00", “DermaDefaultBold”, 0, 0, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
else
draw.DrawText( “Grenade Crate”, “DermaDefaultBold”, 0, 0, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
end
cam.End3D2D()
end

function CrateGUI()
	local frame = vgui.Create("DFrame")
	frame:SetPos(ScrW()/2-200, ScrH()/2-150)
	frame:SetSize(400, 274)
	frame:SetTitle("Grenade Crate")
	frame:MakePopup()
	frame:ShowCloseButton(true)
	
	local frame_pnl_scroll = vgui.Create("DScrollPanel", frame)
	frame_pnl_scroll:SetSize(380, 239)
	frame_pnl_scroll:SetPos(10, 30)
	
	local frame_list = vgui.Create("DIconLayout", frame_pnl_scroll)
	frame_list:SetSize(360, 229)
	frame_list:SetPos(0, 0)
	frame_list:SetSpaceY(5)
	frame_list:SetSpaceX(5)
	
	for i = 1, self:GetNWInt("grenade_count") do
		local btn_grenade = frame_list:Add("SpawnIcon")
		btn_grenade:SetModel("models/weapons/w_eq_fraggrenade.mdl")
		btn_grenade.DoClick = function()
			print("Test") 
		end
	end
end
usermessage.Hook("CrateGUI", CrateGUI)

end
[/lua]

I’m stumped and any help is greatly appreciated.

self does not exist here: your function CrateGUI is not a member of your entity. Self refers to the entity the function is called on; you should add the crate entity in your usermessage: umsg.Entity(self). Then in your function CrateGUI you read the entity from the usermessage:



function CrateGUI(data)
    local crate = data:ReadEntity()
    local grenade_count = crate:GetNWInt("grenade_count")
    ...
end



Cheers frozen, I wasn’t aware of this method of doing things at all.

It has fixed any errors, how ever it doesn’t display any SpawnIcons.

  • Fixed, misread where you said to add it to the usermessage also. Appreciate the help!

Back again - Is there a way to update the panel in real time? It seems as though I have to close and then re open the frame before the buttons disappear.
I’ve looked around at :Rebuild() but nothing successful.

There’s no such function as Panel:Rebuild() for DIconLayout.

Could you be more specific as to what you want to happen?

Depending on what you say, I would suggest to just use the panels think hook and a variable attached to the panel to check if it’s been updated, ex: “ent.OldMenuContent != ent.CurMenuContent”.

Would be a good idea to make a global derma panel instead on the initialize function of the entity also which would allow you to access and check things easier.

Sure thing, what I want to happen is have a set amount of grenades to start with once the crate has been purchased. These grenades will then show in a DIconLayout.
Once I’ve clicked on an Icon I want it to then disappear (and eventually I’ll implement the purchase of the grenade).

I’ll see if I can work in some time tomorrow to fix this up for you.

You need to look into “ENT:SetupDataTables()”. A good example is inside the sent_ball.

Hey NintendoEKS, I ended up coming out with this, but i’m not sure if it’ll update live or not because I would need someone to try it with.

[lua]
AddCSLuaFile()

ENT.Type = “anim”
ENT.PrintName = “Grenade Crate”
ENT.Author = “Inferno, Eh?”
ENT.Information = “”
ENT.Category = “Crates”

ENT.Editable = true
ENT.Spawnable = true
ENT.AdminOnly = false
ENT.Model = Model(“models/items/item_item_crate.mdl”)

function ENT:SetupDataTables()
self:NetworkVar(“Float”, 0, “Funding”)
self:NetworkVar(“Int”, 0, “Grenades”)
end

function ENT:Initialize()
if SERVER then
util.AddNetworkString(“GC_Menu”)
self:SetModel(self.Model)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
local phys = self:GetPhysicsObject()
if phys:IsValid() then
phys:Wake()
end
self:SetColor(Color(255,0,0))
self:SetMaterial(“models/wireframe”)
end
self:SetFunding(200)
self:SetGrenades(25)
self.NextUse = CurTime()
self.UseDelay = 1
end

function ENT:Use(activator, caller, type, value)
if IsValid(activator) then
if ((CurTime() - self.NextUse) < self.UseDelay) then return end

	if (self:GetFunding() == 0) then
		net.Start("GC_Menu")
			net.WriteInt(self:EntIndex(), 32)
		net.Send(activator)
	else
		if (activator:CanAfford(1)) then
			local toFund = math.Clamp(activator:GetMoney(), 1, 50)
			if ((self:GetFunding() - toFund) &gt; 0) then
				activator:TakeMoney(toFund)
				activator:Notify("You've funded $50 to the crate!")
				self:SetFunding(self:GetFunding() - toFund)
			else
				activator:TakeMoney(toFund + (self:GetFunding() - toFund))
				activator:Notify("Funding complete!")
				self:SetMaterial("debug/white")
				self:SetColor(Color(255,255,255))
				self:SetFunding(0)
			end
		else
			activator:Notify("Insufficient funds!")
		end
	end
end

end

if CLIENT then
function ENT:Draw()
self:DrawModel()
cam.Start3D2D(self:LocalToWorld(self:OBBCenter()) + Vector(0, 0, 20), Angle(0, (self:GetPos() - EyePos()):Angle().y - 90, 90), 0.3)
if (self:GetFunding() > 0) then
draw.DrawText("$"…self:GetFunding(), “DermaDefaultBold”, 0, 0, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
else
draw.DrawText(self.PrintName, “DermaDefaultBold”, 0, 0, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
end
cam.End3D2D()
end

function ENT:CrateGUI()
	local DMain = vgui.Create("DFrame")
	DMain:SetPos(ScrW()/2-200, ScrH()/2-150)
	DMain:SetSize(400, 274)
	DMain:SetTitle("Grenade Crate")
	DMain:MakePopup()
	DMain:ShowCloseButton(true)
	
	local DMain_Scroll = vgui.Create("DScrollPanel", DMain)
	DMain_Scroll:SetSize(380, 239)
	DMain_Scroll:SetPos(10, 30)
	
	local function CustomListUpdate()
		local PANEL = vgui.Create("DIconLayout", DMain_Scroll)
		PANEL:SetSize(360, 229)
		PANEL:SetPos(0, 0)
		PANEL:SetSpaceY(5)
		PANEL:SetSpaceX(5)
		PANEL.GrenadeCount = self:GetGrenades()
		PANEL.Think = function(pnl)
			if self:GetGrenades() != pnl.GrenadeCount then
				pnl.GrenadeCount = self:GetGrenades()
				PANEL:Remove()
				CustomListUpdate()
			elseif self:GetGrenades() &lt;= 0 then
				DMain:Remove()
			end
		end
		
		for i = 1, self:GetGrenades() do
			local btn_grenade = PANEL:Add("SpawnIcon")
			btn_grenade:SetModel("models/weapons/w_eq_fraggrenade.mdl")
			btn_grenade.DoClick = function()
				self:SetGrenades(self:GetGrenades()-1)
			end
		end

		return PANEL
	end
	
	CustomListUpdate()
end

net.Receive("GC_Menu", function(len)
	local id = net.ReadInt(32)
	local ent = Entity(id)
	local ply = LocalPlayer()
	if !IsValid(ent) or !IsValid(ply) then return end
	if ent:GetGrenades() &lt;= 0 then
		ply:Notify("This crate has no grenades left!")
		return
	end
	
	ent:CrateGUI()
end)

end
[/lua]

Hey Brandon, when I get a chance after work tomorrow I’ll give it a chop.

Thank you in advance, will get back to you!