Frictional Games Forum (read-only)
Get Sanity to drain in the dark and increase in the light? - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Get Sanity to drain in the dark and increase in the light? (/thread-14703.html)

Pages: 1 2


RE: Get Sanity to drain in the dark and increase in the light? - DRedshot - 04-13-2012

Here's how I would have done it Tongue
Spoiler below!

float sanityFactor = 10.0f;
float curSanity = 100.0f;
float oldSanity = 100.0f;

void OnStart()
{
SetPlayerSanity(100.0f);
AddTimer("SanityTimer", 0.5f, "UpdateSanity");
}

void UpdateSanity(string &in asTimer)
{
curSanity = GetPlayerSanity();
float temp;
if(curSanity < oldSanity)
{
temp = curSanity - sanityFactor;
SetPlayerSanity(temp);
}

if (curSanity >= oldSanity)
{
temp = curSanity + sanityFactor;
if(temp <= 100.0f)
{
SetPlayerSanity(temp);
}
else
{
SetPlayerSanity(100.0f);
}
}

curSanity = GetPlayerSanity();
oldSanity = curSanity;
AddTimer("SanityTimer", 0.5f, "UpdateSanity");
}


I bolded the thing you missed. You had to recalculate the current sanity after modifying it with the sanityFactor.
Also, I changed a '>' to a '>=' because sanity doesn't recover in light. You are in light when sanity is equal to it's previous value Smile

Hope this helps.

Edit: Bold doesn't work in code format :/

Also, I'd lower the sanity factor by quite a lot, losing 20 sanity per second seems a bit too much, maybe make it = 0.5-1


RE: Get Sanity to drain in the dark and increase in the light? - FragdaddyXXL - 04-13-2012

(04-13-2012, 02:06 AM)DRedshot Wrote: I bolded the thing you missed. You had to recalculate the current sanity after modifying it with the sanityFactor.
Also, I changed a '>' to a '>=' because sanity doesn't recover in light. You are in light when sanity is equal to it's previous value Smile

Hope this helps.

Edit: Bold doesn't work in code format :/

Also, I'd lower the sanity factor by quite a lot, losing 20 sanity per second seems a bit too much, maybe make it = 0.5-1
Yeah, I was staring at my code for a while and noticed the <=. And I was making it lose a lot to speed up the effect for testing. But would you look at that? It works now! (with the addition of that extra

curSanity = GetPlayerSanity();) I thought that because I hadn't changed curSanity in the Update method, I wouldn't need that.