Jump to content

[help?] Sending data from host to client / getting values of host from client


Recommended Posts

I need to somehow send values from host to client... The only idea I have is this:

Server side code:

darkclone.components.hunger.max = numberofphrasedarkclone.components.hunger.current = numberofarraydarkclone.components.named.name = nameofdarkclone.components.health.maxhealth = timeb

Client side code:

darkclone = FindEntity(self.inst, 30, function(guy) return (guy:HasTag("darkclone"))end)timeb = darkclone.replica.health.maxhealthnumberofarray = darkclone.replica.hunger.current  numberofphrase = numberofphrase = darkclone.replica.hunger.maxname = darkclone.replica.named.name

But this gives a crash on client. Because  timeb, numberofarray, numberofphrase, name are nil.

 

 

Any tips what should I do to get server data from client?

Link to comment
Share on other sites

@Desblat, Look at Peter's The Hunt and player_classified. I think you'll have to use netvars for this, since the health replica's values (like maxhealth) are only available for your own character (they're populated by the player_classified).

This makes sense. Thanks!

Link to comment
Share on other sites

If you plan to make custom net var data make sure to use this in component

if TheWorld.ismastersim then    --serversidefunctionend

to differentiate between client and server/host. I can give you a head start code to give you an idea of how it can work. I only give you an example for hunger component since you seem to be running multiple component for your mod.

local function OnHungerMaxDirty(inst)    inst.components.hunger.max  = inst.component.hunger.net_max:value()endlocal function OnHungerCurrentDirty(inst)    inst.components.hunger.current = inst.components.hunger.net_current:value()endlocal Hunger = Class(function(self, inst)    self.max = 0.0    self.net_max = net_ushortint(self.inst.GUID, "max", "maxdirty" )    self.current = 0.0    self.net_sanityrate = net_ushortint(self.inst.GUID, "current", "sanityratedirty" )    if TheWorld.ismastersim then        --Host data only        self.numberofarray        self.numberofprhase    else--Client side only, listen for these data changes.        self.inst:ListenForEvent("maxdirty", OnHungerMaxDirty)        self.inst:ListenForEvent("currentdirty", OnHungerCurrentDirty)    end    self.inst:StartUpdatingComponent(self)end)function Hunger:OnUpdate(dt)    --If server do    if TheWorld.ismastersim then    --Do some calculation        self.net_max:set(self.numberofphrase)        self.net_current:set(self.numberofarray)    endend

What this does is declare two net variable, self.net_max and self.net_current. For client you are creating listener which listen when the netvariable calls :set(wholeinteger) which will update the OnDirty function.

 

For example when self.net_max:set(value) is detected by self.inst:ListenForEvent("maxdirty",OnHungerMaxDirty), OnHungerMaxDirty will be called and the local variable self.max is updated from the net_max variable.

 

This means that self.max is usable for the client, as it is updated from the server. So there is no need to make further calls to retrieve self.max, just use it.

 

One important note, net_ushortint only support 2 byte, meaning only whole number from 0 - 65535. You will need to use math.floor(value + 0.5) to round number before entering into net variable. You will also need range check, I do not not know the consequence if you try enter bad value, but best to take precaution.  And if you use custom net variable this flag on mod info must be set true or it will cause issue

all_clients_require_mod = true

Other wise if you are just planning to access the character data normally from host as client you can use these code.

darkclone.replica.hunger.classified.currenthunger:value()darkclone.replica.hunger.classified:Max()darkclone.replica.health.classified.currenthealth:value()darkclone.replica.health.classified:Max()

I'll attach a component of my mod which deals with net variable if you are interested.

 

 

 

regeneration_arrowcomponent.lua

Link to comment
Share on other sites

If you plan to make custom net var data make sure to use this in component

all_clients_require_mod = true

Other wise if you are just planning to access the character data normally from host as client you can use these code.

darkclone.replica.hunger.classified.currenthunger:value()darkclone.replica.hunger.classified:Max()darkclone.replica.health.classified.currenthealth:value()darkclone.replica.health.classified:Max()

I'll attach a component of my mod which deals with net variable if you are interested.

Your comment is really helpful! Thanks!

 

However I have one more question: Does mob (darkclone is a mob, not a player) use same code  (classified, replica etc)  as an actual player?

 

 

EDIT: Got a crash as client:

[string "../mods/workshop-357835593/scripts/componen..."]:79: attempt to index field 'classified' (a nil value)

 

I was using this:

 

 

local darkclone = FindEntity(self.inst, 30, function(guy) return (guy:HasTag("darkclone"))end)

 

if  not darkclone then return end

 

self.timeb = darkclone.replica.health.classified:Max() 

Any tips, what is wrong?

Edited by Desblat
Link to comment
Share on other sites

Does mob (darkclone is a mob, not a player) use same code  (classified, replica etc)  as an actual player?
 They have replicas, but they won't have player_classified, so they won't have the health values and such. So that's why your code is crashing there, there's no classified for it to access. You may need to make a classified of your own.
Link to comment
Share on other sites

 classified of your own.

classfieldClass field?  Glass field? Grass field?  ... God, plz no! There should be easier way.

 

 

If mob has replica for health, how can I get current health value of it?

 

 

darkclone.replica.health.maxhealth   ?

Edited by Desblat
Link to comment
Share on other sites

@Deblat

My mistake, I thought this was a character creation. I think you can still pass the mob data to client. Since you are creating custom net variable you as well set the data from component instead of replica, from the host side. From the above code change this line

self.net_current:set(self.numberofarray)

To this

self.net_current:set( math.floor( darkclone.components.health.current + 0.5 ) )

This should make self.current(which should be name as self.currenthealth for better context) receive update from the host of the current health of the mob. From the client side self.current can be used.

The component should exist otherwise replace darkclone.component.health.current with darkclone.replica.health.currenthealth(or darkclone.replica.health.current if you get a nil)

 

You can do something similar with maxhealth. Can I ask why you need the current health and max health passed to the client?

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...