PaladinPlayer Posted January 23, 2015 Share Posted January 23, 2015 Hello, Thank you for stumbling upon this thread stranger! I would like to add a sanity perk to my character but I don't know how to write code very we so here goes. The perks work like this;She will have a small sanity gain when holding a bird and a slightly smaller gain when holding a feather. They are and simple but I do not know how to check for an item in an inventory. Thank you for your help kind stranger! Link to comment Share on other sites More sharing options...
rezecib Posted January 24, 2015 Share Posted January 24, 2015 @PaladinPlayer, I think putting this in the master_postinit for your character should do the trick:local birds = {"robin", "robin_winter", "crow"}local function sanityfn(inst) local hasbird = false local hasfeather = false for k,v in pairs(birds) do if inst.components.inventory:Has(v) then hasbird = true end if inst.components.inventory:Has("feather_"..v) then hasfeather = true end end return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)endlocal function master_postinit(inst) inst.components.sanity.custom_rate_fn = sanityfnI showed the master_postinit in there because it's better to have the sanityfn outside of it. I made some assumptions about how you wanted this to work, here:Having multiple birds does not give you any additional sanityHaving a bird negates any additional sanity from having feathersThere's probably a more efficient way to do this (something like listening for birds/feathers entering/leaving your inventory), but this should work, at least. Link to comment Share on other sites More sharing options...
PaladinPlayer Posted January 24, 2015 Author Share Posted January 24, 2015 @PaladinPlayer, I think putting this in the master_postinit for your character should do the trick:local birds = {"robin", "robin_winter", "crow"}local function sanityfn(inst) local hasbird = false local hasfeather = false for k,v in pairs(birds) do if inst.components.inventory:Has(v) then hasbird = true end if inst.components.inventory:Has("feather_"..v) then hasfeather = true end end return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)endlocal function master_postinit(inst) inst.components.sanity.custom_rate_fn = sanityfnI showed the master_postinit in there because it's better to have the sanityfn outside of it. I made some assumptions about how you wanted this to work, here:Having multiple birds does not give you any additional sanityHaving a bird negates any additional sanity from having feathersThere's probably a more efficient way to do this (something like listening for birds/feathers entering/leaving your inventory), but this should work, at least. Hi Rez! Thanks (again) for the code it was very helpful since it worked perfectly!Also while I look through more Modding Help pages I see your posts are always helpful which is awesome! Link to comment Share on other sites More sharing options...
PaladinPlayer Posted January 24, 2015 Author Share Posted January 24, 2015 @rezecib Your code worked perfectly up to a point but now it freezes/refuses to load/crashes and I don't know what to do please help this was such a nice feature before. P.S. I will do my best to provide info where it is needed but for now this is the prefab file I am using: wilda.lua Link to comment Share on other sites More sharing options...
rezecib Posted January 24, 2015 Share Posted January 24, 2015 @PaladinPlayer, Give us the crash log Link to comment Share on other sites More sharing options...
PaladinPlayer Posted January 24, 2015 Author Share Posted January 24, 2015 @rezecib First of all I assume the crash log is in documents/klei/donotstarvetogether/log.txt When sanityfn is here:local birds = {"robin", "robin_winter", "crow"}local function sanityfn(inst) local hasbird = false local hasfeather = false for k,v in pairs(birds) do if inst.components.inventory:Has(v) then hasbird = true end if inst.components.inventory:Has("feather_"..v) then hasfeather = true end end return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)endinst.components.sanity.custom_rate_fn = sanityfnend I get this crash: and this log:log.txt But when sanityfn is placed outside of master_postinit:local birds = {"robin", "robin_winter", "crow"}local function sanityfn(inst) local hasbird = false local hasfeather = false for k,v in pairs(birds) do if inst.components.inventory:Has(v) then hasbird = true end if inst.components.inventory:Has("feather_"..v) then hasfeather = true end end return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)endendinst.components.sanity.custom_rate_fn = sanityfn It will not load past this screen: and the log is:log.txt P.S. Thanks for the help and sorry for the long post Link to comment Share on other sites More sharing options...
rezecib Posted January 24, 2015 Share Posted January 24, 2015 (edited) @PaladinPlayer, Change both of the Has() to have ,1) at the end (e.g. Has("feather_"..v, 1) ) Edited January 25, 2015 by rezecib Link to comment Share on other sites More sharing options...
PaladinPlayer Posted January 25, 2015 Author Share Posted January 25, 2015 @rezecib,This works well now thank you but the birds now give the same amount of sanity as the feathers. Link to comment Share on other sites More sharing options...
PaladinPlayer Posted January 25, 2015 Author Share Posted January 25, 2015 @rezecib, also she is a vegetarian but I wanted to add eggs to her diet so how would I go about doing that? Link to comment Share on other sites More sharing options...
rezecib Posted January 25, 2015 Share Posted January 25, 2015 @PaladinPlayer, Make a foodtype for the eggs and add it to her foodprefs. There are a ton of posts around on how to do that. Hmm... As for birds/feathers, it should be working the way it is, I think, but we can try rewriting the "return" line as a few lines instead:local delta = 0if hasfeather then delta = TUNING.DAPPERNESS_TINY endif hasbird then delta = TUNING.DAPPERNESS_SMALL endBut it may be working already, with the difference just not noticeable. Try changing the values, e.g. the SMALL to HUGE. Link to comment Share on other sites More sharing options...
PaladinPlayer Posted January 25, 2015 Author Share Posted January 25, 2015 @rezecib,Thanks again for all of your help! It works really well now and after a bit of food tweaking I think this mod is finished! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now