Jump to content

Recommended Posts

Hi there, recently I've been having an issue concerning my custom 'furnace'. I wanted it to smelt ores and for this I created two new components similar to cooker and cookable, named smelter and smeltable. After that I added the components to the respective items and structure but in-game I dont have the option to 'smelt' my ores when standing close to the furnace. Any help on this problem?

 

 

smelter.lua (located in scripts/components)

local Smelter = Class(function(self, inst)
self.inst = inst
 
inst:AddTag("smelter")
end)
 
function Smelter:onremoveFromEntity()
self.inst:Removetag("smelter")
end
 
function Smelter:isSmeltable(item, owner)
return item ~= nil
and item.components.smeltable ~= nil
end
 
function Smelter:SmeltItem(item, owner)
if self:isSmeltable(item, owner) then
local newitem = item.components.smeltable:Smelt(self.inst, owner)
 
        if self.onsmeltitem ~= nil then
            self.onsmeltitem(item, newitem)
        end
 
if self.inst.SoundEmitter ~= nil then
            self.inst.SoundEmitter:PlaySound("dontstarve/wilson/cook")
        end
 
   if self.onsmeltfn ~= nil then
self.onsmeltfn(self.inst, newitem, owner)
end
 
        item:Remove()
        return newitem
    end
end
 
return Smelter

 

 

smeltable.lua (located in scripts/components)

local Smeltable = Class(function(self, inst)    self.inst = inst    self.product = nil    self.onsmelted = nil		inst:AddTag("smeltable")end)function Smeltable:OnRemoveFromEntity()    self.inst:RemoveTag("smeltable")endfunction Smeltable:SetOnSmeltedFn(fn)    self.onsmelted = fnendfunction Smeltable:Smelt(smelter, owner)    if self.onsmelted ~= nil then        self.onsmelted(self.inst, smelter, owner)    end    if self.product ~= nil then        local prod = SpawnPrefab(            type(self.product) ~= "function" and            self.product or            self.product(self.inst, smelter, owner)        )        if prod ~= nil then            return prod        end    endendreturn Smeltable 

 

copperore.lua (one of the ores, located in scripts/prefabs)

-- Asset defining (required)local Assets = {	Asset("ANIM", "anim/copperore_anim.zip"),	Asset("ATLAS", "images/inventoryimages/copperore.xml")}-- Prefab creation/customization (returns intsance)local function fn(Sim)-- Entity creation and transformations	local inst = CreateEntity()	inst.entity:AddTransform()	inst.entity:AddAnimState()		MakeInventoryPhysics(inst)	-- Animation handler?	    inst.AnimState:SetBank("copperore")    inst.AnimState:SetBuild("copperore_anim")    inst.AnimState:PlayAnimation("idle", true)		inst:AddTag("smeltable")	inst:AddTag("molebait")    inst:AddTag("renewable")		inst.entity:SetPristine()	    if not TheWorld.ismastersim then        return inst    end        inst:AddComponent("inventoryitem")	inst.components.inventoryitem.atlasname = "images/inventoryimages/copperore.xml"	inst:AddComponent("inspectable")		inst:AddComponent("smeltable")	inst.components.smeltable.product = "copperingot"		inst:AddComponent("stackable")    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM        return instend-- Name and descriptionSTRINGS.NAMES.COPPERORE = "Copper Ore"STRINGS.CHARACTERS.GENERIC.DESCRIBE.COPPERORE = "Maybe I can refine this."STRINGS.CHARACTERS.WOLFGANG.DESCRIBE.COPPERORE = "Is rock, but different."STRINGS.CHARACTERS.WAXWELL.DESCRIBE.COPPERORE = "Hmmm. Now what do I do with this?"STRINGS.CHARACTERS.WATHGRITHR.DESCRIBE.COPPERORE = "A pile of ancient gravel."STRINGS.CHARACTERS.WEBBER.DESCRIBE.COPPERORE = "What are we supposed to do with this?"STRINGS.CHARACTERS.WENDY.DESCRIBE.COPPERORE = "Oh, look what we got from mere destruction."STRINGS.CHARACTERS.WICKERBOTTOM.DESCRIBE.COPPERORE = "A pile of gravel, mixed with traces of metal."STRINGS.CHARACTERS.WILLOW.DESCRIBE.COPPERORE = "Maybe I can burn this."STRINGS.CHARACTERS.WOODIE.DESCRIBE.COPPERORE = "All busted up."STRINGS.CHARACTERS.WX78.DESCRIBE.COPPERORE = "I WILL REFINE THESE INTO HIGH TECHNOLOGY"-- Return prefab with costumization (ESSENTIAL)return Prefab("common/inventory/copperore", fn, Assets) 

 

furnace.lua (located in scripts/prefabs, NOTE: I know it doesnt accept fuel or behave like a campfire, im planning on implementing it.)

require "prefabutil"require "recipe"require "modutil"local assets={    Asset("ANIM", "anim/furnace_anim.zip"),    Asset("ATLAS", "images/inventoryimages/furnace.xml"),    Asset("IMAGE", "images/inventoryimages/furnace.tex"),}local function onhammered(inst, worker)    inst.components.lootdropper:DropLoot()    inst.SoundEmitter:PlaySound("dontstarve/common/destroy_stone")    inst:Remove()endlocal function ontakefuel(inst)    inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")endlocal function onbuilt(inst)    inst.AnimState:PlayAnimation("place")    inst.AnimState:PushAnimation("idle", false)    inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")endlocal function fn()    local inst = CreateEntity()    inst.entity:AddTransform()    inst.entity:AddAnimState()    inst.entity:AddSoundEmitter()    inst.entity:AddMiniMapEntity()    inst.entity:AddNetwork()    MakeObstaclePhysics(inst, 1)	    inst.AnimState:SetBank("furnace")    inst.AnimState:SetBuild("furnace_anim")    inst.AnimState:PlayAnimation("idle", false)	    inst:AddTag("structure")    local minimap = inst.entity:AddMiniMapEntity()    minimap:SetIcon( "furnace.tex" )	minimap:SetPriority(1)	inst:AddTag("smelter")    inst.entity:SetPristine()    if not TheWorld.ismastersim then        return inst    end		inst:AddComponent("smelter")    inst:AddComponent("lootdropper")    inst:AddComponent("inspectable")	inst:AddComponent("workable")	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)    inst.components.workable:SetWorkLeft(4)    inst.components.workable:SetOnFinishCallback(onhammered)	    inst:ListenForEvent("onbuilt", onbuilt)    return instendSTRINGS.NAMES.FURNACE = "Furnace"STRINGS.RECIPE_DESC.FURNACE = "Smelts ores."STRINGS.CHARACTERS.GENERIC.DESCRIBE.FURNACE = "I can't cook food in this one..."return Prefab( "common/furnace", fn, assets, prefabs),        MakePlacer( "common/furnace_placer", "furnace", "furnace", "idle" )  

 

 

PS: The furnace placer doesnt show a 'green outline' like the regular structures when you craft them, any idea to fix it?

PS: Is there a way to make the furnace emit light based on its fuel level? (Campfire adds it with the flame particle based on its 'section')

 

Help is greatly appreciated.

@Overreactedosbol

For the green outline, change:

MakePlacer( "common/furnace_placer", "furnace", "furnace", "idle" )  

to

MakePlacer( "common/furnace_placer", "furnace_anim", "furnace", "idle" )  

The second argument of this function is animation bank name.

 

For the smelting action, you need to add this in your modmain:

local smeltable = function(inst, doer, target, actions)            if target:HasTag("smelter") then                table.insert(actions, ACTIONS.SMELT)            end        endAddComponentAction("USEITEM", "smeltable", smeltable)

The "smeltable" function will be called every time you hover something with your cursor while holding a "smeltable" item.

Thanks! I knew it had something to do with it having to be registered in the modmain.lua, but now it crashes.

Seemingly, lua can't find ACTIONS.SMELT, do I need to define it somewhere? (This is my interpretation from the crash log.)

 

00:00:52]: [string "../mods/industrial/modmain.lua"]:59: attempt to index global 'ACTIONS' (a nil value)
LUA ERROR stack traceback:
../mods/industrial/modmain.lua:59 in (local) collector (Lua) <57-61>
inst = 111587 - copperore(LIMBO) (valid:true)
doer = 110988 - willow (valid:true)
target = 108345 - furnace (valid:true)
actions = table: 0CDC4338
scripts/componentactions.lua:920 in (method) CollectActions (Lua) <897-929>
self (valid:true) =
modactioncomponents = table: 3950E598
Transform = Transform (143A47B8)
inlimbo = true
parent = 110988 - willow (valid:true)
event_listening = table: 3950C770
actioncomponents = table: 3950A290
lower_components_shadow = table: 3950A330
entity = Entity (390E90F0)
AnimState = AnimState (143A47D8)
prefab = copperore
Physics = Physics (143A47F8)
event_listeners = table: 3950C450
spawntime = 0.033333335071802
name = Copper Ore
replica = table: 3950A470
GUID = 111587
components = table: 3950A358
persists = true
type = USEITEM
arg = nil
t = table: 0D4628A0
modname = industrial
modtable = table: 3950E6B0
i = 1
v = 1
collector = function - ../mods/industrial/modmain.lua:57
scripts/components/playeractionpicker.lua:83 in (method) GetUseItemActions (Lua) <80-86>
self =
map = Map (2793B478)
containers = table: 2C4C6068
inst = 110988 - willow (valid:true)
actionfilter = function - scripts/prefabs/player_common.lua:475
target = 108345 - furnace (valid:true)
useitem = 111587 - copperore(LIMBO) (valid:true)
right = true
actions = table: 0CDC4338
scripts/components/playercontroller.lua:2495 in (method) GetItemUseAction (Lua) <2486-2503>
self =
predictionsent = false
controller_target_age = 0
draggingonground = false
handler = table: 39A93D30
highlight_guy = 108345 - furnace (valid:true)
inst = 110988 - willow (valid:true)
remote_vector = (0.00, 0.00, 0.00)
classified = 110989 - player_classified (valid:true)
controller_target = 108345 - furnace (valid:true)
controller_attack_target = 111947 - robin (valid:true)
map = Map (2793B478)
directwalking = true
locomotor = table: 1F6E1338
time_direct_walking = 0.066666670143604
ismastersim = true
predictwalking = false
mousetimeout = 10
dragwalking = false
deploy_mode = false
remote_controls = table: 2C509A78
active_item = 111587 - copperore(LIMBO) (valid:true)
target = 108345 - furnace (valid:true)
scripts/widgets/inventorybar.lua:643 in (method) UpdateCursorText (Lua) <601-775>
self =
hudcompass = Hud Compass
shown = true
actionstringtitle = Text - Copper Ore
cursor = Image - images/hud.xml:slot_select.tex
rebuild_pending = false
bg = Image - images/hud.xml:inventory_bg.tex
enabled = true
in_pos = (0.00, 102.00, 0.00)
actionstringtime = 8.116608787328
root = root
bgcover = Image - images/hud.xml:inventory_bg_cover.tex
inv = table: 39C2AAA0
name = Inventory
focus_flow = table: 2C656A20
toprow = toprow
out_pos = (0.00, 68.00, 0.00)
autoanchor = WorldResetTimer
callbacks = table: 2C655D50
inst = 111055 - (valid:true)
focus = false
equip = table: 39C2AAF0
children = table: 2C655AD0
current_list = table: 39C2AAA0
focus_flow_args = table: 2C656BB0
focus_target = false
reps = 0
selected_scale = 0.8
openhint = Text - Š
owner = 110988 - willow (valid:true)
actionstring = actionstring
bottomrow = toprow
equipslotinfo = table: 2C656F98
parent = bottom_scale_root
base_scale = 0.6
active_slot = ItemSlot
backpackinv = table: 39C2AB18
hint_update_check = 0.51661975495517
repeat_time = 0
actionstringbody = Text - Ž Inspect
‹ Drop
inv_item = 111587 - copperore(LIMBO) (valid:true)
active_item = nil
controller_id = 17
is_e
[00:00:52]: [string "../mods/industrial/modmain.lua"]:59: attempt to index global 'ACTIONS' (a nil value)
LUA ERROR stack traceback:
../mods/industrial/modmain.lua:59 in (local) collector (Lua) <57-61>
scripts/componentactions.lua:920 in (method) CollectActions (Lua) <897-929>
scripts/components/playeractionpicker.lua:83 in (method) GetUseItemActions (Lua) <80-86>
scripts/components/playercontroller.lua:2495 in (method) GetItemUseAction (Lua) <2486-2503>
scripts/widgets/inventorybar.lua:643 in (method) UpdateCursorText (Lua) <601-775>
scripts/widgets/inventorybar.lua:859 in (method) UpdateCursor (Lua) <787-860>
scripts/widgets/inventorybar.lua:322 in (method) OnUpdate (Lua) <271-352>
scripts/frontend.lua:594 in (method) Update (Lua) <460-609>
scripts/update.lua:93 in () ? (Lua) <39-123>

[00:00:52]: SCRIPT ERROR! Showing error screen
[00:00:53]: stack traceback:
scripts/widgets/widget.lua:276 in (method) SetPosition (Lua) <271-280>
scripts/widgets/followtext.lua:45 in (method) OnUpdate (Lua) <37-47>
scripts/frontend.lua:594 in (method) Update (Lua) <460-609>
scripts/frontend.lua:642 in (method) PushScreen (Lua) <620-647>
scripts/frontend.lua:753 in (method) ShowScreen (Lua) <750-755>
scripts/frontend.lua:808 in (method) DisplayError (Lua) <804-817>
scripts/mainfunctions.lua:931 in () ? (Lua) <909-964>
[00:00:58]: Force aborting...

 

and here's line 59 of my modmain.lua:

57: local smeltable = function(inst, doer, target, actions)58:             if target:HasTag("smelter") then59:                 table.insert(actions, ACTIONS.SMELT)60:             end61:         end62: AddComponentAction("USEITEM", "smeltable", smeltable)

Seemingly, lua can't find ACTIONS.SMELT, do I need to define it somewhere?

You're right, I didn't notice you hadn't had it in the first place. That's how you do it:

AddAction("SMELT", "Smelt", function(act)    if act.target.components.smelter ~= nil then        local smelt_pos = act.target:GetPosition()        local ingredient = act.doer.components.inventory:RemoveItem(act.invobject)        local product = act.target.components.smelter:SmeltItem(ingredient, act.doer)        if product ~= nil then            act.doer.components.inventory:GiveItem(product, nil, smelt_pos)            return true        elseif ingredient:IsValid() then            act.doer.components.inventory:GiveItem(ingredient, nil, smelt_pos)        end        return false    endend)

Should work, but you know what to do if there are any problems.

Okay, right now it gives me the option to smelt, but when I press left arrow to smelt(X-Box controller), it still doesn't do the action (I tried to make it print out strings to follow the exact progress, but it seems the action isnt even being activated.)

 

Here's my current modmain.lua:

SpawnPrefab = GLOBAL.SpawnPrefabRECIPETABS = GLOBAL.RECIPETABSTECH = GLOBAL.TECH-- Points to files in "scripts/prefabs"PrefabFiles = {	"copperingot",	"copperore",	"ironingot",	"ironore",	"tiningot",	"tinore",	"furnace"	}	-- Structures minimap iconAddMinimapAtlas("images/inventoryimages/furnace.xml")-- Recipes	local myrecipe = AddRecipe("furnace", -- name{Ingredient("boards", 2), Ingredient("rocks", 10)}, -- ingredientsRECIPETABS.LIGHT, -- tabTECH.SCIENCE_ONE, -- science level"furnace_placer", -- placernil, -- min_spacingnil, -- nounlocknil, -- numtogivenil, -- builder_tag"images/inventoryimages/furnace.xml", -- atlas"furnace.tex")-- Adding ore drops to the rocks.	function Loot_rock1(inst)		inst.components.lootdropper:AddChanceLoot("copperore", 0.2)		inst.components.lootdropper:AddChanceLoot("ironore", 0.4)		inst.components.lootdropper:AddChanceLoot("tinore", 0.15)endAddPrefabPostInit("rock1", Loot_rock1)function Loot_rock2(inst)		inst.components.lootdropper:AddChanceLoot("copperore", 0.15)		inst.components.lootdropper:AddChanceLoot("ironore", 0.2)		inst.components.lootdropper:AddChanceLoot("tinore", 0.4)endAddPrefabPostInit("rock2", Loot_rock2)function Loot_rock_flint(inst)		inst.components.lootdropper:AddChanceLoot("copperore", 0.4)		inst.components.lootdropper:AddChanceLoot("ironore", 0.15)		inst.components.lootdropper:AddChanceLoot("tinore", 0.2)endAddPrefabPostInit("rock_flintless", Loot_rock_flint)-- Making sure you can smelt the smeltable itemsAddAction("SMELT", "Smelt", function(act)     if act.target.components.smelter ~= nil then         local smelt_pos = act.target:GetPosition()        local ingredient = act.doer.components.inventory:RemoveItem(act.invobject)         local product = act.target.components.smelter:SmeltItem(ingredient, act.doer)        if product ~= nil then            act.doer.components.inventory:GiveItem(product, nil, smelt_pos)            return true        elseif ingredient:IsValid() then            act.doer.components.inventory:GiveItem(ingredient, nil, smelt_pos)        end        return false     endend)local smeltable = function(inst, doer, target, actions)            if target:HasTag("smelter") then                table.insert(actions, GLOBAL.ACTIONS.SMELT)            end        endAddComponentAction("USEITEM", "smeltable", smeltable)

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