Mana System

Hello, so I have been trying to use net messages and I’ve been getting various problems whilst trying to create a sort of mana system
that will set the player’s mana to a number, get the player’s current mana for drawing part of the hud, and taking/giving mana to the
player.

So far I have several different files that contain the code I will post shortly. I have a serverside file sv_playerinit_ext that has the functions that give/take
and get the player’s mana.


function meta:GetMana(num)
	return self.Mana or 0
end

function meta:SetMana(num)
	net.Start("PlayerSetmana")
		net.WriteEntity(self)
		net.WriteInt(num, 32)
	net.Send(self)
	self.Mana = num
end

function meta:AddMana(num)
	self:SetMana(self:GetMana()+num)
end

Then I have anther file that is clientside that draws a box that is red, that basically represents the player’s mana. The below is called cl_initialize.lua.


net.Receive("PlayerSetmana", function(len)
	energy = LocalPlayer():GetMana()
	draw.RoundedBoxEx(4, 40, 40, math.Clamp(energy, 0, 100), 100, Color(255, 0, 0, 255), false, false, false, false)
end)

The error I’m currently getting is this: [attempt to call method GetMana a nil value] the error is located in cl_initialize line 3.

If anyone could give me some pointers as to what I’m doing wrong I would be greatful. I’ve been changing things around for several hours now

and I feel like an idiot for not realizing my mistake by now.

As a last note, I tried using the idea on a money system from here:
http://forum.facepunch.com/showthread.php?t=1315437

to create this, and this may be why I’m messing up so badly.

You’re getting the error because it is a server-side function and you’re calling it clientside…

Additionally; instead of writing the entity as well, it isn’t needed and wastes data transfer, just send it to the entity that gets updated.

On the client, add the meta:SetMana to just update the variable it uses to display the amount on the hud. The server-side should do exactly as it does now except remove net.WriteEntity…

Because, in your client net.Receive, you don’t even use net.ReadEntity( )… You will need to use net.ReadInt( 32 ) in order to set the mana to that… then do LocalPlayer( ).Mana = mana_value_read_from_net

draw.RoundedBoxEx(4, 40, 40, math.Clamp(energy, 0, 100), 100, Color(255, 0, 0, 255), false, false, false, false)

Should be in a draw hook like HUDPaint.

If Mana isn’t supposed to be shown to other players except the LocalPlayer you don’t have to write the entity, you can just set it to LocalPlayer() as Acecool has already said above.

edit: Okay so I changed a few things around, and this is now my code. It now displays text in the upper right hand corner of how much mana the player has.


function GM:HUDPaint()
	--empty hud bars
	draw.RoundedBoxEx(4, 40, 40, 100, 100, Color(41, 41, 41, 180), false, false, false, false)
	draw.SimpleText("Mana: "..mana, "Default", 40, 40, Color(255,255,255))
end
net.Receive("PlayerSetmana", function(len)
	mana = net.ReadInt(32)
	LocalPlayer().Mana = mana
end)

Curiosity, could you add the mana functions in shared? Wouldn’t that work?

Like in a shared file?(sh_file.lua) I havn’t tried that yet. I’ve got my mana functions in just a server file atm.