Jump to content

[Coding help] Lure plant adjustable digestion gone wrong


Recommended Posts

I've been trying to add an adjustable digestion timer for the lure plant, but can only get it to work on a static timer. With what I have it at now, it gives a scheduler error.

My config options in modinfo.lua

configuration_options ={	{		name = "timer",		label = "Digestion Time",		options =		{			{description = "20 Secs.", data = "20"},			{description = "1 Minute", data = "60"},			{description = "2 Minutes", data = "120"},			{description = "3 Minutes", data = "180"},			{description = "4 Minutes", data = "240"},			{description = "5 Minutes", data = "300"},			{description = "10 Minutes", data = "600"},			{description = "15 Minutes", data = "900"},			{description = "20 Minutes", data = "1200"},			{description = "25 Minutes", data = "1500"},			{description = "30 Minutes", data = "1800"},		},		default = "20",	},}

 

My modmain.lua

Prefabs = {	prefab("lureplant"),}if GetModConfigData("timer")==20 then	local newdigest = 20elseif GetModConfigData("timer")==60 then	local newdigest = 60elseif GetModConfigData("timer")==120 then	local newdigest = 120elseif GetModConfigData("timer")==180 then	local newdigest = 180elseif GetModConfigData("timer")==240 then	local newdigest = 240elseif GetModConfigData("timer")==300 then	local newdigest = 300elseif GetModConfigData("timer")==600 then	local newdigest = 600elseif GetModConfigData("timer")==900 then	local newdigest = 900elseif GetModConfigData("timer")==1200 then	local newdigest = 1200elseif GetModConfigData("timer")==1500 then	local newdigest = 1500elseif GetModConfigData("timer")==1800 then	local newdigest = 1800endlocal function Digest(prefab)	prefab.components.digester.digesttime = newdigestend	AddPrefabPostInit("lureplant", Digest)

 

And the error log

[00:04:14]: [string "scripts/scheduler.lua"]:291: attempt to perform arithmetic on local 'dt' (a nil value)
LUA ERROR stack traceback:
scripts/scheduler.lua:291 in (method) GetListForTimeFromNow (Lua) <289-302>
self =
running = table: 0CC20BF0
waitingfortick = table: 0CC20BC8
tasks = table: 0CC20B50
waking = table: 0D89F480
attime = table: 0CC20C68
hibernating = table: 0CC20C40
dt = nil
nowtick = 2283
scripts/scheduler.lua:306 in (method) ExecutePeriodic (Lua) <304-310>
self =
running = table: 0CC20BF0
waitingfortick = table: 0CC20BC8
tasks = table: 0CC20B50
waking = table: 0D89F480
attime = table: 0CC20C68
hibernating = table: 0CC20C40
period = nil
fn = function - scripts/components/digester.lua:8
limit = nil
initialdelay = nil
id = 112463
arg = nil
periodic = *** failed to evaluate ***
scripts/entityscript.lua:993 in (method) DoPeriodicTask (Lua) <990-1002>
self (valid:true) =
GUID = 112463
inlimbo = false
brain = --brain--
sleep time: 0.00
Priority - READY ()>0.00
>ControlMinions - READY ()>0.00

OnLongUpdate = function - scripts/prefabs/lureplant.lua:214
AnimState = AnimState (0C6A5538)
task = PERIODIC 112463: 1.000000
Network = Network (0C6A45B8)
pendingtasks = table: 153D85A0
sg = sg="lureplant", state="idlein", time=45.23, tags = "hiding,idle,vine,"
spawntime = 30.866668276489
Transform = Transform (0C6A4778)
actionreplica = table: 0D87A038
event_listening = table: 36AD7908
actioncomponents = table: 0D87D170
entity = Entity (1D24B418)
prefab = lureplant
OnEntitySleep = function - scripts/prefabs/lureplant.lua:255
OnSave = function - scripts/prefabs/lureplant.lua:208
children = table: 14B76D00
OnLoad = function - scripts/prefabs/lureplant.lua:200
lurefn = function - scripts/prefabs/lureplant.lua:164
MiniMapEntity = MiniMapEntity (0C6A5638)
event_listeners = table: 36AD9500
OnEntityWake = function - scripts/prefabs/lureplant.lua:250
ListenForWinter = PERIODIC 112463: 30.000000
name = Meat Bulb
persists = true
brainfn = table: 1D803BC0
replica = table: 0D878850
SoundEmitter = SoundEmitter (0C6A4678)
components = table: 0D876438
Physics = Physics (0C6A3F78)
time = nil
fn = function - scripts/components/digester.lua:8
initialdelay = nil
arg = nil
scripts/components/digester.lua:8 in (local) fn (Lua) <6-10>
scripts/entityscript.lua:880 in (method) PushEvent (Lua) <874-897>
self (valid:true) =
GUID = 112463
inlimbo = false
brain = --brain--
sleep time: 0.00
Priority - READY ()>0.00
>ControlMinions - READY ()>0.00

OnLongUpdate = function - scripts/prefabs/lureplant.lua:214
AnimState = AnimState (0C6A5538)
task = PERIODIC 112463: 1.000000
Network = Network (0C6A45B8)
pendingtasks = table: 153D85A0
sg = sg="lureplant", state="idlein", time=45.23, tags = "hiding,idle,vine,"
spawntime = 30.866668276489
Transform = Transform (0C6A4778)
actionreplica = table: 0D87A038
event_listening = table: 36AD7908
actioncomponents = table: 0D87D170
entity = Entity (1D24B418)
prefab = lureplant
OnEntitySleep = function - scripts/prefabs/lureplant.lua:255
OnSave = function - scripts/prefabs/lureplant.lua:208
children = table: 14B76D00
OnLoad = function - scripts/prefabs/lureplant.lua:200
lurefn = function - scripts/prefabs/lureplant.lua:164
MiniMapEntity = MiniMapEntity (0C6A5638)
event_listeners = table: 36AD9500
OnEntityWake = function - scripts/prefabs/lureplant.lua:250
ListenForWinter = PERIODIC 112463: 30.000000
name = Meat Bulb
persists = true
brainfn = table: 1D803BC0
replica = table: 0D878850
SoundEmitter = SoundEmitter (0C6A4678)
components = table: 0D876438
Physics = Physics (0C6A3F78)
event =
[00:04:14]: SCRIPT ERROR! Showing error screen
[00:04:16]: QueryServerComplete no callback
[00:04:16]: QueryServerComplete no callback
[00:04:20]: Force aborting...

 

I'm very new to this and any help is greatly appreciated

Link to comment
Share on other sites

I cut down the code in modmain.lua, and print returns the desired value, but it's still digesting at the default 20 seconds. This is the update modmain.lua

local function Digestionist(prefab)	prefab.newdigest = GetModConfigData("timer")	prefab.components.digester.digesttime = prefab.newdigest	print(prefab.components.digester.digesttime)endAddPrefabPostInit("lureplant", Digestionist)
Link to comment
Share on other sites

@greatguys, here is why it doesn't work.

 

self.task = self.inst:DoPeriodicTask(self.digesttime, function() self:TryDigest() end) self.inst:ListenForEvent("gotnewitem", function() 	if not self.task then 		self.task = self.inst:DoPeriodicTask(self.digesttime, function() self:TryDigest() end) 	end end)	 

 

 

You can cancel a task with task:Cancel(). You will need to cancel the digester.task and create a new one with your new timer.

Link to comment
Share on other sites

@greatguys, here is why it doesn't work.

 

self.task = self.inst:DoPeriodicTask(self.digesttime, function() self:TryDigest() end) self.inst:ListenForEvent("gotnewitem", function() 	if not self.task then 		self.task = self.inst:DoPeriodicTask(self.digesttime, function() self:TryDigest() end) 	end end)	 

 

 

You can cancel a task with task:Cancel(). You will need to cancel the digester.task and create a new one with your new timer.

 

Just did this and-

Works flawlessly.

Thank you!

Link to comment
Share on other sites

I tried to elaborate on the mod a little more by adding a max inventory option to it, however, it isn't going to well. After the lure plant eats something, it crashes giving this error:

post-578579-0-17947200-1426658452_thumb.

 

My modmain.lua is now this:

local function Digestionist(prefab)	if not GLOBAL.TheWorld.ismastersim then		return inst	end 	-- Hunger timer    prefab.newdigest = GetModConfigData("timer")    prefab.components.digester.digesttime = prefab.newdigest	prefab.components.digester.task:Cancel()	prefab.components.digester.task = prefab.components.digester.inst:DoPeriodicTask(prefab.components.digester.digesttime, function() prefab.components.digester:TryDigest() end) 	-- Inventory mod	prefab:AddComponent("inventory")	prefab.components.inventory.maxslots = GetModConfigData("items")	print("Digest time is",prefab.components.digester.digesttime) 	print("Inventory slots are",prefab.components.inventory.maxslots)endAddPrefabPostInit("lureplant", Digestionist)

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