Jump to content

Dummy prefab for multiple recipes to create the same object: how complicated?


Recommended Posts

I've made a simple mod to allow some already existing objects to be crafted, but I'd like to make it possible to use a second recipe for each that takes more ingredients and yields several of the same item at once, with the bulk recipes all taking less total resources than using than it would take to use the single-yield recipe to produce the same quantity. I did a little googling and found that this isn't practical at the moment, and must be done using a dummy prefab that instantly transforms into the desired item instead. How complicated would this be? I'm completely new to modding and am more interested in adjusting existing items/mechanics than introducing new ones, so I don't know the first thing about creating prefabs. :/

Link to comment
Share on other sites

how shadow puppets do it:

local function onbuilt(inst, builder) 
	local item = SpawnPrefab(inst.itemtogive)
	if item.components.stackable ~= nil then
		item.components.stackable:SetStackSize(inst.amount)
	end
	builder.components.inventory:GiveItem(item)
	inst:Remove()
end

local function MakeDummy(prefab, amount)
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()

        inst:AddTag("CLASSIFIED")

        --[[Non-networked entity]]
        inst.persists = false

        --Auto-remove if not spawned by builder
        inst:DoTaskInTime(0, inst.Remove)

        if not TheWorld.ismastersim then
            return inst
        end

        inst.itemtogive = prefab
        inst.amount = amount
        inst.OnBuiltFn = onbuilt

        return inst
    end
    return Prefab(prefab..amount.."_dummy", fn, nil, { prefab })
end

--MakeDummy(item, amount)
return MakeDummy("log", 5)

you can add more items by adding more "MakeDummy"s to the last line

return MakeDummy("log", 5), MakeDummy("boards", 5)

 

Add this to a scripts/prefabs under whatever name.lua, add make sure to add it to modmain.lua

Edited by Aquaterion
Link to comment
Share on other sites

3 hours ago, TheHalcyonOne said:

I did a little googling and found that this isn't practical at the moment, and must be done using a dummy prefab that instantly transforms into the desired item instead.

Ah, yes, but I learned something better!

local AllRecipes = GLOBAL.AllRecipes
local RECIPETABS = GLOBAL.RECIPETABS
local STRINGS = GLOBAL.STRINGS
local TECH = GLOBAL.TECH

local axe_sortkey = AllRecipes["axe"]["sortkey"]

local axe2 = AddRecipe("axe2", {Ingredient("twigs", 10)}, RECIPETABS.TOOLS, TECH.NONE)
axe2.product = "axe"
axe2.image = "axe.tex"
axe2.sortkey = axe_sortkey + 0.1
axe2.numtogive = 1
STRINGS.NAMES.AXE2 = "Another axe"
STRINGS.RECIPE_DESC.AXE2 = "Seriously."

local axe3 = AddRecipe("axe3", {Ingredient("flint", 10)}, RECIPETABS.TOOLS, TECH.NONE)
axe3.product = "axe"
axe3.image = "axe.tex"
axe3.sortkey = axe_sortkey + 0.2
axe3.numtogive = 1
STRINGS.NAMES.AXE3 = "Yet another axe"
STRINGS.RECIPE_DESC.AXE3 = "Yes, seriously."

This is an example for two new axe recipes.

Both have a unique name identifier, axe2 and axe3.

They all produce axe, and have the axe image.

They all come after axe in the crafting table because they are between the axe sortkey  and the axe sortkey + 1.

You can alter both the ingredients table, and also the numtogive to determine how much you craft of the recipe.

They can have new strings for the menu.

 

Adding dummy prefabs isn't hard though.

Link to comment
Share on other sites

@Aquaterion Super appreciate the quick response but that all flew right over my head. :( I haven't used DST Maxwell much so I don't even know what the scenario is, to be honest. Really do appreciate it though!

@DarkXero You're my hero. What's the clash potential here for working with other mods? Also, I'd like to make the order they appear in the crafting list somewhat configurable; like, say, you could configure it so the single and bulk versions of an item appear next to each other, or you could configure it so that all the single recipes of an item appear in the list first, and then the bulk versions come after all of those in, well, bulk. Is that feasible? Would I do the former the way I'm imagining, i.e. get the sort key for the last single and tack the bulk recipes on after? Will the sortkey tolerate values to the hundredth decimal point? (Got 14 recipes)

Link to comment
Share on other sites

4 minutes ago, TheHalcyonOne said:

What's the clash potential here for working with other mods?

First name has got to be unique.

If you use names like "axe_TheHalcyonOne_1" then you most likely will have that id unique for you.

7 minutes ago, TheHalcyonOne said:

Also, I'd like to make the order they appear in the crafting list somewhat configurable; like, say, you could configure it so the single and bulk versions of an item appear next to each other, or you could configure it so that all the single recipes of an item appear in the list first, and then the bulk versions come after all of those in, well, bulk. Is that feasible?

Yes.

You use the configuration_options table of modinfo.lua.

configuration_options = {
	{
		name = "next_or_end",
		label = "All Next or At End",
		hover = "Choose location of new bulk recipes.",
		options = {
			{description = "All Next", data = "next", hover = "New recipes next to old one."},
			{description = "At End", data = "end", hover = "New recipes at end of table."},
		},
		default = "next",
	},
}
16 minutes ago, TheHalcyonOne said:

Would I do the former the way I'm imagining, i.e. get the sort key for the last single and tack the bulk recipes on after? Will the sortkey tolerate values to the hundredth decimal point? (Got 14 recipes)

Yeah, lua got floats.

 

Would look like:

local AllRecipes = GLOBAL.AllRecipes
local RECIPETABS = GLOBAL.RECIPETABS
local STRINGS = GLOBAL.STRINGS
local TECH = GLOBAL.TECH

local axe_sortkey = AllRecipes["axe"]["sortkey"]

for i = 1, 19, 1 do
	local x = i + 1
	local xs = tostring(x)
	local _axe = AddRecipe("axe_TheHalcyonOne_"..xs, {Ingredient("twigs", x),Ingredient("flint", x)}, RECIPETABS.TOOLS, TECH.NONE)
	_axe.product = "axe"
	_axe.image = "axe.tex"
	_axe.numtogive = x
	STRINGS.NAMES["AXE_THEHALCYONONE_"..xs] = STRINGS.NAMES.AXE
	STRINGS.RECIPE_DESC["AXE_THEHALCYONONE_"..xs] = STRINGS.RECIPE_DESC.AXE

	local config = GetModConfigData("next_or_end")
	if config == "next" then
		_axe.sortkey = axe_sortkey + 0.01
		axe_sortkey = _axe.sortkey
	end
end

 

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