Jump to content

Some codes not working


Recommended Posts

i try to make some codes but they're not working. here is the codes

 

--When kittykit around gain sanity
local function sanitykk(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local delta = 0
	local ents = TheSim:FindEntities(x, y, z, 20, {"critter_kitten"})
	for k, v in pairs(ents) do
		if v ~= inst then
			local bonus_sanity = TUNING.SANITYAURA_SMALL
			local distsq = math.max(inst:GetDistanceSqToInst(v), 1)
			delta = delta + bonus_sanity / distsq
		end
	end
	return delta
end

--When varling around lose sanity
local function sanityvl(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local delta = 0
	local ents = TheSim:FindEntities(x, y, z, 20, {"critter_puppy"})
	for k, v in pairs(ents) do
		if v ~= inst then
			local bonus_sanity = -TUNING.SANITYAURA_MEDIUM
			local distsq = math.max(inst:GetDistanceSqToInst(v), 1)
			delta = delta + bonus_sanity / distsq
		end
	end
	return delta
end

i don't know what is wrong here is master postinit

	inst.components.sanity.custom_rate_fn = sanitykk
	inst.components.sanity.custom_rate_fn = sanityvl

and also this one is not working too.

local function oneat(inst, food)	
if food and food.components.edible and food.prefab  == not "ice" or "fish_cooked" or "fish" or "fishtacos" or "fishsticks" then
		inst.components.health:DoDelta(0)
		inst.components.sanity:DoDelta(0)
		inst.components.hunger:DoDelta(5)
		inst.components.talker:Say("I shouldn't eat that. Nya~")
end

and master postinit

	inst.components.eater:SetOnEatFn(oneat)

there is no error or the game is not closing they just... not working. I don't know how to fix. Did i write wrong or did i miss something?

Link to comment
Share on other sites

when you declare:

	inst.components.sanity.custom_rate_fn = sanitykk
	inst.components.sanity.custom_rate_fn = sanityvl

sanityvl overwrites sanitykk, custom_rate_fn should be treated as one function "slot", it'll then refer to the sanity file in the core game files to handle custom_rate_fn.

A better way to do it would be to combine both functions into one unique function that operates like this:
 

-- return a value to judge for sanitydelta
local function critter_SanityAura(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local delta = 0
	local ents = TheSim:FindEntities(x, y, z, 20, {"critter_puppy", "critter_kitten"})
	local bonus_sanity = nil	-- initialise
	local distsq = nil		-- initialise
    	local totalDelta = nil		-- initialise
	
	for k, v in pairs(ents) do
		if v ~= inst then
			if v.entity == "critter_puppy" then
			-- Lose sanity here
				bonus_sanity = -TUNING.SANITYAURA_MEDIUM
				distsq = math.max(inst:GetDistanceSqToInst(v), 1)
				delta = delta + bonus_sanity / distsq
			elseif v.entity == "critter_kitten" then
			-- Gain sanity here
				bonus_sanity = TUNING.SANITYAURA_SMALL
				distsq = math.max(inst:GetDistanceSqToInst(v), 1)
				delta = delta + bonus_sanity / distsq
			else
			 -- do nothing; I like to have else clauses just in case.
			end
		end
        totalDelta = totalDelta + delta
	end
    
    -- totalDelta will be a collective of all data stored for each instance of critter.
    -- #[arrayname] is length, want to ensure that our totalDelta scales results per critter.
    if #ents > 0 then -- actually you could move this up to just before the for loop (save some processing power).
    	totalDelta = totalDelta / #ents
    else
    	totalDelta = 0
    end
    
	-- whoops, nearly forgot to consider delta changes every loop iteration
	return totalDelta
end

-- [[
	I cannot comment on whether this code will work as it is,
	the logic is how I envision it.
	but I hate abstract terms for "forloops".
	(More of a c++, java guy, learning lua through practice atm.)
]]

-- [[
	A warning, this may need to be refreshed to determine what is
	near you at any one time as it acts like a "scan".
	running this function continuously might slow your/others machine down.
]]

SetOnEatFn() is declaring the function, but I cannot observe how the food parameter reaches that function; I can't really comment too well there.

Edited by MorickClive
edited: forgot to consider delta var reset.
Link to comment
Share on other sites

15 hours ago, MorickClive said:

when you declare:


	inst.components.sanity.custom_rate_fn = sanitykk
	inst.components.sanity.custom_rate_fn = sanityvl

sanityvl overwrites sanitykk, custom_rate_fn should be treated as one function "slot", it'll then refer to the sanity file in the core game files to handle custom_rate_fn.

A better way to do it would be to combine both functions into one unique function that operates like this:
 


-- return a value to judge for sanitydelta
local function critter_SanityAura(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local delta = 0
	local ents = TheSim:FindEntities(x, y, z, 20, {"critter_puppy", "critter_kitten"})
	local bonus_sanity = nil	-- initialise
	local distsq = nil		-- initialise
    	local totalDelta = nil		-- initialise
	
	for k, v in pairs(ents) do
		if v ~= inst then
			if v.entity == "critter_puppy" then
			-- Lose sanity here
				bonus_sanity = -TUNING.SANITYAURA_MEDIUM
				distsq = math.max(inst:GetDistanceSqToInst(v), 1)
				delta = delta + bonus_sanity / distsq
			elseif v.entity == "critter_kitten" then
			-- Gain sanity here
				bonus_sanity = TUNING.SANITYAURA_SMALL
				distsq = math.max(inst:GetDistanceSqToInst(v), 1)
				delta = delta + bonus_sanity / distsq
			else
			 -- do nothing; I like to have else clauses just in case.
			end
		end
        totalDelta = totalDelta + delta
	end
    
    -- totalDelta will be a collective of all data stored for each instance of critter.
    -- #[arrayname] is length, want to ensure that our totalDelta scales results per critter.
    if #ents > 0 then -- actually you could move this up to just before the for loop (save some processing power).
    	totalDelta = totalDelta / #ents
    else
    	totalDelta = 0
    end
    
	-- whoops, nearly forgot to consider delta changes every loop iteration
	return totalDelta
end

-- [[
	I cannot comment on whether this code will work as it is,
	the logic is how I envision it.
	but I hate abstract terms for "forloops".
	(More of a c++, java guy, learning lua through practice atm.)
]]

-- [[
	A warning, this may need to be refreshed to determine what is
	near you at any one time as it acts like a "scan".
	running this function continuously might slow your/others machine down.
]]

SetOnEatFn() is declaring the function, but I cannot observe how the food parameter reaches that function; I can't really comment too well there.

thank you so much for code and information.

 

Link to comment
Share on other sites

About OnEat function the other parts of code works properly so here is the full code

local function oneat(inst, food)	

	if food and food.components.edible and food.prefab == "fishsticks" then
	       inst.components.health:DoDelta(50)
		inst.components.sanity:DoDelta(50)
		inst.components.hunger:DoDelta(50)
		inst.components.talker:Say("Itadakimasu. Nya~", 1.2, true)
	
	elseif food and food.components.edible and food.prefab == "fishtacos" then
	       inst.components.health:DoDelta(50)
		inst.components.sanity:DoDelta(50)
		inst.components.hunger:DoDelta(50)
		inst.components.talker:Say("Itadakimasu. Nya~", 1.2, true)
	
	elseif food and food.components.edible and food.prefab == "fish" then
		inst.components.health:DoDelta(20)
		inst.components.sanity:DoDelta(30)
		inst.components.hunger:DoDelta(20)
		inst.components.talker:Say("Itadakimasu. Nya~", 1.2, true)
	
	elseif food and food.components.edible and food.prefab == "fish_cooked" then
		inst.components.sanity:DoDelta(-15)
		inst.components.talker:Say("Don't you know the fire is not good for me! Do not try to eat that me again!!!. Nya~", 1.2, true)
	
    elseif food and food.components.edible and food.prefab == "ice" then
	       inst.components.health:DoDelta(5)
		inst.components.sanity:DoDelta(10)
		inst.components.talker:Say("Itadakimasu. Nya~", 1.2, true)
		
	elseif food and food.components.edible and food.prefab  == not "ice" or "fish_cooked" or "fish" or "fishtacos" or "fishsticks" then
		inst.components.health:DoDelta(0)
		inst.components.sanity:DoDelta(0)
		inst.components.hunger:DoDelta(5)
		inst.components.talker:Say("I shouldn't eat that. Nya~")
	end
end

 

Link to comment
Share on other sites

And also this code is just not works it was working before

 

inst.AddTag("fridge")

when i put this into master_postinit the game crashes and here is error

Quote

[string "scripts/entitysctript.lua"]:483: attempt tp index field "entit" (a nil vaule)

and the line is

function EntityScript:AddTag(tag)
    self.entity:AddTag(tag)
end

Link to comment
Share on other sites

I assume you want to set it so all food that's not your required list only gives 5 hunger, but as you might be able to observe, wet goop and rot will in theory also give you +5 hunger now; that's because you aren't modifying what the food does, only adding to the impact of receiving it.

-- hmm wouldn't it be food.inst.prefab?
-- ediables store their parent item in .inst , maybe I'm wrong here though if food.prefab is okay.
-- if my prefab name check fails - just overwrite it.

elseif food and food.components.edible and food.prefab  == not "ice" or "fish_cooked" or "fish" or "fishtacos" or "fishsticks" then
	local hungerboost = 5
	local foodcheck = food.inst.prefab == "spoiled_food" and 0 or hungerboost -- if true then forget adding bonus, else give our hungerboost
	-- ternary function for foodcheck.

	inst.components.health:DoDelta(0) -- "-(food:GetHealth() * eater.hungerabsorption)" if you wanted to prevent health gains & losses(careful).
	inst.components.sanity:DoDelta(0) -- "-(food:GetSanity() * eater.sanityabsorption)" nullify sanity to 0 gains & losses(careful).		
	inst.components.hunger:DoDelta(5 - food:GetHunger() * eater.hungerabsorption) -- This still would nullify the hunger value from the item and then add 5.
	-- bare in mind, this wouldn't consider anything other than rot/"spoiled_food", you'd have to figure out a way to deal with mushrooms etc.
	inst.components.talker:Say("I shouldn't eat that. Nya~")
end

I just hope it works and I didn't offend by writing the code out. ^^'

I'll keep an eye out if you have any more issues in regards to this - see if I can't help out; utterly stumped on my mod atm. XD

 

- Edited in:

13 minutes ago, AkaiNight said:

inst.AddTag("fridge")

master_postinit  deals with client and server side at the same time, I'm stuck with these kinds of things for my own mod; trying to implement in a badge. I don't fully know how the fridge tag would impact the rest of the code, it could potentially well nested in many files.

Edited by MorickClive
- addressing second comment.
Link to comment
Share on other sites

2 hours ago, MorickClive said:

I assume you want to set it so all food that's not your required list only gives 5 hunger, but as you might be able to observe, wet goop and rot will in theory also give you +5 hunger now; that's because you aren't modifying what the food does, only adding to the impact of receiving it.


-- hmm wouldn't it be food.inst.prefab?
-- ediables store their parent item in .inst , maybe I'm wrong here though if food.prefab is okay.
-- if my prefab name check fails - just overwrite it.

elseif food and food.components.edible and food.prefab  == not "ice" or "fish_cooked" or "fish" or "fishtacos" or "fishsticks" then
	local hungerboost = 5
	local foodcheck = food.inst.prefab == "spoiled_food" and 0 or hungerboost -- if true then forget adding bonus, else give our hungerboost
	-- ternary function for foodcheck.

	inst.components.health:DoDelta(0) -- "-(food:GetHealth() * eater.hungerabsorption)" if you wanted to prevent health gains & losses(careful).
	inst.components.sanity:DoDelta(0) -- "-(food:GetSanity() * eater.sanityabsorption)" nullify sanity to 0 gains & losses(careful).		
	inst.components.hunger:DoDelta(5 - food:GetHunger() * eater.hungerabsorption) -- This still would nullify the hunger value from the item and then add 5.
	-- bare in mind, this wouldn't consider anything other than rot/"spoiled_food", you'd have to figure out a way to deal with mushrooms etc.
	inst.components.talker:Say("I shouldn't eat that. Nya~")
end

I just hope it works and I didn't offend by writing the code out. ^^'

I'll keep an eye out if you have any more issues in regards to this - see if I can't help out; utterly stumped on my mod atm. XD

 

- Edited in:

master_postinit  deals with client and server side at the same time, I'm stuck with these kinds of things for my own mod; trying to implement in a badge. I don't fully know how the fridge tag would impact the rest of the code, it could potentially well nested in many files.

thanks again i'll try something for fridge i hope one of them work

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