How do I network one entities vars to anothers for VGUI?
7 replies, posted
Heya, I've been having a bit of a hard time figuring out how to network vars from one entities to anothers cl_init.lua file for VGUI stuff.
function ENT:OnTakeDamage( dmginfo )
local dmg = dmginfo:GetDamage()
local wep = dmginfo:GetInflictor()
local attacker = dmginfo:GetAttacker()
local damagetype = dmginfo:GetDamageType()
if debugvalue == true then
print(wep)
print(dmg)
print(attacker)
print(damagetype)
end
if attacker:IsPlayer() && damagetype == 128 then
StoneMineral = StoneMineral + 20
SulfurMineral = SulfurMineral + 5
MetalOre = MetalOre + 10
ResourceGather()
else
print("You have to use a melee weapon")
end
end
The vars, specifically StoneMineral, SulfurMineral and MetalOre.
I want to network it to a cl_init.lua file for VGUI which is:
net.Receive( "WorkbenchMenu", function()
local Frame = vgui.Create( "DFrame" )
Frame:SetTitle( "Workbench" )
Frame:SetSize( 600, 600 )
Frame:Center()
Frame:MakePopup()
Frame.Paint = function( self, w, h )
draw.RoundedBox( 0, 0, 0, w, h, Color( 47, 45, 46, 255 ) )
end
local SulfurCount = vgui.Create( "DImage", Frame )
SulfurCount:SetPos( 20, 20 )
SulfurCount:SetSize( 100, 100 )
SulfurCount:SetImage("vgui/hud/9mmpistol_legacy")
local SulfurCounter = vgui.Create("DLabel", Frame)
SulfurCounter:SetPos( 50, 75 )
SulfurCounter:SetSize( 100, 100 )
SulfurCounter:SetText('Sulfur: ' ..SulfurMineral..'')
end )
I want to network it to the
SulfurCounter:SetText('Sulfur: ' ..SulfurMineral..'')
part so that I could see how much of that element I have.
Any idea on how I should do this? Any help is appreciated. Thank you!
https://i.imgur.com/yWvio1J.png
Something like this but of course instead of nil it would be a number for those resources.
You could send the amount of minerals whenever you start your networking with net.WriteInt
function ResourceGather() //Do math random here later
util.AddNetworkString( "ResourceNetworking" )
StoneMineral = StoneMineral + 20
SulfurMineral = SulfurMineral + 5
MetalOre = MetalOre + 10
net.Start( "ResourceNetworking" )
net.WriteInt(MetalOre:GetValue(), 8)
net.Send()
net.Broadcast()
print ('You have '..MetalOre.. ' metal ores, '..StoneMineral..' stones and '..SulfurMineral..' sulfur ores!')
end
Moved this bit to another function but it's still the same. So i'm assuming i'd have to use it something like this? I'm sorry if this is completely wrong im still a complete dumbass with the net. library so.
First of all, you should add your network string outside any functions.
It is also better to start networking when you want to open the vgui.
Right now, whenever your function runs it will start running your receiving end on the client.
https://wiki.garrysmod.com/page/net/Send requires you to specify a player that you want to network this information to.
MetalOre:GetValue() seems unnecessary from what I am reading here, doesnt MetalOre already contain the value in it self?
net.Broadcast will network to every player on your server so you probably dont want to do that when you intend to send it to one player.
You should probably check this out too https://wiki.garrysmod.com/page/Net_Library_Usage
Ye, I've read the net. library page before, some of the stuff still confuses me but i'm getting there with some trial and error.
Right, so I fixed up some of my stuff(Probably):
util.AddNetworkString( "ResourceNetworking" )
function ResourceGather() //Do math random here later
StoneMineral = StoneMineral + 20
SulfurMineral = SulfurMineral + 5
MetalOre = MetalOre + 10
net.Start( "ResourceNetworking" )
net.WriteEntity()
net.Broadcast() //Gonna use Broadcast() for now, will switch to Send() later
print ('You have '..MetalOre.. ' metal ores, '..StoneMineral..' stones and '..SulfurMineral..' sulfur ores!')
end
I'm assuming I gotta do something like this? Now let's say I want this to be inserted here:
local MetalCounter = vgui.Create("DLabel", Frame)
MetalCounter:SetPos( 150, 75 )
MetalCounter:SetSize( 100, 100 )
MetalCounter:SetText('Metal: ' ..MetalOre..'!') //<--- This part of the code is where I want to display the number.
How should I do it?
I don't understand why you suddenly use net.WriteEntity() now, without actually sending anything as well.
I believe you want to send a whole number, so use net.WriteInt() instead.
Inside your net.Receive() function, which must be on the client side where you have your vgui and such, want to use net.ReadInt to read the number that was sent from the server.
local MetalOreCount = net.ReadInt()
could look like this
Holy shit thank you! This finally works.
Sorry, you need to Log In to post a reply to this thread.