Jump to content

Code problem


Recommended Posts

Hello! i have this problem with this code. I had to frankenstein this, so i expected problems.

the code is 

-- Spawn poop when sanity is low 
local function OnSanityDelta(inst, data)
	if data.newpercent == .20 then 
		inst.components.talker:Say("AHH! I just saw something just jumped at me!")
		SpawnPrefab("poop").Transform:SetPosition(inst.Transform:GetWorldPosition())

	end
end

Before you say it. yes, it is a silly piece of code.

anyway, the problem. this code only seems to work when i set my sanity to .20 percent with the console command "c_setsanity(0.20)". if i set my sanity to .21 and let it naturally fall down, then the code doesn't run. if i change the code to this

-- Spawn poop when sanity is low
local function OnSanityDelta(inst, data)
	if data.newpercent <= .20 then 
		inst.components.talker:Say("AHH! I just saw something just jumped at me!")
		SpawnPrefab("poop").Transform:SetPosition(inst.Transform:GetWorldPosition())

	end
end

Then at .20, he starts dropping poop each tick. 

in the end, all i want is at .20 sanity my character says that and drops a single poop.

Thanks in advance 

Link to comment
Share on other sites

I took this code from Player Poop mod, so hope this code will help you/

 

inst.lastpooptime = nil -- put this to master_postinit

local function TimeSinceLastPoop()
    if inst.lastpooptime ~= nil then --checks if lastpooptime is not nil, which means you have pooped sometime before
        return GetTime() - inst.lastpooptime				
    end
end

local function OnSanityDelta(inst, data)
    if data.newpercent <= .20 then 
      	if inst.lastpooptime == nil or TimeSinceLastPoop() > TUNING.SEG_TIME*4 then
            inst.lastpooptime = GetTime()
            inst.components.talker:Say("AHH! I just saw something just jumped at me!")
            SpawnPrefab("poop").Transform:SetPosition(inst.Transform:GetWorldPosition())
        end
    end
end

 

Link to comment
Share on other sites

local function TimeSinceLastPoop(inst)
    if inst.lastpooptime ~= nil then --checks if lastpooptime is not nil, which means you have pooped sometime before
        return GetTime() - inst.lastpooptime				
    end
	return 0
end
 
 
-- Spawn poop when sanity is low (basicly crap your pants)
local function OnSanityDelta(inst, data)
    if data.newpercent <= .20 then 
      	if inst.lastpooptime == nil or TimeSinceLastPoop(inst) > TUNING.SEG_TIME*4 then
            inst.lastpooptime = GetTime()
            inst.components.talker:Say("AHH! I just saw something just jumped at me!")
            SpawnPrefab("poop").Transform:SetPosition(inst.Transform:GetWorldPosition())
        end
    end
end
local master_postinit = function(inst)
	inst.lastpooptime = nil
	inst:ListenForEvent("sanitydelta", OnSanityDelta)
end

Tested.

Link to comment
Share on other sites

It works! Thanks for coming through with me again! you're a hero!

EDIT: one question. how long does the game take before it allows another poop to happen? and what would i do to make it shorter or longer

Edited by Augustusc
because a question came up
Link to comment
Share on other sites

4 hours ago, Augustusc said:

It works! Thanks for coming through with me again! you're a hero!

EDIT: one question. how long does the game take before it allows another poop to happen? and what would i do to make it shorter or longer

You can print(TimeLastSincePoop(inst)) when TimeLastSincePoop(inst) = 120 Its will poop

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