Jump to content

[SOLVED] Sanity Aura Code Crashing


Recommended Posts

Hi there!

 

I've found myself in a rather annoying situation with a line of code I absolutely cannot figure out. I have searched far and wide for an answer and just can't fnd a solution.

I've made a character with all functionalities working fine up to this point. I'm trying to add a sanity aura to certain entities; example, when my character and only my character approaches say a flower or a bee, they get a boost in sanity. I have found several different codes for this, but either they don't work or cause the game to crash. I attached my pfefab file for reference (the crashing occurs at line 71).

 

Here is the code that causes my game to crash with this error every, single time:

[string "scripts/mainfunctions.lua"]:150: Error loading file prefabs/wolyo
[string "../mods/wolyo_v4/scripts/prefabs/wolyo.lua"]:71: ')' expected near ','
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/mainfunctions.lua(150,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(161,1)
        scripts/mods.lua(603,1) in function 'RegisterPrefabs'
        scripts/gamelogic.lua(278,1) in function 'LoadAssets'
        scripts/gamelogic.lua(925,1)
        =[C] in function 'SetPersistentString'
        scripts/saveindex.lua(188,1) in function 'Save'
        scripts/saveindex.lua(409,1)
        =[C] in function 'SerializeWorldSession'
        scripts/networking.lua(290,1) in function 'SerializeWorldSession'
        scripts/saveindex.lua(412,1) in function 'OnGenerateNewWorld'
        scripts/gamelogic.lua(936,1) in function 'cb'
        scripts/screens/worldgenscreen.lua(151,1) in function 'OnUpdate'
        scripts/frontend.lua(660,1) in function 'Update'
        scripts/update.lua(92,1)

This is the line that causes the crash:

AddPrefabPostInit("butterfly", function(inst))
        if GetPlayer().prefab == "wolyo" then
            inst:AddComponent("sanityaura")
            inst.components.sanityaura.aura = TUNING.SANITYAURA_LARGE
        end
end)

 

If this isn't even the right sequence I'm supposed to be using, please inform me. I'm not even sure if these lines work for DST.

 

Any help at all will be greatly appreciated.

Thanks in advance!

 

 

wolyo.lua

Edited by Cthonicus
Link to comment
Share on other sites

3 hours ago, thomas4846 said:

AddPrefabPostInit("butterfly", function(inst)) => AddPrefabPostInit("butterfly", function(inst)  

you added a bracket `)` after the function(inst)

(don't worry my entire code crashed 30 times cus i named my character poop by accident)

Thanks for the response!

 

I tried what you had said and it still resulted in a crash, except this time it told me this:

[00:02:56]: [string "../mods/wolyo_v4/scripts/prefabs/wolyo.lua"]:76: variable 'AddPrefabPostInit' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        ../mods/wolyo_v4/scripts/prefabs/wolyo.lua(76,1) in function 'fn'
        scripts/mainfunctions.lua(153,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(161,1)
        scripts/mods.lua(603,1) in function 'RegisterPrefabs'
        scripts/gamelogic.lua(278,1) in function 'LoadAssets'
        scripts/gamelogic.lua(925,1)
        =[C] in function 'SetPersistentString'
        scripts/saveindex.lua(188,1) in function 'Save'
        scripts/saveindex.lua(409,1)
        =[C] in function 'SerializeWorldSession'
        scripts/networking.lua(290,1) in function 'SerializeWorldSession'
        scripts/saveindex.lua(412,1) in function 'OnGenerateNewWorld'
        scripts/gamelogic.lua(936,1) in function 'cb'
        scripts/screens/worldgenscreen.lua(151,1) in function 'OnUpdate'
        scripts/frontend.lua(660,1) in function 'Update'
        scripts/update.lua(92,1)
[00:02:56]: [string "../mods/wolyo_v4/scripts/prefabs/wolyo.lua"]:76: variable 'AddPrefabPostInit' is not declared
LUA ERROR stack traceback:
        =[C] in function 'error'
        scripts/strict.lua(23,1)
        ../mods/wolyo_v4/scripts/prefabs/wolyo.lua(76,1) in function 'fn'
        scripts/mainfunctions.lua(153,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(161,1)
        scripts/mods.lua(603,1) in function 'RegisterPrefabs'
        scripts/gamelogic.lua(278,1) in function 'LoadAssets'
        scripts/gamelogic.lua(925,1)
        =[C] in function 'SetPersistentString'
        scripts/saveindex.lua(188,1) in function 'Save'
        scripts/saveindex.lua(409,1)
        =[C] in function 'SerializeWorldSession'
        scripts/networking.lua(290,1) in function 'SerializeWorldSession'
        scripts/saveindex.lua(412,1) in function 'OnGenerateNewWorld'
        scripts/gamelogic.lua(936,1) in function 'cb'
        scripts/screens/worldgenscreen.lua(151,1) in function 'OnUpdate'
        scripts/frontend.lua(660,1) in function 'Update'
        scripts/update.lua(92,1)	

Is the line even supported by DST?

Thanks,

Link to comment
Share on other sites

6 minutes ago, thomas4846 said:

 i'm not sure if the 


AddPrefabPostInit

can be written in the character code, i put mine in the modmain

Wow, it worked!!!

 

I could have sworn I have tried this a multitude of times (even without the parenthesis at the end, I just wanted to try it).

 

BIG thanks! :D

  • Like 2
Link to comment
Share on other sites

3 minutes ago, thomas4846 said:

it's fine i'm really happy to help, plus it helps me procrastinate with my homework

At least it's for a good cause? :]

 

One last quickie question, if you don't mind! I'd like to know if it's possible to attach more than one entity in the string so I don't have to copy and paste this like ten times. I tried AddPrefabPostInit("butterfly, flower", function(inst), but it just disabled the aura for butterflies and didn't give one for flowers either. If it's not possible it's no biggie at all, I just wanted to clean the file up a bit.

AddPrefabPostInit("butterfly", function(inst)
        if GetPlayer().prefab == "wolyo" then
            inst:AddComponent("sanityaura")
            inst.components.sanityaura.aura = TUNING.SANITYAURA_LARGE
        end
end) 

Thanks again!

  • Haha 1
Link to comment
Share on other sites

you can use the function multiple times but the 

AddPrefabPostInit must be used for each one i think

 

--------------------------e.g-------------------------------

local function SanityAura(inst)

if GetPlayer().prefab == "wolyo" then

           inst.AddComponent("sanityaura")

           inst.components.sanityaura = TUNING.SANITYAURA_LARGE

         end

end

AddPrefabPostInit("butterfly",SanityAura)

AddPrefabPostInit("flower",SanityAura) ---> this should work since most of your code has the same function anyway

--------------------------------------------------------------

To be blunt tho, this may not work: i can help tho if you need it

Edited by thomas4846
Link to comment
Share on other sites

2 minutes ago, thomas4846 said:

you can use the function multiple times but the 

AddPrefabPostInit must be used for each one i think

 

--------------------------e.g-------------------------------

local function SanityAura(inst)

if GetPlayer().prefab == "wolyo" then

           inst.AddComponent("sanityaura")

           inst.components.sanityaura = TUNING.SANITYAURA_LARGE

         end

end

AddPrefabPostInit("butterfly",SanityAura)

AddPrefabPostInit("flower",SanityAura) ---> this should work since most of your code has the same function anyway

--------------------------------------------------------------

To be blunt tho, this may not work: i can help tho if you need it

Okay cool, I'll give it a go :D

9 minutes ago, thomas4846 said:

you can use the function multiple times but the 

AddPrefabPostInit must be used for each one i think

 

--------------------------e.g-------------------------------

local function SanityAura(inst)

if GetPlayer().prefab == "wolyo" then

           inst.AddComponent("sanityaura")

           inst.components.sanityaura = TUNING.SANITYAURA_LARGE

         end

end

AddPrefabPostInit("butterfly",SanityAura)

AddPrefabPostInit("flower",SanityAura) ---> this should work since most of your code has the same function anyway

--------------------------------------------------------------

To be blunt tho, this may not work: i can help tho if you need it

Will this just work with my character, and no one else?

Link to comment
Share on other sites

no that's the problem

sanity aura applies for all but it's fixable

local function CalcSanityAura(inst, observer)
    return observer:HasTag("spiderwhisperer") and 0 or inst.components.sanityaura.aura
end

 

    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = CalcSanityAura

 

basically these to pieces of code are ripped out of the webber spider code, i'm gonna have it done with you finding it out cus i think it's more important to do so.

try to explain what the function does 

Edited by thomas4846
Link to comment
Share on other sites

2 minutes ago, thomas4846 said:

no that's the problem

sanity aura applies for all but i can fix that

Okay cool

13 minutes ago, thomas4846 said:

no that's the problem

sanity aura applies for all but it's fixable

local function CalcSanityAura(inst, observer)
    return observer:HasTag("spiderwhisperer") and 0 or inst.components.sanityaura.aura
end

 

    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = CalcSanityAura

 

basically these to pieces of code are ripped out of the webber spider code, i'm gonna have it done with you finding it out cus i think it's more important to do so.

try to explain what the function does 

I think I'll just stick to using copy/pasting the same code to avoid a hassle, knowing it'll work for sure.

 

Again, thanks a bunch for your help! I was up all night and couldn't figure that out from anywhere lol

Link to comment
Share on other sites

Okay, so I found a teensy problem.

 

The sanity aura works great for all NPCs, but when I try to use the same code on a flower it crashes. No error report is being displayed; the server just doesn't launch to the character selection screen.

 

Any idea why this might be?

Thanks

Link to comment
Share on other sites

local function cute (inst)
        if inst.components.sanityaura == nil then 
            inst:AddComponent("sanityaura")
            inst.components.sanityaura.aurafn = IsWolyo
      end
end

local function IsWolyo (inst,observer)
         return observer.prefab == "wolyo" and TUNING.SANITYAURA_MED 
  end
  
  AddPrefabPostInit("flower",cute)

this probably won't work but it might

Edited by thomas4846
Link to comment
Share on other sites

Since your function replaces there actually being a value ("aura" is no longer used when an "aurafn" function is given, unless "aurafn" returns nil), so you can still use the "aura" variable as a default value. Also, it should be noted that if you want to add an original component to an original entity, you should probably check if some other mod has already added a sanityaura component to the bees and/or if they have changed the "aurafn" function.

I have done so here.

Spoiler

local function wolyoFriendlyAnimalSanityAura (inst,observer)
	return observer.prefab == "wolyo" and TUNING.SANITYAURA_MED or nil
end

local function cute(inst)
    if inst.components.sanityaura == nil then
		if not inst.components.sanityaura then
			inst:AddComponent("sanityaura")
		end
		
		local origAuraFn = inst.sanityaura.aurafn
		if origAuraFn then
			inst.sanityaura.aurafn = function(inst, observer)
				return wolyoFriendlyAnimalSanityAura(inst, observer) or origAuraFn(inst, observer)
			end
		else
			inst.sanityaura.aurafn = wolyoFriendlyAnimalSanityAura(inst, observer)
		end
    end
end

AddPrefabPostInit("flower", cute)

 

However, if you are going to add a bunch of character-specific sanity auras, you should probably turn things around and instead implement an aura for the player, which gives them sanity when they are close to certain other entities. See how to do that in the following post. You can easily change the entity search to look for bees etc. and give some amount of sanity to the player per bee:

 

Edited by Ultroman
  • Like 1
Link to comment
Share on other sites

12 hours ago, Ultroman said:

Since your function replaces there actually being a value ("aura" is no longer used when an "aurafn" function is given, unless "aurafn" returns nil), so you can still use the "aura" variable as a default value. Also, it should be noted that if you want to add an original component to an original entity, you should probably check if some other mod has already added a sanityaura component to the bees and/or if they have changed the "aurafn" function.

I have done so here.

  Hide contents


local function wolyoFriendlyAnimalSanityAura (inst,observer)
	return observer.prefab == "wolyo" and TUNING.SANITYAURA_MED or nil
end

local function cute(inst)
    if inst.components.sanityaura == nil then
		if not inst.components.sanityaura then
			inst:AddComponent("sanityaura")
		end
		
		local origAuraFn = inst.sanityaura.aurafn
		if origAuraFn then
			inst.sanityaura.aurafn = function(inst, observer)
				return wolyoFriendlyAnimalSanityAura(inst, observer) or origAuraFn(inst, observer)
			end
		else
			inst.sanityaura.aurafn = wolyoFriendlyAnimalSanityAura(inst, observer)
		end
    end
end

AddPrefabPostInit("flower", cute)

 

However, if you are going to add a bunch of character-specific sanity auras, you should probably turn things around and instead implement an aura for the player, which gives them sanity when they are close to certain other entities. See how to do that in the following post. You can easily change the entity search to look for bees etc. and give some amount of sanity to the player per bee:

I tried the line you provided here and it sadly resulted in the server stuck on "generating world" :(. But, when I remove

AddPrefabPostInit("flower", cute)

the server launches and there are seemingly no issues (but there is also no function now so it renders the sequence pointless).

I attached the log if that would help here.

 

Please pardon my inexperience. I do see the thread you've created, which is very well explained, however, I must be honest when I say I really am quite clueless when it comes to constructing a functioning line of code. I see that the line you've provided has a health value and not sanity (so I would change that(?)), but I personally can't see where this applies to only one entity, which is in my case the one player or where I would assign the entity the sanity aura originates from. I'm sure this is supposed to have additional lines added to it, but I really wouldn't know where to start with that.


inst:DoPeriodicTask(1.0, function(inst)
	-- Do nothing if the entity has a health component and is dead, or it is a playerghost.
	if inst.components.health and inst.components.health:IsDead() or inst:HasTag("playerghost") then
		return
	end
	
	-- Store the position of the entity in x, y, z variables.
	local x,y,z = inst.Transform:GetWorldPosition()
	
	-- Description of important function, which finds specific entities within a range:
	-- TheSim:FindEntities(x, y, z, radius, mustHaveTags, cantHaveTags, mustHaveOneOfTheseTags)
	
	-- I have limited it to entities with the "player" tag, that are also not ghosts or otherwise in limbo.
	-- I have set the radius to be 10. You can set it to whatever radius you want.
	local players = TheSim:FindEntities(x, y, z, 10, {"player"}, {"playerghost", "INLIMBO"}, nil)
	
	-- Run through all the players within range...
	for _,v in ipairs(players) do
		-- ...and if they have a health component, add 1 to their health.
		if v.components.health then
			v.components.health:DoDelta(1)
		end
	end
end)

Again I'm abundantly thankful for the help I've been receiving. I just wish I knew more so I could make this less of a hassle.

client_log.txt

Link to comment
Share on other sites

i can't see the problem rn but  

CPU                       
    numCores                  4
    features                  SSE,SSE2,SSE3,SSSE3,SSE41,SSE42,AVX
    name                      Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    manufacturer              GenuineIntel
    clockSpeed                2801
  RAM                       
    megsOfRam                 16384

 

bro i'm actually jealous 

instead of doing the postinit function maybe try to do a custom sanity function like what willow has around fires

Link to comment
Share on other sites

13 hours ago, thomas4846 said:

instead of doing the postinit function maybe try to do a custom sanity function like what willow has around fires

What Willow does is almost the same as I'm suggesting, except they add the function as a custom sanity rate-calculation, instead of splitting them out into a separate periodic task. The way Willow's code does it IS the most "right" way to do it, but it is important to note that a caveat of this is that your code will run every frame, so if you're doing a bunch of stuff in your calculations, and it's not super-important for your code to add/remove sanity every frame, you can save a bunch of CPU-cycles by controlling how often your sanity changes are calculated and applied.

It's up to you how you want to solve this one, but there are two very good solutions here to pick from.

20 hours ago, Cthonicus said:

I tried the line you provided here and it sadly resulted in the server stuck on "generating world" :(.

Since I failed to tell you where to put the line, given I wasn't aware of your inexperience with modding at the time I wrote it, I completely understand why the code isn't working. You've probably put it in your AddPrefabPostinit, right? It should go at the bottom of the masterpostinit function of your character's Lua file.

20 hours ago, Cthonicus said:

Please pardon my inexperience. I do see the thread you've created, which is very well explained, however, I must be honest when I say I really am quite clueless when it comes to constructing a functioning line of code. I see that the line you've provided has a health value and not sanity (so I would change that(?)), but I personally can't see where this applies to only one entity, which is in my case the one player or where I would assign the entity the sanity aura originates from. I'm sure this is supposed to have additional lines added to it, but I really wouldn't know where to start with that.

That's why I wrote "You can easily change the entity search to look for bees etc. and give some amount of sanity to the player per bee". The code in the given example looks for other players within range (TheSim:FindEntities) and then gives THEM health (by looping over the entities that were found and adding 1 HP to them. You would need to instead make it look for bees and whatever else you want, and then write some code to calculate how much sanity you should gain. Using Willow's code, you need to calculate the value your player should receive in exactly 1 second (this is important, since the sanity component updates the current sanity every frame using the calculated rate, multiplying it with deltatime to figure out how much to add depending on how long it has been since the last frame. Using my code, you need to calculate how much sanity the player should gain per 0.5 seconds (or whatever you set the interval for the periodic task to be).

20 hours ago, Cthonicus said:

I just wish I knew more so I could make this less of a hassle.

You can! Check out the newcomer post. Take your time. Do the Lua Crash Course linked there and when you feel like the syntax makes sense to you, look at some of the example mods. It should get you a good deal of the way, if you actually want to pursue modding (or coding in general).

Edited by Ultroman
  • Like 1
Link to comment
Share on other sites

That said, Willow's code is pretty spicy for a newcomer. Took me a minute or two to fully understand what it does, with all the ranges and different values. I mean, I got the gist of it quickly, but understanding exactly how it does it, is a different matter. And you need to be at that level in order to change it.

Edited by Ultroman
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...