Jump to content

Recommended Posts

I am in the process of implementing my own component and its replica and I am encountering one last problem:

The oxygen replica doen't exist on the client side

modmain.lua:

AddPlayerPostInit(function(inst)
    AddReplicableComponent("oxygen")

    if GLOBAL.TheWorld.ismastersim then
        --Server
        inst:AddComponent("oxygen")
    else
        --Client
        print(inst.replica.oxygen) -- nil here and other places
    end
end)

oxygen.lua:

local function onmax(self, max)
    self.inst.replica.oxygen:SetMax(max)
end

local function oncurrent(self, current)
    self.inst.replica.oxygen:SetCurrent(current)
end

local Oxygen = Class(function(self, inst)
    self.inst = inst
    self.max = 30
    self.current = self.max
end, nil, {
    max = onmax,
    current = oncurrent
})

return Oxygen

I know for sure that the replica exists on the server side because it is called by the onmax and oncurrent functions.

oxygen_replica.lua:

local function OnOxygendirty(inst)
    print(inst.replica.oxygen)
end

local Oxygen = Class(function(self, inst)
    self.inst = inst

    self.maxoxygen = net_ushortint(inst.GUID, "oxygen.max", "oxygendirty")
    self.currentoxygen = net_ushortint(inst.GUID, "oxygen.current", "oxygendirty")

    if not TheWorld.ismastersim then
        inst:ListenForEvent("oxygendirty", OnOxygendirty)
    end
end)

function Oxygen:SetCurrent(current)
    if TheWorld.ismastersim then
        self.currentoxygen:set(current)
    end
end

function Oxygen:SetMax(max)
    if TheWorld.ismastersim then
        self.maxoxygen:set(max)
    end
end

return Oxygen

The replica is built like named_replica.lua

Is there a mistake somewhere?

References:

 

Edited by Nhyarlathoteps
Fix spoiler

@NhyarlathotepsI think you have to AddReplicableComponent("oxygen") at the beginning of the script. Also, it does take time for the replica to show up on the client. It's not instanteous, but should be there very quickly.

  • Like 2

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