Jump to content

Problem about writing a mod for server and clients


Recommended Posts

I've been writing a weapon pack mod these days and I encountered with a few questions. Mainly, I don't get the idea about how things are working with clients and servers. Specifically, I don't know where to use "components" and where to use "replica" and when a wrong usage of these two words will cause a crash on client or server.

Also when I'm starting a game with a cave (I suppose this will make me a client server so that I can test things as a client), I don't receive the debug info I printed on the console that should've popped if I am the server. What's going on here?

Here is a callback function when an item's finiteuse is depleted in my code that has a problem:

local function OnFinished(inst)
    local owner = inst.components.inventoryitem.owner
    if TheWorld.ismastersim then
        for slot, item in pairs(owner.components.inventory.itemslots) do
            if item.prefab == inst.prefab then
                owner.components.inventory:Equip(item)
                break
            end
        end
    else
        for slot, item in pairs(owner.replica.inventory.itemslots) do
            if item.prefab == inst.prefab then
                owner.replica.inventory:Equip(item)
                break
            end
        end
    end
    inst:Remove()
end
What I'm trying to do is to auto-equip a specific weapon when the same kind of weapon is depleted. But this piece of code crashes when the weapon is depleted. And the game just tells me I am disconnected from the server without any debugging details.
I really need some help and advice about how to make things work on both client and server machines. 
THX!
Link to comment
Share on other sites

On 7/19/2021 at 7:40 AM, Apokli said:

auto-equip a specific weapon when the same kind of weapon is depleted

local function OnFinished(inst) 
    local owner = inst.components.inventoryitem.owner
	local item = inst.components.inventory:FindItem(function(item) return item.prefab == 'Your items code name' and item ~= inst end) 
	-- using the find item function made graciously by the devs god bless their souls to find your item
    -- please don't release an update that crashes my mods 

	if item == nil then --if the item doesn't exist/ = nil then it'll return the function and end it prematurely
	   return
	end

	owner.components.inventory:Equip(item) --equips item
    inst:Remove() -- gets deleted
end
--The find items function
function Inventory:FindItem(fn)
    for k,v in pairs(self.itemslots) do --  goes through all of the item slots 
        if fn(v) then -- uses your comparing thing
            return v -- returns if its good
        end
    end

    if self.activeitem and fn(self.activeitem) then -- stuff
        return self.activeitem
    end

    local overflow = self:GetOverflowContainer() -- stuff
    return overflow ~= nil and overflow:FindItem(fn) or nil
end

I'm not really a coder should ask big cheeses like penguin or carl here for that, but server stuff is i think is the game itself and everything going on
client stuff is things that only appear on your end.
so all of this is just gonna be server stuff

client stuff may be stuff like:
  >colour cubes
  >textures/models sometimes
  >hud stuff like stats

btdubs i don't think you'll need to use the if world master sim as long as your code is called beneath it

oh yeah you can still use replica (client stuff) to get data but honestly i'm not sure what's the upside for that when using it for server stuff

Edited by Thomas Die
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...