- Home /
Problem with instances of class
Certain "skills" in my game have buffs. When a Monster() attacks with a skill, an attack command is sent with the target's Monster() instance and the attacker's Monster() instance. Obviously, the damage is sent to the target and the buffs are sent to the attacker.
For some reason though, the buffs are added to the target and I have no idea why. Adding debug logs confuses me even further because it actually confirms that the buff is being sent to the attacker, not the target (or so I assume).
Here's the script for the skill:
public class FistoftheBoar : Skill, IEffect
{
public FistoftheBoar()
{
Name = "Fist of the Boar";
Desc = "An inaccurate attack capable of dealing devastating damage. Boosts attack for 3 turns.";
}
public void ApplyEffect(Monster target, Monster sender)
{
target.Damage(2, DamageType.Physical);
sender.AddBuff(new SkillBuff("Fist of the Boar", BuffType.DMG, 2.0f, 3));
}
}
And the script for the Monster class:
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class Monster : MonoBehaviour
{
public string Name = "Hercules";
public int MaxHealth = 100;
public TextMesh HealthIndicator;
public Skill[] AvailableSkills = new Skill[3];
private Dictionary<SkillBuff, int> _buffs = new Dictionary<SkillBuff, int>();
private int _currentHealth;
void Start()
{
_currentHealth = MaxHealth;
HealthIndicator.text = _currentHealth + "/" + MaxHealth;
AvailableSkills[0] = new CrushingBlow();
AvailableSkills[1] = new VictorySlam();
AvailableSkills[2] = new FistoftheBoar();
}
public void Damage(int amount, DamageType dmgType)
{
float modAmount = amount;
SkillBuff[] dmgBuffs = SkillHelper.FindBuffs(_buffs.Keys.ToArray(), BuffType.DMG);
foreach (SkillBuff dmgBuff in dmgBuffs)
{
modAmount *= dmgBuff.Modifier;
}
_currentHealth -= (int)modAmount;
HealthIndicator.text = _currentHealth + "/" + MaxHealth;
BattleManager.Instance.ShowDamage(transform.position, (int)modAmount);
}
public void AddBuff(SkillBuff buff)
{
if (_buffs.ContainsKey(buff)) _buffs[buff] = buff.TurnTime;
else _buffs.Add(buff, buff.TurnTime);
}
public void Attack(Monster target, Skill skill = null)
{
if (skill == null) skill = AvailableSkills[Random.Range(0, AvailableSkills.Length)];
if (skill is IEffect) ((IEffect)skill).ApplyEffect(target, this);
}
public void ModifyBuffTime(int amount, SkillBuff modBuff = null)
{
if (modBuff != null && _buffs.ContainsKey(modBuff)) _buffs[modBuff] += amount;
else
{
SkillBuff[] currentBuffs = _buffs.Keys.ToArray();
foreach (SkillBuff buff in currentBuffs)
{
_buffs[buff] += amount;
if (_buffs[buff] <= 0) _buffs.Remove(buff);
}
}
}
public int GetHealth() { return _currentHealth; }
public Dictionary<SkillBuff, int> GetBuffs() { return _buffs; }
}
I apologize that it's uncommented, but hopefully someone could help me with this problem. It's really frustrating and I have no idea what's wrong.
I'm taking a look - one comment on style though - (IEffect) skill is a slower cast than (skill as IEffect)
Oh and all of that is slower than doing:
var skillEffect = skill as IEffect;
if(skillEffect != null)
The answer might be in how you call the ApplyEffect. It could be something you do before the call making target and sender being two references to the same object (the target).
It would certainly appear that the monster had to be attacking itself for that to happen. You've debug.logged the target and the sender and they are different you said?
Answer by Paniku · Apr 21, 2013 at 12:14 PM
Argh! I found the problem! On the Damage() method, the buffs were being taken from the target rather than the sender. So, if the monster1 attacked monster2, monster2's buffs would affect the attack rather than monster1's.
Your answer
Follow this Question
Related Questions
Weird gameplay slow down problem 1 Answer
A node in a childnode? 1 Answer
Unity fails to recognize Maya models/Unity scenes 1 Answer
How to create webbrowser object in Unity using C#? 0 Answers
Importing from Blender to Unity, animation problem 0 Answers