【发布时间】:2017-06-06 13:56:28
【问题描述】:
我正在统一制作 QuestSystem。
我想做的是为我的 questData 分配一个事件,以便它可以知道任务目标何时完成。
假设有一个名为 A 的类和一个名为 a 的操作。
我想要 Class B,Action b 想要参考 A.a
如果我这样做了
b = A.a;,
b+= someAction;,实际上是a+=someAction;
但是如果我这样做的话。它将只是简单地 b+=someAction 和 A.a 将保持 null
我应该怎么做才能完成我想要的?
这里有一些标签。 (我不知道答案是什么。所以..)
# event
# subscribing event
# assigning event
# referencing event
# action
# delegate
====== 已编辑 ======= 这是我的代码。
QuestData.cs
public class QuestData
{
public string questName;
public string qusetID;
public string questDescription;
public SceneType questSceneType;
private string isActivePrefsKey { get { return $"QuestKey{qusetID}"; } }
public bool isActive {
get {
return Convert.ToBoolean (PlayerPrefs.GetInt (isActivePrefsKey));
}
set { PlayerPrefs.SetInt (isActivePrefsKey, Convert.ToInt16 (value)); }
}
public QuestObjective questObjective;
public QuestReward questReward;
public void Activate ()
{
if (AppController.CurrentScene == questSceneType) {
questObjective.ActivateObjective ();
}
}
}
QuestObjective.cs
public class QuestObjective
{
// TODO rename all
public int goalObjectiveCount;
public int currentObjectiveCount;
public Action questAction;
public void OnConditionMatch ()
{
Debug.Log ("OnConditionMatch");
currentObjectiveCount += 1;
}
public void ActivateObjective ()
{
questAction += OnConditionMatch;
}
}
QuestManager.cs
public class QuestManager : MonoBehaviour
{
List<QuestData> questDatas;
void Awake ()
{
PrepareQuestDatas ();
ActivateActiveQuests ();
}
void ActivateActiveQuests ()
{
var activeQuests = GetActiveQuests ();
foreach (var activeQuest in activeQuests) {
activeQuest.Activate ();
}
}
List<QuestData> GetActiveQuests ()
{
// for debuging
return questDatas;
// real code
return questDatas.Where (q => q.isActive == true).ToList ();
}
public void PrepareQuestDatas ()
{
questDatas = new List<QuestData> {
new QuestData {
questName = "Foot Print",
questDescription = "win the game for first time",
questSceneType = SceneType.Main,
questObjective = new QuestObjective {
goalObjectiveCount = 1,
questAction = GamePlayController.instance.endGameCon.onWinGame
},
questReward = new QuestCoinReward{
rewardAmount = 100,
},
}
};
}
}
【问题讨论】:
-
您要问的问题的答案是给
B一个对有问题的A的引用:B.MyReferenceToSomeA.a += someAction;但我不确定这不是an XY Problem。 -
我不知道我想要什么。所以.. - 也许您应该尝试更准确地了解您的问题
-
@adjan 已编辑。我当然知道我想要什么哈哈
-
如果没有看到您的原始代码,我们将不知道如何对您现有的代码库进行更改。请发帖a minimal example of what needs to change,并详细说明需要修改的地方。您可能还想阅读How to Ask。
标签: c# events delegates event-handling