Jump to content

Recommended Posts

Hey guys

 

So I want to make a consumable item that has a temporally effect like...loosing sanity for 10 sec or something...

 

I was thinking about using the world clock somehow, like eating the item triggers a worldclock tracking event or something like that? Or is there a similar mod? Cause I searched high and low but couldn't find anything similar yet I would be suprised if nobody have made one before

 

Thanks

Yumiko

@KawaiiYumiko

You could use DoPeriodicTask and call a function that decreases your sanity every second, like so:
 

AddPrefabPostInit("carrot", function(inst)	local function LoseSanity(eater, ini_time)			if eater.components.sanity then			eater.components.sanity:DoDelta(-1)		end				if GLOBAL.GetTime() > ini_time + 10 then			eater.lose_sanity_task:Cancel()			eater.lose_sanity_task = nil		end	end		local function OnEaten(inst, eater)			if eater.lose_sanity_task ~= nil then eater.lose_sanity_task:Cancel() end		eater.lose_sanity_task = nil				eater.lose_sanity_task = eater:DoPeriodicTask(1, LoseSanity, nil, GLOBAL.GetTime())	end	    inst.components.edible:SetOnEatenFn(OnEaten)end)

Now eating a carrot will make your sanity go down by 1 every second for 10 seconds.

Thx a lot I was thinking about something similar, one question tho..what's the purpose of the OnEaten function what does it do?

 

	local function OnEaten(inst, eater)			if eater.lose_sanity_task ~= nil then eater.lose_sanity_task:Cancel() end		eater.lose_sanity_task = nil				eater.lose_sanity_task = eater:DoPeriodicTask(1, LoseSanity, nil, GLOBAL.GetTime())	end	

 

what's the purpose of the OnEaten function what does it do?

 

The function OnEaten, which could also be named WhateverYouWantToNameIt, is setup to be the function called when the item with the edible component is eaten.

 

Players have the eater component. And when they eat, they run the Eat function of it.

That function has this:

        if food.components.edible ~= nil then            food.components.edible:OnEaten(self.inst)        end

If the thing eaten has an edible component (which should), then it's OnEaten function is ran.

What's inside that function?

    if self.oneaten then        self.oneaten(self.inst, eater)    end

So if there is an oneaten function assigned, it gets executed with the food and the eater parameters.

How do we setup the oneaten of the edible component it references (self)?

function Edible:SetOnEatenFn(fn)    self.oneaten = fnend

The OnEaten function you were given:

- if there is a lose_sanity_task, then cancel it (it doesn't run anymore)

- make lose_sanity_task = nil, to clean it up

- assign a periodic task to lose_sanity_task with the parameters

function EntityScript:DoPeriodicTask(time, fn, initialdelay, ...)

* time (1), which is the interval between executions

* fn (LoseSanity), which is the function executed between executions

* initialdelay (nil), which is the initialdelay before starting

* (...) all the arguments you want to pass to the fn you are going to use

- in this case, GLOBAL.GetTime()

(GLOBAL because we are in modmain, the mod environment)

(GetTime() returns the time passed since world creation)

 

LoseSanity has (inst, ini_time):

* inst is passed because of how DoPeriodicTask sets up fn to be called

* ini_time is the (...) passed

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