我正在尝试创建一个从咒语的较小部分进行咒语投射的系统。例如,损害成分和修复成分。为此,我创建了类,伤害和治疗,并将它们存储在字典中。然后,当它们从字典中拉出时,我想在这些类中调用一个函数。该功能始终被命名为“使用”。
由于每个类具有不同的类型,我很难让字典接受这些类。此外,当我致电Use时,系统会告诉我“类型不包含Use的定义”。
我已经进行了研究,无法弄清自己做错了什么。
public class MakeAbility : MonoBehaviour
{
public BlockScriptableObject block;
public IDictionary<string, Type> abilities_parts = new Dictionary<string, Type>();
public PartAttack part_attack;
private void Start()
{
abilities_parts.Add("part_attack", part_attack.GetType(PartAttack));
}
public void make_ability()
{
foreach (string i in block.ability_parts) // This is a string list of all the component parts. The idea is that For each entry, say "attack", it would call the class in the dictionary with the keyword "attack" and enact it's .Use method.
{
abilities_parts[i].Use();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PartAttack
{
public Targeting target_manager;
public int block_attack_damage = 0;
public int block_attack_repeats = 0;
void Use()
{
foreach (Combatant enemy in target_manager.list_of_targetable)
{
int repeats = block_attack_repeats;
while (repeats >= 0)
{
enemy.health = enemy.health - block_attack_damage;
block_attack_repeats -= 1;
}
}
}
}
答案 0 :(得分:0)
首先,您将Use
的实现方式
void Use()
{
...
}
自动将其设置为private
,因此无法从另一个类调用它。
public void Use()
{
...
}
该函数始终被命名为“使用”。
这是interface的确切用例
public interface IAbility
{
// interface method are automatically public
void Use();
}
现在,每个实现该接口的类都必须实现类似方法Use()
[System.Serializable]
public class PartAttack, IAbility
{
public Targeting target_manager;
public int block_attack_damage = 0;
public int block_attack_repeats = 0;
// has to implement this because of IAbility
public override void Use()
{
foreach (Combatant enemy in target_manager.list_of_targetable)
{
int repeats = block_attack_repeats;
while (repeats >= 0)
{
enemy.health = enemy.health - block_attack_damage;
block_attack_repeats -= 1;
}
}
}
}
如果您已经希望能够具有某些具有某些功能的预定义方法,并且使这些类也共享某些相同的字段,则将其设置为abstract class
[System.Serializable]
public abstract class Ability
{
// this can not be changed or accessed not even by any inheritor
private float aValueOnlyThisClassCanSee;
// this can changed/accessed by inheritors but not from the outside
protected string SomeProtectedValue;
// this can accessed/changed by everyone
public Targeting target_manager;
// the same keywords can be used on methods ... I skip this ;)
// This method HAS TO BE implemented by every inheritor
public abstract void Use();
// This method CAN BE implemented in order to extend or overwrite it by inhertors
public virtual void SaySomething()
{
Debug.Log("I basically do nothing.");
}
}
现在您可以继承
[System.Serializable]
public class PartAttack : Ability
{
// inherit target_manager and can use it in every inheritor
public int block_attack_damage = 0;
public int block_attack_repeats = 0;
// Has to be implemented otherwise throws compiler-error
public override void Use()
{
foreach (Combatant enemy in target_manager.list_of_targetable)
{
int repeats = block_attack_repeats;
while (repeats >= 0)
{
enemy.health = enemy.health - block_attack_damage;
block_attack_repeats -= 1;
}
}
}
// CAN BE implemented in order to extend
public override void SaySomething()
{
Debug.Log("I'm creating some debug logs but ..");
base.SaySomething();
}
}
现在取决于您选择哪本字典
public Dictionary<string, IAbility> abilities_parts = new Dictionary<string, IAbility>();
或
public Dictionary<string, Ability> abilities_parts = new Dictionary<string, Ability>();