updating a DLabel

Ohai
So for my first lua script I’m trying to make a basic derma menu.

Everything in init.lua are working as they should and it is receiving the usermessage + printing it to console fine in cl_init.lua.

But the problems arises when trying to update my my DLabel…

it updates the text fine with


	local infoLabel = vgui.Create( "DLabel", BackGround )
	infoLabel:SetPos( 10, 110 )
	infoLabel:SetText( "Finding stats..." )
	
	timer.Create( "refreshLabel", 0.5, 0, function()
		local labelText = "XP: "..xp.."/"..xpMax..".
Level: "..level..".
Shop Points: "..shopPoints.."."
		infoLabel:SetText( labelText )
		infoLabel:SizeToContents()
	end)

but when i close my BackGround dframe it spams my console with:

Timer Error: lachiebox/gamemode/cl_init.lua:73: Tried to use invalid object (type Panel) (Object was NULL or not of the right type)

I assume this is because its trying to update something that doesn’t exist :stuck_out_tongue:

so i’ve changed it to this:


	local infoLabel = vgui.Create( "DLabel", BackGround )
	infoLabel:SetPos( 10, 110 )
	infoLabel:SetText( "Finding stats..." )
	
	if BackGround:IsActive() then
		timer.Create( "refreshLabel", 0.5, 0, function()
			local labelText = "XP: "..xp.."/"..xpMax..".
Level: "..level..".
Shop Points: "..shopPoints.."."
			infoLabel:SetText( labelText )
			infoLabel:SizeToContents()
		end)
	else timer.Stop( "refreshLabel" ) end

but the timer…doesnt ever seem to start :stuck_out_tongue:

anyone have any idea’s?

(I will eventually make it update with the usermessage hook…but that’s for once i get the IsActive() thing happening…)

When you close a DFrame, it gets removed along with its children. You can disable this by calling DFrame.SetDeleteOnClose.

ah so it gets “closed” but the frame still exists? im off to try this out now :3

edit

ok so its working now… I re-read my post and realised my mistake… the if statement was never actually getting called… :stuck_out_tongue: 2 hours of staring at code and its solved like that ><

but i will ask… is there a better (more efficient) way of sending multiple variables in a usermessage than this:


function sendInfo( ply )

	local messageString = string.Implode(" ", {xp, xpMax, level, shopPoints})
	SendUserMessage( "playerInfo", ply, messageString )
	
end

and on the receiving end :


function getInfo( message )

	local infoTable = string.Explode( " ", message:ReadString() )
	
	xp = infoTable[1]
	xpMax = infoTable[2]
	level = infoTable[3]
	shopPoints = infoTable[4]

	if BackGround and BackGround:IsActive() then
		labelText = "XP: "..xp.."/"..xpMax..".
Level: "..level..".
Shop Points: "..shopPoints.."."
		infoLabel:SetText( labelText )
		infoLabel:SizeToContents()
	end
	
end
usermessage.Hook( "playerInfo", getInfo )

thanks :3

You don’t need to pack them into a string. Send them as numbers and read them using ReadLong.

but how do i send multiple in the one usermessage? or is that not possible

[lua]umsg.Start(“playerinfo”, ply)
umsg.Short(1)
umsg.Short(2)
umsg.String(“hi”)
umsg.End()[/lua]