Jump to content

Recommended Posts

I've managed to create a custom component for my character and a custom badge. However when caves are enabled it doesn't display the value at all.
I'm struggling to put the two together and I think I'm missing something. I think it may be a replica version of my component. I've made one but I've clearly not done it correctly.
I would like some help getting these pieces working together. I've attached the component, the replica and badge.

brightness.lua brightness_replica.lua greenbadge.lua

-- health.lua, sanity.lua, hunger.lua
if TheWorld.ismastersim then
  self.classified = inst.player_classified
elseif self.classified == nil and inst.player_classified ~= nil then
  self:AttachClassified(inst.player_classified)
end

Replicas' existence typically, if not always, rely on TheWorld being mastersim or not.

If you are wondering what the purpose of replicas is generally, the Zark's first reply on this thread put it nicely.

I think the reason it doesnt work when you have caves enabled is because the variable changes to a classified state (usually with no caves ThePlayer typically exists on mastersim. Either TheNet or TheWorld becomes master simulation in game. and can be checked with TheNet:GetIsMasterSimulation() or TheWorld:GetIsMasterSimulation(). In the code its typically TheWorld.ismastersim or inst.Network == nil informally.

The reason the variables become classified is because we want netvars to only be modified on TheNet (because you want to sync caves and forest shards I believe).

You dont need any additional net variables because the component is supposed to handle that in a classified way and they are stored on the replica already by the original component (see ***), the reason why sanity has.

self._issane = net_bool(inst.GUID, "sanity._issane", "issanedirty")

for instance is because your HUD changes drastically depending on your sanity state and needs to be handled with care in case you desync (you can check with Local commands, on Local console do ThePlayer.replica.sanity._issane). The comment references lunacy so I guess this is to ensure you are never in lunacy mode and sanity mode at the same time (which might happen with lag but never without it?).

Spoiler

image.png.d2e135a57a2e6ef62bfb82a227aaafbc.png

i believe component replicas are added automatically in the entity script. i might be wrong though, but the way to access replicas is inst.replica in your original component.

***Replica components functions typically get bundled with regular components because replica components are supposed to do what the components do plus different things depending on the network's needs. I think all of what you added to the replica originally handled by bundling functions to the replica and that creating new variables is not necessarily what needs to be done unless you have other variables at play that are necessary on the net.

before sanity.lua class fn (the only time where .replica is mentioned in the component)

Spoiler

Screenshot2025-02-24201339.png.3887b4e508f7809968dde2f2f86a6733.png

At the end of sanity.lua class fn

Spoiler

Screenshot2025-02-24201352.png.2e675c9999fbcaa729849def45cd88e5.png

Also something last to mention is that replica functions mention other replicas and use "if not self.classified" a lot, maybe something else to keep in mind if something ends up not working as desired..

Hope that helped explain a bit. I'm not entirely sure on the details of what you need to change without all the files because this netvar stuff is generally still new to me, but maybe this is enough to put you in the right direction.

Edited by oregu

I've made the meter start at 100, and when I press a key it drains to 0 using components.brightness:SetCurrent(0). Inside the SetCurrent function I use self.inst.replica.brightness.net_current:set() to update the net var.

I cannot seem to get it to fill back up as the component increases. I need to update the replica's net var to stay in sync with the component's variables. Is there anyway I can keep them in sync when it increases?

Glad you figured it out. Hindsight is 20/20 but generally what helps is to create a debug string that displays all your necessary variables in case something goes wrong.  The variable for whether it updates would have been displayed, then.

Lots of components have a GetDebugString(). Also DumpComponent(inst,component.name) can be used (it's in debughelpers).

Edited by oregu

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
×
  • Create New...