Noveltease
Member
Hello,
I am currently trying to get a Starving script working. I set up my 4 primary attributes, Health/Stamina/Hunger/Thirst. These were quite easy to do in the attribute Manager.
However, I am trying to put together a Script so that when Hunger and Thirst hit 0, it begins to damage the players health over time. Below is the code I am currently using. I thought I could test it with doing a damage of 50 health since I have 100 to start with, which would have been obvious if it was working as intended or not. Using health.Damage(50) killed me in 1 hit though. I reduced it to 10 and even 1 as you see in the code and still it kills me in 1 hit. I want starving to be bad, but not instant death.
I was hoping someone knows where I went wrong and also how I could convert it to deal damage over time once my Hunger hits 0. I also suppose I will have to add a couple more things to make the code neater and stop killing me after I am dead as it seems to want to do now. I can probably just have a start and stop function.
I appreciate any help, thanks ahead of time! (I assume that for the dehydration script I can duplicate this and rename from starving to dehydration then simply change the attribute it looks for from hunger to Thirst and adjust the Damage over Time as needed.)
I am currently trying to get a Starving script working. I set up my 4 primary attributes, Health/Stamina/Hunger/Thirst. These were quite easy to do in the attribute Manager.
However, I am trying to put together a Script so that when Hunger and Thirst hit 0, it begins to damage the players health over time. Below is the code I am currently using. I thought I could test it with doing a damage of 50 health since I have 100 to start with, which would have been obvious if it was working as intended or not. Using health.Damage(50) killed me in 1 hit though. I reduced it to 10 and even 1 as you see in the code and still it kills me in 1 hit. I want starving to be bad, but not instant death.
I was hoping someone knows where I went wrong and also how I could convert it to deal damage over time once my Hunger hits 0. I also suppose I will have to add a couple more things to make the code neater and stop killing me after I am dead as it seems to want to do now. I can probably just have a start and stop function.
I appreciate any help, thanks ahead of time! (I assume that for the dehydration script I can duplicate this and rename from starving to dehydration then simply change the attribute it looks for from hunger to Thirst and adjust the Damage over Time as needed.)
C#:
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Traits;
using UnityEngine;
public class Starving : MonoBehaviour
{
[Tooltip("The character that has the health component.")]
[SerializeField] protected GameObject m_Character;
[Tooltip("The AttributeManager that contains the attribute. If null the character's Attribute Manager will be used.")]
[SerializeField] protected AttributeManager m_AttributeManager;
[Tooltip("The name of the attribute that the UI should monitor.")]
[SerializeField] protected string m_AttributeName = "Hunger";
private Attribute m_Attribute;
public void Awake()
{
if (m_AttributeManager != null)
{
EventHandler.RegisterEvent<Attribute>(m_AttributeManager.gameObject, "OnAttributeUpdateValue", OnUpdateValue);
}
}
private void OnUpdateValue(Attribute attribute)
{
m_Attribute = m_AttributeManager.GetAttribute(m_AttributeName);
Debug.Log(m_AttributeName + "Value:" + m_Attribute.Value);
if (m_Attribute.Value <= 0)
{
Debug.Log("You Are Starving");
var health = m_Character.GetComponent<Health>();
health.Damage(1);
}
}
public void OnDestroy()
{
if (m_AttributeManager != null)
{
EventHandler.UnregisterEvent<Attribute>(m_AttributeManager.gameObject, "OnAttributeUpdateValue", OnUpdateValue);
}
}
}
Last edited: