Jump to content

I need some help with net_events


Recommended Posts

Hi. I'm working on a client-only mod. I'm trying to listen for when the player builds a structure. I need to know when it happened and what structure it is. I can get it working as the host, but I'm running into issues getting net_events working as a client on a random server. 

This is what I have so far:

local function test()
	print('boop')
end

local function fn()
    local inst = CreateEntity()
    inst.entity:AddNetwork()
    inst.entity:Hide()
    inst:AddTag("CLASSIFIED")

	inst.buildstructure = net_event(inst.GUID, "buildstructure", "buildstructure")
	inst:ListenForEvent("buildstructure", test)

    inst.persists = false
	
    return inst
end

return Prefab("test_classified", fn)

Am I just doing something wrong, or is it necessary for the server to have code on its end to accept net_events from a client? If it's not possible for a client-only mod to do this, then can you think of any workaround to get that data?

Link to comment
Share on other sites

1 hour ago, BluesyBuesy said:

or is it necessary for the server to have code on its end to accept net_events from a client?

Clients don't "send net events". The only thing clients send are RPCs, which, if the server doesn't have a handler to handle them, then they won't do anything. Net variables are variables that must exist on the server and the client, and when the server changes its value, then that change will propagate to the client, but not viceversa.

    * netvars must exist and be declared identically on server and clients
      for each entity, otherwise entity deserialization will fail.  Note
      that this means if a MOD uses netvars, then server and clients must
      all have the same MOD active.

A net event is a 1-bit boolean variable that the server changes so that the client (which should be listening to the event pushed clientside when this variable changes) reacts on it.

That being said, the event pushed client side when a client builds a structure is "builder.build", which in turn triggers "buildsuccess".

Unfortunately for you, what the structure is, is not handled as an argument.

So here what you do is either make your mod a server-client mod, and make a variable that communicates to the client a string containing the built structure name, or keep your mod client side, and making a hack, like getting the structure under your mouse cursor and assume that was the one built, or get the closest structure to you and assume that one was the one built.

Link to comment
Share on other sites

18 minutes ago, DarkXero said:

So here what you do is either make your mod a server-client mod, and make a variable that communicates to the client a string containing the built structure name, or keep your mod client side, and making a hack, like getting the structure under your mouse cursor and assume that was the one built, or get the closest structure to you and assume that one was the one built.

Could also have all structure prefabs hooked with a PrefabPostInit and cache the entity GUIDs for uniqueness, since the entity prefab is constructed multiple times depending if an entity pops out/in.

 

Though what the Bluesy's wanting won't work without the server giving some more information with a client & server mod.

Edited by CarlZalph
+s
Link to comment
Share on other sites

32 minutes ago, DarkXero said:

Clients don't "send net events". The only thing clients send are RPCs, which, if the server doesn't have a handler to handle them, then they won't do anything. Net variables are variables that must exist on the server and the client, and when the server changes its value, then that change will propagate to the client, but not viceversa.


    * netvars must exist and be declared identically on server and clients
      for each entity, otherwise entity deserialization will fail.  Note
      that this means if a MOD uses netvars, then server and clients must
      all have the same MOD active.

A net event is a 1-bit boolean variable that the server changes so that the client (which should be listening to the event pushed clientside when this variable changes) reacts on it.

That being said, the event pushed client side when a client builds a structure is "builder.build", which in turn triggers "buildsuccess".

Unfortunately for you, what the structure is, is not handled as an argument.

So here what you do is either make your mod a server-client mod, and make a variable that communicates to the client a string containing the built structure name, or keep your mod client side, and making a hack, like getting the structure under your mouse cursor and assume that was the one built, or get the closest structure to you and assume that one was the one built.

I was afraid of that. Your ideas sound promising though. I'll definitely look into those and see if they're reliable at all. If not, then I'll just have to exclude some of DST's content from my mod, which sucks, but it won't be the end of the world.

Thanks @DarkXero and @CarlZalph for the help.

Link to comment
Share on other sites

By definition, net_event (other net_vars) are used to facilitate communication from the server to the client. Thus using them in a client-only mod adds no value. Furthermore, adding a net_var to an existing networked entity only on the client can lead to network synchonization issues that could cause a crash (see e.g. many reports of Smarter Crock Pot mod crashes) or "only" an incorrect deserialization of other net_vars of the same entity (visual glitches and incorrect stat values if the entity is the player).

For communication within a client (or within a server), an event could be used - you push an event on an entity to signalize to all listeners what has happened.
In your code, you've defined a new event buildstructure usable on an entity of test_classified prefab that would be pushed when the net_event is pushed; this event is different from buildstructure event that is being pushed on a player entity (of any prefab that uses MakePlayerCharacter helper function (which adds builder component to an entity) to define a player prefab).

buildstructure event on a player entity is being pushed in components/builder|DoBuild(), which is called from ACTIONS.BUILD.fn (that is called from BufferedAction:Do(), which is called from various EntityScript/stategraph/locomotor functions on the server only).

For an alternative see posts above.

Edited by Muche
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...