I am having a bug where the weapon is correctly loaded when still in play mode and I press S and L but not when I enter portal or when I stop and restart the game after saving. The health and experience are saved properly but not the weapon equipped.
using RPG.Core;
using RPG.Stats;
using RPG.Saving;
using RPG.Attributes;
using RPG.Movement;
using UnityEngine;
using System.Collections.Generic;
using GameDevTV.Utils;
using System;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction, ISaveable, IModifierProvider
{
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] Transform rightHandTransform = null;
[SerializeField] Transform leftHandTransform = null;
[SerializeField] WeaponConfig defaultWeapon = null;
float timeSinceLastAttack = Mathf.Infinity;
Health target;
Mover mover;
WeaponConfig currentWeaponConfig;
LazyValue<Weapon> currentWeapon;
private void Awake()
{
mover = GetComponent<Mover>();
currentWeaponConfig = defaultWeapon;
currentWeapon = new LazyValue<Weapon>(SetUpDefaultWeapon);
}
private Weapon SetUpDefaultWeapon()
{
return EquipWeapon(defaultWeapon);
}
void Start()
{
currentWeapon.ForceInit();
}
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (target.IsDead()) return;
if (!GetIsInRange(target.transform))
{
mover.MoveTo(target.transform.position, 1f);
}
else
{
mover.Cancel();
AttackBehaviour();
}
}
public void SetWeapon(WeaponConfig weapon)
{
currentWeaponConfig = weapon;
currentWeapon.value = EquipWeapon(weapon);
}
private Weapon EquipWeapon(WeaponConfig weapon)
{
Animator anim = GetComponent<Animator>();
return weapon.Spawn(rightHandTransform, leftHandTransform, anim);
}
public Health GetTarget()
{
return target;
}
void AttackBehaviour()
{
transform.LookAt(target.transform);
// This will trigger Hit() event
if (timeSinceLastAttack > timeBetweenAttacks)
{
TriggerAttack();
timeSinceLastAttack = 0;
}
}
private void TriggerAttack()
{
GetComponent<Animator>().ResetTrigger("stopAttack");
GetComponent<Animator>().SetTrigger("attack");
}
// Animation Event
void Hit()
{
if(target == null) return;
float damage = GetComponent<BaseStats>().GetStat(Stat.Damage);
if(currentWeapon.value != null)
{
currentWeapon.value.OnHit();
}
if (currentWeaponConfig.HasProjectile())
{
currentWeaponConfig.LaunchProjectile(rightHandTransform, leftHandTransform, target, gameObject, damage);
}
else
{
target.TakeDamage(gameObject, damage);
}
}
void Shoot()
{
Hit();
}
bool GetIsInRange(Transform targetTransform)
{
return Vector3.Distance(transform.position, targetTransform.position) < currentWeaponConfig.GetRange();
}
public bool CanAttack(GameObject combatTarget)
{
if (combatTarget == null) return false;
if (!GetComponent<Mover>().CanMoveTo(combatTarget.transform.position) &&
GetIsInRange(combatTarget.transform))
{
return false;
}
Health target = combatTarget.GetComponent<Health>();
return target != null && !target.IsDead();
}
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.GetComponent<Health>();
}
public void Cancel()
{
StopAttack();
GetComponent<Mover>().Cancel();
target = null;
}
private void StopAttack()
{
GetComponent<Animator>().ResetTrigger("attack");
GetComponent<Animator>().SetTrigger("stopAttack");
}
public IEnumerable<float> GetAdditiveModifiers(Stat stat)
{
if (stat == Stat.Damage)
{
yield return currentWeaponConfig.GetDamage();
}
}
public IEnumerable<float> GetPercentageModifiers(Stat stat)
{
if (stat == Stat.Damage)
{
yield return currentWeaponConfig.GetPercentageBonus();
}
}
public object CaptureState()
{
return currentWeaponConfig.name;
}
public void RestoreState(object state)
{
string weaponName = (string)state;
WeaponConfig weapon = UnityEngine.Resources.Load<WeaponConfig>(weaponName);
EquipWeapon(weapon);
}
}
}
This is my fighter script and I think the issue lies somewhere in here but I am not sure where. Any help much appreciated, cheers!