My Health script after moving to the Attributes folder I was running into errors I ended up closing VSC but when I reopened it the whole Health Script is completely blank with nothing…
Trying to rebuild it as best I can…any help?
My Health script after moving to the Attributes folder I was running into errors I ended up closing VSC but when I reopened it the whole Health Script is completely blank with nothing…
Trying to rebuild it as best I can…any help?
This is what I have added so far:
using System.Collections;
using System.Collections.Generic;
using RPG.Saving;
using UnityEngine;
namespace RPG.Attributes
{
public class Health : MonoBehaviour, ISaveable
{
[SerializeField] float healthPoints = 100f;
bool isDead = false;
// Start is called before the first frame update
// void Start(){}
// Update is called once per frame
// void Update(){}
public bool IsDead()
{
return isDead;
}
public void RestoreState(object state)
{
throw new System.NotImplementedException();
}
public void TakeDamage(float damage)
{
healthPoints = Mathf.Max(healthPoints - damage, 0);
if (healthPoints == 0)
{
Die();
}
}
private void Die()
{
if (isDead)
{
return;
}
}
public object CaptureState()
{
throw new System.NotImplementedException();
}
}
}
``
Slowly stopped the lecture and got the script rebuilt I think…I just hope the game still functions…
Hi, if you move scripts always close them first else you can run into error like this.
Having said that, you can always copy the script(s) from the github page of the lecture!
gl with your learning
Here’s the script from the course repo at this point:
using System;
using GameDevTV.Utils;
using RPG.Core;
using GameDevTV.Saving;
using RPG.Stats;
using UnityEngine;
using UnityEngine.Events;
namespace RPG.Attributes
{
public class Health : MonoBehaviour, ISaveable, IPredicateEvaluator
{
[SerializeField] float regenerationPercentage = 70;
[SerializeField] public TakeDamageEvent takeDamage;
public UnityEvent onDie;
[System.Serializable]
public class TakeDamageEvent : UnityEvent<float>
{
}
private LazyValue<float> healthPoints;
bool wasDeadLastFrame = false;
private void Awake() {
healthPoints = new LazyValue<float>(GetInitialHealth);
}
private float GetInitialHealth()
{
return GetComponent<BaseStats>().GetStat(Stat.Health);
}
private void Start()
{
healthPoints.ForceInit();
}
private void OnEnable() {
GetComponent<BaseStats>().onLevelUp += RegenerateHealth;
}
private void OnDisable() {
GetComponent<BaseStats>().onLevelUp -= RegenerateHealth;
}
public bool IsDead()
{
return healthPoints.value <= 0;
}
public void TakeDamage(GameObject instigator, float damage)
{
healthPoints.value = Mathf.Max(healthPoints.value - damage, 0);
if(IsDead())
{
onDie.Invoke();
AwardExperience(instigator);
}
else
{
takeDamage.Invoke(damage);
}
UpdateState();
}
public void Heal(float healthToRestore)
{
healthPoints.value = Mathf.Min(healthPoints.value + healthToRestore, GetMaxHealthPoints());
UpdateState();
}
public float GetHealthPoints()
{
return healthPoints.value;
}
public float GetMaxHealthPoints()
{
return GetComponent<BaseStats>().GetStat(Stat.Health);
}
public float GetPercentage()
{
return 100 * GetFraction();
}
public float GetFraction()
{
return healthPoints.value / GetComponent<BaseStats>().GetStat(Stat.Health);
}
private void UpdateState()
{
Animator animator = GetComponent<Animator>();
if (!wasDeadLastFrame && IsDead())
{
animator.SetTrigger("die");
GetComponent<ActionScheduler>().CancelCurrentAction();
}
if (wasDeadLastFrame && !IsDead())
{
animator.Rebind();
}
wasDeadLastFrame = IsDead();
}
private void AwardExperience(GameObject instigator)
{
Experience experience = instigator.GetComponent<Experience>();
if (experience == null) return;
experience.GainExperience(GetComponent<BaseStats>().GetStat(Stat.ExperienceReward));
}
private void RegenerateHealth()
{
float regenHealthPoints = GetComponent<BaseStats>().GetStat(Stat.Health) * (regenerationPercentage / 100);
healthPoints.value = Mathf.Max(healthPoints.value, regenHealthPoints);
}
public object CaptureState()
{
return healthPoints.value;
}
public void RestoreState(object state)
{
healthPoints.value = (float) state;
UpdateState();
}
public bool? Evaluate(EPredicate predicate, string[] parameters)
{
switch (predicate)
{
case EPredicate.IsAlive: return !IsDead();
}
return null;
}
}
}