Lumina Posted November 26, 2017 Share Posted November 26, 2017 Hi, I have an item allowing trading of food for another food item, kinda similar to the birdcage. At the moment, if i give a food, i receive a totally fresh food. I would like to keep the state of the food, like, if i give a spoiled item, i obtain a spoiled item in return. At least partially, i don't mind if it's not exactly the same value and/or if there is a little refresh, i just want to avoid receiving a green food when i give a red one. Is it possible, and if yes, how ? Link to comment Share on other sites More sharing options...
spideswine Posted November 27, 2017 Share Posted November 27, 2017 what does .components.trader.onaccept of the thing you are giving the item to look like? Link to comment Share on other sites More sharing options...
Lumina Posted November 27, 2017 Author Share Posted November 27, 2017 @spideswine Entire prefab (sorry if it's not a clean code) require "prefabutil" local assets = { Asset("ANIM", "anim/press.zip"), } local prefabs = { "raisin_grape", "carrot", -- "berries", "medicinal_root", "alcohol", "succulent_picked", "aloevera", "carrot_juice", "root_juice", "collapse_small", --helianti } local function GiveItem(inst, item) if item.prefab == "raisin_grape" then inst.components.lootdropper:SpawnLootPrefab("alcohol") end -- if item.prefab == "berries" then -- inst.components.lootdropper:SpawnLootPrefab("alcohol") -- end if item.prefab == "medicinal_root" then inst.components.lootdropper:SpawnLootPrefab("root_juice") end if item.prefab == "carrot" then inst.components.lootdropper:SpawnLootPrefab("carrot_juice") end if item.prefab == "succulent_picked" then inst.components.lootdropper:SpawnLootPrefab("aloevera") end -- else -- if item.prefab == "helianti_flower" then -- inst.components.lootdropper:SpawnLootPrefab("helianti_oil") -- end end local function OnGetItemFromPlayer(inst, giver, item) if item.prefab == "raisin_grape" -- or item.prefab == "berries" or item.prefab == "carrot" or item.prefab == "medicinal_root" or item.prefab == "succulent_picked" -- or item.prefab == "helianti_flower" then inst.AnimState:PlayAnimation("pressing", true) inst:DoTaskInTime(20/30, GiveItem, item) inst.AnimState:PushAnimation("idle", true) end end local function OnRefuseItem(inst, giver, item) inst.AnimState:PushAnimation("idle", true) end local function CouldPress(inst, item) local can_accept = item.prefab == "raisin_grape" -- or item.prefab == "berries" or item.prefab == "carrot" or item.prefab == "medicinal_root" or item.prefab == "succulent_picked" -- or item.prefab == "helianti_flower" return can_accept end local function OnBuilt(inst) inst.AnimState:PlayAnimation("pressing", true) inst.AnimState:PushAnimation("idle", true) end local function OnSave(inst, data) end local function OnLoad(inst, data) inst.AnimState:PlayAnimation("pressing", true) inst.AnimState:PushAnimation("idle", true) end local function OnHammered(inst, worker) -- if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() then -- inst.components.burnable:Extinguish() -- end inst.components.lootdropper:DropLoot() local fx = SpawnPrefab("collapse_small") fx.Transform:SetPosition(inst.Transform:GetWorldPosition()) fx:SetMaterial("wood") inst:Remove() end local function OnHit(inst) if not inst:HasTag("burnt") then inst.AnimState:PlayAnimation("pressing") inst.AnimState:PushAnimation("idle", true) end end local function pressfn() local inst = CreateEntity() inst.entity:AddTransform() inst.entity:AddAnimState() inst.entity:AddSoundEmitter() inst.entity:AddMiniMapEntity() inst.entity:AddNetwork() MakeObstaclePhysics(inst, .5) inst:AddTag("structure") inst.MiniMapEntity:SetIcon("press.tex") inst.AnimState:SetBank("press") inst.AnimState:SetBuild("press") inst.AnimState:PlayAnimation("idle", true) --trader (from trader component) added to pristine state for optimization inst:AddTag("trader") MakeSnowCoveredPristine(inst) inst.entity:SetPristine() if not TheWorld.ismastersim then return inst end inst:AddComponent("inspectable") inst:AddComponent("lootdropper") inst:AddComponent("workable") inst.components.workable:SetWorkAction(ACTIONS.HAMMER) inst.components.workable:SetWorkLeft(2) inst.components.workable:SetOnFinishCallback(OnHammered) inst.components.workable:SetOnWorkCallback(OnHit) -- MakeLargeBurnable(inst, nil, nil, true) -- MakeLargePropagator(inst) inst:AddComponent("trader") inst:ListenForEvent("onbuilt", OnBuilt) MakeSnowCovered(inst) inst.OnSave = OnSave inst.OnLoad = OnLoad inst.components.trader:SetAcceptTest(CouldPress) inst.components.trader.onaccept = OnGetItemFromPlayer inst.components.trader.onrefuse = OnRefuseItem return inst end return Prefab("press", pressfn, assets, prefabs), MakePlacer( "common/press_placer", "press", "press", "idle" ) This is the part you ask if i'm not wrong : local function OnGetItemFromPlayer(inst, giver, item) if item.prefab == "raisin_grape" -- or item.prefab == "berries" or item.prefab == "carrot" or item.prefab == "medicinal_root" or item.prefab == "succulent_picked" -- or item.prefab == "helianti_flower" then inst.AnimState:PlayAnimation("pressing", true) inst:DoTaskInTime(20/30, GiveItem, item) inst.AnimState:PushAnimation("idle", true) end end Link to comment Share on other sites More sharing options...
spideswine Posted November 27, 2017 Share Posted November 27, 2017 well you're gonna want to change GiveItem, try this(I'm not sure how spawnlootprefab works): local function GiveItem(inst, item) local drop= nil if item.prefab == "raisin_grape" then drop=inst.components.lootdropper:SpawnLootPrefab("alcohol") end -- if item.prefab == "berries" then -- inst.components.lootdropper:SpawnLootPrefab("alcohol") -- end if item.prefab == "medicinal_root" then drop=inst.components.lootdropper:SpawnLootPrefab("root_juice") end if item.prefab == "carrot" then drop=inst.components.lootdropper:SpawnLootPrefab("carrot_juice") end if item.prefab == "succulent_picked" then drop=inst.components.lootdropper:SpawnLootPrefab("aloevera") end -- else -- if item.prefab == "helianti_flower" then -- inst.components.lootdropper:SpawnLootPrefab("helianti_oil") -- end if drop then if item.components.perishable and drop.components.perishable then drop.components.perishable.perishremainingtime=item.components.perishable.perishremainingtime end end end Link to comment Share on other sites More sharing options...
Lumina Posted November 27, 2017 Author Share Posted November 27, 2017 (edited) The code seems to work fine, except that depending of the item, i either obtain a fresh food (like, if i press a fresh carrot > fresh carrot juice), or an already stale food (if i press a fresh grape > stale alcohol). My guess is that carrot perish time is longer than carrot juice (no loss), when grape perish time is a lot shorter than alcohol. Is there a way to use percent rather than raw value ? (If this is the reason, of course) Edited November 27, 2017 by Lumina Link to comment Share on other sites More sharing options...
spideswine Posted November 27, 2017 Share Posted November 27, 2017 (edited) change drop.components.perishable.perishremainingtime=item.components.perishable.perishremainingtime to drop.components.perishable.perishremainingtime= item.components.perishable.perishremainingtime/item.components.perishable.perishtime*drop.components.perishable.perishtime or maybe drop.components.perishable.perishremainingtime= item.components.perishable:GetPercent()*drop.components.perishable.perishtime will be better Edited November 27, 2017 by spideswine Link to comment Share on other sites More sharing options...
Lumina Posted November 27, 2017 Author Share Posted November 27, 2017 I just tried the second option, it seems to work fine, thanks a lot Link to comment 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