Jump to content

Need help with AddComponentPostInit - component acts incorrectly


Recommended Posts

I'm trying to edit function for component "stewer" via AddComponentPostInit and it all works except one issue and I can't find a way to fix it. The issue is cookpot (which component I edit) preparing food infinitely. Stopping and relogging into server gives prepared food. I completely removed my changes and problem still persists. 
Here is the code (almost the same as the one in original component):
 

AddComponentPostInit("stewer",function(Stewer)
		function Stewer:StartCooking()
			if self.targettime == nil and self.inst.components.container ~= nil then
				self.done = nil
				self.spoiltime = nil

				if self.onstartcooking ~= nil then
					self.onstartcooking(self.inst)
				end

				local ings = {}         
				for k, v in pairs (self.inst.components.container.slots) do
					table.insert(ings, v.prefab)
				end

				local cooktime = 1
				self.product, cooktime = cooking.CalculateRecipe(self.inst.prefab, ings)
				local productperishtime = cooking.GetRecipe(self.inst.prefab, self.product).perishtime or 0

				if productperishtime > 0 then
					local spoilage_total = 0
					local spoilage_n = 0
					for k, v in pairs (self.inst.components.container.slots) do
						if v.components.perishable ~= nil then
							spoilage_n = spoilage_n + 1
							spoilage_total = spoilage_total + v.components.perishable:GetPercent()
						end
					end
					self.product_spoilage = 1
					if spoilage_total > 0 then
						self.product_spoilage = spoilage_total / spoilage_n
						self.product_spoilage = 1 - (1 - self.product_spoilage) * .5
					end
				else
					self.product_spoilage = nil
				end
						
				cooktime = TUNING.BASE_COOK_TIME * cooktime
				self.targettime = GLOBAL.GetTime() + cooktime --Forced to use GLOBAL huh
				if self.task ~= nil then
					self.task:Cancel()
				end
				self.task = self.inst:DoTaskInTime(cooktime, dostew, self)

				self.inst.components.container:Close()
				self.inst.components.container:DestroyContents()
				self.inst.components.container.canbeopened = false
			end
		end
	end)		

I tried forcing cooktime = 1, but that's not issue.
I completely lost now and hope to get some help there.

edit: I'm doing this in modmain

Edited by NightSparrow
Link to comment
Share on other sites

at the third line and onward, you repeatedly try to use the variable 'self', which does not exist

change the first two lines to this:

AddComponentPostInit("stewer", function(self)
	function self:StartCooking()

 

Edited by JohnWatson
Link to comment
Share on other sites

Okay I found the source of problem is function "dostew" in
 self.task = self.inst:DoTaskInTime(cooktime, dostew, self)
which somehow doesn't trigger or cause any error.

Currently examining possible ways to fix it and I hope there is way other than replacing this function with new one in modmain
edit: is there a way other than replacing dostew with function() copypaste? I can't find a workaround...

Edited by NightSparrow
Link to comment
Share on other sites

On 10/13/2018 at 1:32 AM, NightSparrow said:

Okay I found the source of problem is function "dostew" in
 self.task = self.inst:DoTaskInTime(cooktime, dostew, self)
which somehow doesn't trigger or cause any error.

Currently examining possible ways to fix it and I hope there is way other than replacing this function with new one in modmain
edit: is there a way other than replacing dostew with function() copypaste? I can't find a workaround...

I don't think you can find a workaround. You do not have access to the local function dostew from your modmain.lua file.

I don't know what you want to change in the StartCooking function, but if you just want to add something, you could always do it like this:

local stewer = GLOBAL.require("components/stewer")
local old_fn = stewer.StartCooking
function stewer.StartCooking()
  -- Your code here
  old_fn()
  -- Your code here
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...