Jump to content

Creating Custom Containers


Recommended Posts

Hello! I'm adding a feature to my mod, where the player is able to make craftable upgrades to their character's custom gear. In the case I'm trying to add this to a Hat item.

The upgrades will go in a custom slot above the item, similar to how ammo is loaded into Walter's Slingshot (See image 1 and 2). The slot appears when the item is equipped, and disappears when it isn't equipped.

Image 1: Normal Hotbar

Image 2: Walter Slingshot Equipped

jBrawvj.png  OZUi4it.png

I've looked into the slingshot's prefab file and figured out how to add this slot to my own items, but I've run into a few problems:

1. The slot still only accepts Slingshot Ammo

2. How I have it written seems to crash on servers with caves enabled.

3. The slot always only appears above the Hand Slot.

I don't know how I can solve the 2nd problem, but the 1st and 3rd problems I believe I could solve by coding my own container, I just have no idea how I could go about doing that.

Here is the code of my prefab, which contains all the container code as well as some extra effects the Hat already does. https://pastebin.com/f9a8tJQP

 

I hope you can help me, and I hope I wasn't too confusing trying to explain my problem.

Edit: Here is my client crash log when I equip the related Hat when I wasn't the host. https://pastebin.com/rJWKmJJA
 

 

Edited by TheSkylarr
added crash lag
Link to comment
Share on other sites

sorry i just realised how useless that was uhh gimme a sec

local Vector3 = GLOBAL.Vector3

local params = {}

params.hat_stuff = {
    widget =
    {
		slotpos =
        {
            Vector3(0,   32 + 4,  0),
        },
        slotbg =
        {
            { image = "slingshot_ammo_slot.tex" },
        },
        animbank = "ui_chest_3x2",
        animbuild = "ui_chest_3x2",
        pos = Vector3(0, 15, 0),
    },
    usespecificslotsforitems = true,
    type = "hand_inv",
}

function params.hat_stuff.itemtestfn(container, item, slot)
	return item:HasTag("Your_Tag") or item.prefab == "item_prefab"
end

local containers = GLOBAL.require("containers")

containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, params.hat_stuff.widget.slotpos ~= nil and #params.hat_stuff.widget.slotpos or 0)

local old_wsetup = containers.widgetsetup

function containers.widgetsetup(container, prefab, data)
	if params[prefab or container.inst.prefab] and not data then
		data = params[prefab or container.inst.prefab]
	end
	old_wsetup(container, prefab, data)
end

Sorry i think this should make a new widget to use but i'm not entirely sure, this is from rosalinas code

Link to comment
Share on other sites

6 hours ago, thomas4846 said:

sorry i just realised how useless that was uhh gimme a sec


local Vector3 = GLOBAL.Vector3

local params = {}

params.hat_stuff = {
    widget =
    {
		slotpos =
        {
            Vector3(0,   32 + 4,  0),
        },
        slotbg =
        {
            { image = "slingshot_ammo_slot.tex" },
        },
        animbank = "ui_chest_3x2",
        animbuild = "ui_chest_3x2",
        pos = Vector3(0, 15, 0),
    },
    usespecificslotsforitems = true,
    type = "hand_inv",
}

function params.hat_stuff.itemtestfn(container, item, slot)
	return item:HasTag("Your_Tag") or item.prefab == "item_prefab"
end

local containers = GLOBAL.require("containers")

containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, params.hat_stuff.widget.slotpos ~= nil and #params.hat_stuff.widget.slotpos or 0)

local old_wsetup = containers.widgetsetup

function containers.widgetsetup(container, prefab, data)
	if params[prefab or container.inst.prefab] and not data then
		data = params[prefab or container.inst.prefab]
	end
	old_wsetup(container, prefab, data)
end

Sorry i think this should make a new widget to use but i'm not entirely sure, this is from rosalinas code

In this case, I'm assuming I would throw this into my modmain.lua, and specify my widget setup as hat_stuff in the component for my item? I would test it but I'm a little busy at the moment.

Link to comment
Share on other sites

7 hours ago, thomas4846 said:

probs, i tried something like this a while ago and it was something like that

Thanks for the guidance, this got me to a state where I it's almost where I want, but it still seems to crash with the same error on a world with caves, and doesn't show anything to do with my mod, but I know for sure it's because of it.
The crash log is here.https://pastebin.com/FavMxWcX

21 minutes ago, TheSkylarr said:

Thanks for the guidance, this got me to a state where I it's almost where I want, but it still seems to crash with the same error on a world with caves, and doesn't show anything to do with my mod, but I know for sure it's because of it.
The crash log is here.https://pastebin.com/FavMxWcX

I actually figured out how to fix this by studying some other mods.

If you have a problem with containers with caves enabled servers, in your prefabs main function, add this
 

inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup(WIDGET_VARIABLE) end

inside the part of the function that DOESN'T run for the server, only clients. That should look similar, if not the same to this:
 

    if not TheWorld.ismastersim then
        return inst
    end

Your result should look like this. What this does is replicate the right values for your container to the clients, as the clients don't look at the same variables for widgets and such things, they have their own.

    if not TheWorld.ismastersim then
		inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup(WIDGET_VARIABLE) end
        return inst
    end

 

  • Thanks 1
Link to comment
Share on other sites

On 12/7/2020 at 12:23 AM, TheSkylarr said:

Hello! I'm adding a feature to my mod, where the player is able to make craftable upgrades to their character's custom gear. In the case I'm trying to add this to a Hat item.

The upgrades will go in a custom slot above the item, similar to how ammo is loaded into Walter's Slingshot (See image 1 and 2). The slot appears when the item is equipped, and disappears when it isn't equipped.

Image 1: Normal Hotbar

Image 2: Walter Slingshot Equipped

jBrawvj.png  OZUi4it.png

I've looked into the slingshot's prefab file and figured out how to add this slot to my own items, but I've run into a few problems:

1. The slot still only accepts Slingshot Ammo

2. How I have it written seems to crash on servers with caves enabled.

3. The slot always only appears above the Hand Slot.

I don't know how I can solve the 2nd problem, but the 1st and 3rd problems I believe I could solve by coding my own container, I just have no idea how I could go about doing that.

Here is the code of my prefab, which contains all the container code as well as some extra effects the Hat already does. https://pastebin.com/f9a8tJQP

 

I hope you can help me, and I hope I wasn't too confusing trying to explain my problem.

Edit: Here is my client crash log when I equip the related Hat when I wasn't the host. https://pastebin.com/rJWKmJJA
 

 

I think I have done something very similar with "rail cart" mod, where the Rail Cart Tool have a slot to place the rails

Take a look if you still need some guidance

Link to comment
Share on other sites

4 hours ago, Gleenus said:

I think I have done something very similar with "rail cart" mod, where the Rail Cart Tool have a slot to place the rails

Take a look if you still need some guidance

That sounds like a nice example, though I ended up scrapping this feature in the end, I would still like to know how. Thank you.

  • Like 1
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...