LuciAmitoLom Posted April 20, 2025 Share Posted April 20, 2025 Hello everyone! I have created a new recipe and a new structure. How can I impose the condition that allows crafting that recipe only if the player is at a certain distance from the structure? I also cannot make the structure destructible through hammering... I've been wasting my time on it for months modmain.lua glenda.lua glenda_placer.lua scatola1.lua scatola1_placer.lua Link to comment https://forums.kleientertainment.com/forums/topic/165418-help-crafting-modded-items-only-if-the-player-is-near-a-structure/ Share on other sites More sharing options...
Merkyrrie Posted April 21, 2025 Share Posted April 21, 2025 You shouldn't need separate prefab files for the placers, both as files and in PrefabFiles in modmain, the MakePlacer does most of that work for you. For it not hammering, you need to add some extra lines to your prefab in the fn function right under inst:AddComponent("workable") inst.components.workable:SetWorkAction(ACTIONS.HAMMER) inst.components.workable:SetWorkLeft(5) -- This number can be whatever you want, its how many hits it takes to cause it to break inst.components.workable:SetOnFinishCallback(onhammered) Then also move the onhammered function above the fn function (everything within the a function doesn't know any function or variable below it exists) Making a structure into a prototyper (crafting station) is a little more complicated but not bad, add these lines in the same place inst:AddComponent("prototyper") inst.components.prototyper.trees = TUNING.PROTOTYPER_TREES.MY_CUSTOM_TREE -- the "MY_CUSTOM_TREE" part can be just about anything you want as long as its not already in use -- There is some extra lines you can add for extra effects similar to the other prototypers in the game -- inst.components.prototyper.onturnon = onturnon -- For activating sound effects and animation when a player walks nearby -- inst.components.prototyper.onturnoff = onturnoff -- For deactivating sound effects and animation when a player is no longer nearby -- inst.components.prototyper.onactivate = onactivate -- For sound effects and animation when a player crafts something -- inst.components.prototyper.restrictedtag = "characterspecifictaghere" -- For making it so only a character with this tag can use the prototyper, with "characterspecifictaghere" either being a custom tag for your modded character (if you had one) or a tag specific to a vanilla character, for instance Warly has "masterchef" The TUNING.PROTOTYPER_TREES.MY_CUSTOM_TREE part won't do anything right now so you'll need to add this to your modmain local TUNING = GLOBAL.TUNING local TECH = Global.TECH local TechTree = require("techtree") table.insert(TechTree.AVAILABLE_TECH, "MY_CUSTOM_TREE") TECH.NONE.MY_CUSTOM_TREE = 0 TECH.MY_CUSTOM_TREE_ONE = {MY_CUSTOM_TREE = 1} -- Set all other prototyping trees to have level 0 for your custom one, so they can't craft those for k, v in pairs(TUNING.PROTOTYPER_TREES) do v.MY_CUSTOM_TREE = 0 end TUNING.PROTOTYPER_TREES.MY_CUSTOM_TREE = TechTree.Create({MY_CUSTOM_TREE = 1}) -- Set all vanilla recipes to not use your custom prototyper for i, v in pairs(GLOBAL.AllRecipes) do if v.level.MY_CUSTOM_TREE == nil then v.level.MY_CUSTOM_TREE = 0 end end Lastly, you'll need to update the recipes you want to use that custom prototyper with the new tech level. I noticed you are using AddRecipe for your recipes, which is the deprecated function for adding modded recipes, I suggest (and will show here how) to use AddRecipe2 instead like this AddRecipe2("glenda", { Ingredient("boards", 1), }, TECH.NONE, -- For recipes to add to your custom prototyping tree, replace this with TECH.MY_CUSTOM_TREE_ONE or whatever you used -- Everything in the next { } falls under the 'config' parameter being passed to AddRecipe2, this replaces all those extra 'nils' in AddRecipe. Since its done with keys like 'atlas' or 'placer' now, you only have to add the ones relevant to the recipe and ignore the rest instead of setting everything you don't need to nil. They can also be in any order. All config options are placer, min_spacing, nounlock, numtogive, builder_tag, image, atlas, testfn, product, build_mode, and build_distance. Most of these will usually not be relevant. { atlas = "images/inventoryimages/glenda.xml", image = "glenda.tex", placer = "glenda_placer" }, {"STRUCTURES", "GARDENING"} -- This replaces the recipetabs part with the new crafting filters. Structures and Gardening would both be relevant here ) 2 Link to comment https://forums.kleientertainment.com/forums/topic/165418-help-crafting-modded-items-only-if-the-player-is-near-a-structure/#findComment-1813293 Share on other sites More sharing options...
LuciAmitoLom Posted May 23, 2025 Author Share Posted May 23, 2025 Hi, thank you very much and sorry for the late reply! I still have some problems... I modified the code by inserting the lines you indicated and moved the onhammered function above the fn function but my character still not hammer the structure: I'm missing something local function onhammered(inst) local fx = SpawnPrefab("collapse_small") fx.Transform:SetPosition(inst.Transform:GetWorldPosition()) fx:SetMaterial("wood") inst.components.lootdropper:DropLoot() inst:Remove() end local function fn() local inst = CreateEntity() inst.entity:AddTransform() inst.entity:AddAnimState() inst.entity:AddSoundEmitter() inst.entity:AddMiniMapEntity() inst.entity:AddNetwork() MakeObstaclePhysics(inst, 0.66) inst:AddTag("structure") local minimap = inst.entity:AddMiniMapEntity() minimap:SetIcon("glenda.tex") inst.AnimState:SetBank("glenda") inst.AnimState:SetBuild("glenda") inst.AnimState:PlayAnimation("idle") if not TheWorld.ismastersim then return inst end STRINGS.CHARACTERS.GENERIC.DESCRIBE.glenda = "Il mercato di Glenda!" inst:AddComponent("lootdropper") inst:AddComponent("inspectable") inst:AddComponent("workable") inst.components.workable:SetWorkAction(ACTIONS.HAMMER) inst.components.workable:SetWorkLeft(5) -- This number can be whatever you want, its how many hits it takes to cause it to break inst.components.workable:SetOnFinishCallback(onhammered) return inst end I blundered while trying to create the crafting station local require = GLOBAL.require local STRINGS = GLOBAL.STRINGS local Ingredient = GLOBAL.Ingredient local RECIPETABS = GLOBAL.RECIPETABS local TECH = GLOBAL.TECH local ACTIONS = GLOBAL.ACTIONS local Action = GLOBAL.Action SpawnPrefab = GLOBAL.SpawnPrefab ---- MERCATO DI GLENDA ---------------------------------------------------------------- PrefabFiles = { "glenda", "glenda_placer", "scatola1", "scatola1_placer" } local assets= { Asset("ATLAS", "images/inventoryimages/glenda.xml"), Asset("IMAGE", "images/inventoryimages/glenda.tex"), Asset("ANIM", "anim/glenda.zip"), Asset("ATLAS", "images/inventoryimages/scatola1.xml"), Asset("IMAGE", "images/inventoryimages/scatola1.tex"), Asset("ANIM", "anim/scatola1.zip") } AddMinimapAtlas("images/inventoryimages/glenda.xml") AddMinimapAtlas("images/inventoryimages/scatola1.xml") local TUNING = GLOBAL.TUNING local TECH = Global.TECH local TechTree = require("techtree") table.insert(TechTree.AVAILABLE_TECH, "MY_CUSTOM_TREE") TECH.NONE.MY_CUSTOM_TREE = 0 TECH.MY_CUSTOM_TREE_ONE = {MY_CUSTOM_TREE = 1} for k, v in pairs(TUNING.PROTOTYPER_TREES) do v.MY_CUSTOM_TREE = 0 end TUNING.PROTOTYPER_TREES.MY_CUSTOM_TREE = TechTree.Create({MY_CUSTOM_TREE = 1}) for i, v in pairs(GLOBAL.AllRecipes) do if v.level.MY_CUSTOM_TREE == nil then v.level.MY_CUSTOM_TREE = 0 end end AddRecipe2("glenda", { Ingredient("boards", 1), }, TECH.NONE, { atlas = "images/inventoryimages/glenda.xml", image = "glenda.tex", placer = "glenda_placer" }, {"STRUCTURES", "GARDENING"} -- This replaces the recipetabs part with the new crafting filters. Structures and Gardening would both be relevant here ) GLOBAL.STRINGS.NAMES.GLENDA = "Glenda" --It's name in-game GLOBAL.STRINGS.RECIPE_DESC.GLENDA = "Il mercato di Glenda!" --recipe description AddRecipe2("scatola1", { Ingredient("boards", 1), }, TECH.MY_CUSTOM_TREE_ONE, { atlas = "images/inventoryimages/scatola1.xml", image = "scatola1.tex", placer = "scatola1_placer" }, {"STRUCTURES", "GARDENING"} -- This replaces the recipetabs part with the new crafting filters. Structures and Gardening would both be relevant here ) GLOBAL.STRINGS.NAMES.SCATOLA1 = "Una scatola piena di semi!" --It's name in-game GLOBAL.STRINGS.RECIPE_DESC.SCATOLA1 = "Una scatola piena di semi!" --recipe description Thank you for helping master_server_log.txt modmain.lua glenda.lua Link to comment https://forums.kleientertainment.com/forums/topic/165418-help-crafting-modded-items-only-if-the-player-is-near-a-structure/#findComment-1817160 Share on other sites More sharing options...
LuciAmitoLom Posted June 15, 2025 Author Share Posted June 15, 2025 up Link to comment https://forums.kleientertainment.com/forums/topic/165418-help-crafting-modded-items-only-if-the-player-is-near-a-structure/#findComment-1822609 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now