deadlyreg Posted December 15, 2016 Share Posted December 15, 2016 I'm trying to modify the code for the corn prefab, so that instead of only getting one popcorn for cooking it, you get three. I've cobbled together some code but it doesn't seem to be working. I'm having trouble working out the trigger for the item spawn so that it only goes off when it's added to the inventory from cooking and not any other reason. This is the code I have local function popcorn(prefab) if self.oncooked ~= nil then self.oncooked(self.inst, cooker, chef) end if self.product ~= nil then local poppedcorn = SpawnPrefab("corn_cooked") popcorn.components.stackable.stacksize = 2 if poppedcorn ~= nil then if owner.components.inventory and not owner.components.inventory:IsFull() then owner.components.inventory:GiveItem(poppedcorncorn) elseif owner.components.container and not owner.components.container:IsFull() then owner.components.container:GiveItem(poppedcorncorn) else inst.components.lootdropper:DropLootPrefab(poppedcorncorn) end return poppedcorn end end end AddPrefabPostInit("corn", popcorn) It's cobbled together from the coconut code from Shipwrecked and the cooking code from the cookable component. Am I just not calling things right? Any help is appreciated. Link to comment Share on other sites More sharing options...
Aquaterion Posted December 15, 2016 Share Posted December 15, 2016 (edited) local function onpopcorn(inst) if not GLOBAL.TheWorld.ismastersim then return inst end inst.components.cookable:SetOnCookedFn(function(inst, cooker, chef) local popcorn = GLOBAL.SpawnPrefab("corn_cooked") popcorn.components.stackable.stacksize = 2 chef.components.inventory:GiveItem(popcorn) end) end AddPrefabPostInit("corn", onpopcorn) Edited December 15, 2016 by Aquaterion Link to comment Share on other sites More sharing options...
deadlyreg Posted December 16, 2016 Author Share Posted December 16, 2016 13 hours ago, Aquaterion said: local function onpopcorn(inst) if not GLOBAL.TheWorld.ismastersim then return inst end inst.components.cookable:SetOnCookedFn(function(inst, cooker, chef) local popcorn = GLOBAL.SpawnPrefab("corn_cooked") popcorn.components.stackable.stacksize = 2 chef.components.inventory:GiveItem(popcorn) end) end AddPrefabPostInit("corn", onpopcorn) That fixed it, thank you! I was wondering though, I'm still really new to this modding thing and I while I've read rezecib's guide and looked through the api examples I still can't quite get my head around it. If I tell you how I put my non-functioning code together, could you tell me where my incorrect thinking was and how you came to put together yours? First I looked at the cookable.lua to see how food gets turned into cooked food and found function Cookable:Cook(cooker, chef) and saw if self.oncooked ~= nil then if self.oncooked ~= nil then self.oncooked(self.inst, cooker, chef) end if self.product ~= nil then local prod = SpawnPrefab( type(self.product) ~= "function" and self.product or self.product(self.inst, cooker, chef) ) so I thought, okay, this is the code the game uses to spawn the cooked item. So it must trigger when the food is cooked, just like I want. So if I replace the code for SpawnPrefab(self.product) with just "corn_cooked" I should spawn even more of those items when corn specifically is cooked if I added it to the prefab. Then I went hunting for how Shipwrecked handled coconuts and saw this local function onhacked(inst) local nut = inst [machete hacking code] if owner then local hacked = SpawnPrefab("coconut_halved") hacked.components.stackable.stacksize = 2 if owner.components.inventory and not owner.components.inventory:IsFull() then owner.components.inventory:GiveItem(hacked) elseif owner.components.container and not owner.components.container:IsFull() then owner.components.container:GiveItem(hacked) else inst.components.lootdropper:DropLootPrefab(hacked) end else inst.components.lootdropper:SpawnLootPrefab("coconut_halved") inst.components.lootdropper:SpawnLootPrefab("coconut_halved") end inst.SoundEmitter:PlaySound("dontstarve_DLC002/common/bamboo_hack") end nut:Remove() end So okay, here's a method the game uses to spawn items on an action. Fantastic, just what I need! Just change hacked to poppedcorn and the prefab to "corn_cooked" and it should work fine. The GiveItem code and the SpawnLootPrefab seem to do what I want, so I can't see why it shouldn't work for me. Except it doesn't. Any advice would be appreciated. There's a lot to this I know, and I can often see what it is I want to change but I can't quite work out how to do it. I've been going through the code to see if I can work out the triggers and functions and the like but sometimes 14 hours ago, Aquaterion said: inst.components.cookable:SetOnCookedFn(function(inst, cooker, chef) I can understand how this works but I can't see how you reached it. If you have the time or can point me at some resource, I would be thankful. Link to comment Share on other sites More sharing options...
Aquaterion Posted December 16, 2016 Share Posted December 16, 2016 (edited) I'll take your first code and try to explain what was wrong and stuff i'll put it in an image so it has some colors to make it more readable (click on it to enlarge) Note: this code isn't function due to leaving the wrong parts in it. Edited December 16, 2016 by Aquaterion Link to comment Share on other sites More sharing options...
deadlyreg Posted December 16, 2016 Author Share Posted December 16, 2016 That's extremely helpful, thank you. So when a function is written, the values in the brackets, inst, prefab, self etc. don't have inherent values to them but only values as assigned to them by the function? 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