Jump to content

Can anyone help me understand net_variables?


Recommended Posts

Hello. I am going to start off by saying I looked through the thread by PeterA on net_vars. I also tried Googling it but nothing shows up that is relevant or helps me understand any better. For the record, I am still learning LUA, and as such, there are many "complex" things about it that I don't understand. I have looked at LUA tutorials as well but they still somewhat fly over my head because I have no prior coding experience.

Regardless, I'm still extremely determined to finish the character mod and have made tons of progress thanks to my friend, @poolcool2. I have been told here that I should learn how net_variables work in order to accomplish what I want to do as mentioned in this thread I made. I am just trying so hard to understand that it's really frustrating having to rely on everyone explaining what every tiny thing is to me in Dummy's Simple English. Though, if anyone knows of any good resources that explains LUA and/or net_variables and what they are/how they work in an easy to understand way, please share them with me too!

Thank you and any help at all is greatly appreciated!

  • Health 1
Link to comment
Share on other sites

Net variables are used to transmit data from the server to the client, I think this is something that you already know. 

To make a netvar, you first need to declare it:

inst.net_var = net_bool(inst.GUID, "variable" , "variabledirty")

You have your variable that is called net_var, which is a net_bool, that means it can take the values true or false. "variable" is just a name for the netvar that you can choose, it should just not be called as something else already. "variabledirty" is an event that is fired if the value of net_var is changed.

You cannot change inst.net_var as you change other variable. You need to use set to change the value:

inst.net_var:set(true)

You also have set_local, that changes the value only on the client, it can be used to trigger the ondirty events without changing the value, but that isn't very important at the beginning.

If you want to know the value, you can use

local val = inst.net_var:value()

to save it to a local variable.

So for example, you want to use net_var, so that something only on the client happens. Then you need to add a ListenForEvent to the client.

if TheNet:GetIsClient() then
	inst:ListenForEvent("variabledirty", function(inst)
		print("it works")
	end)
end

If you then change net_var somewhere in a function, the event "variabledirty" will be triggered and in your client log it works will appear. If you want the event to trigger on both the server and client, like you would need for nightsight, you remove the check to see if it's the client.

The same applies for the other netvars, you can transfer numbers or strings for example, so that the client also knows these values.

If you were still not able to get the meter working, shoot me a message with your mod and I will see what I can do ;)

  • Like 2
  • GL Happy 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...