- Home /
Stack Overflow error on delegate call
This is very puzzling! I'm working on a quest system and have chosen to use c# events. I have a quest giver script placed on a model, I have a quest panel that tells the player about quest and gives them the choice via button to accept the quest or not( yes button and no button). I also have a journal script to keep track of current and completed quests. I set up a delegate and event for OnAcceptQuest. Just as a test, I set up a keypress to add the quest, which worked. Only when I try using the button, it didn't work at all; the quest was getting lost so I found a way around that, I thought. Now I'm getting a Stack Overflow error when I try to add the quest via click on a button image. This post is a little long-winded, but I am trying to give as much info as possible. Now for the code:
Event Manager: public class EventsManager { //Quests public delegate void QuestAddDelegate(QuestClass quest); public static QuestAddDelegate OnAcceptQuestEvent; public static void AcceptQuest(QuestClass quest) {
if(OnAcceptQuestEvent != null)
{
OnAcceptQuestEvent(quest);
}
}
}
Quest Giver: private void OnTriggerEnter() { print("Open panel"); OpenQuestPanel(); }
private void OnTriggerExit()
{
print("Close panel");
CloseQuestPanel();
}
private void OnEnable()
{
EventsManager.OnAcceptQuestEvent += AddQuest;
}
private void OnDisable()
{
EventsManager.OnAcceptQuestEvent -= AddQuest;
}
public void OpenQuestPanel()
{
if(Quest.questStatus == QuestStatus.Unassigned)
{
QuestPanel.SetActive(true);
PresentQuest();
}
else if(Quest.questStatus == QuestStatus.InProcess)
{
QuestPanel.SetActive(false);
ProgressRewardPanel.SetActive(true);
QuestInProgress();
}
else if (Quest.questStatus == QuestStatus.Complete)
{
Rewards();
}
}
public void CloseQuestPanel()
{
QuestPanel.SetActive(false);
}
void PresentQuest()
{
QuestPanel.SetActive(true);
IntroText.text = Quest.introText;
QuestDescText.text = Quest.questDescription;
RewardText.text = "You will receive " + Quest.reward + " for your trouble.";
}
//public void OnClickYes()
//{
//questAccepted = true;
// Debug.Log("Yes, accept the Quest!");
// AddQuest(Quest);
//checkquest();
// Add quest to the active quest list
// change icon to hourglass
//}
public void OnClick()
{
Debug.Log("Yes, accept the Quest!");
AddQuest(Quest);
}
public void OnClickNo()
{
RefusalText.text = Quest.refusalText;
}
void AddQuest(QuestClass quest)
{
EventsManager.AcceptQuest(quest);
Quest.questStatus = QuestStatus.InProcess;
}
}
Journal:
private void OnEnable()
{
EventsManager.OnAcceptQuestEvent += AddQuest;
EventsManager.OnCancelQuestEvent += RemoveQuest;
}
private void OnDisable()
{
EventsManager.OnAcceptQuestEvent -= AddQuest;
EventsManager.OnCancelQuestEvent -= RemoveQuest;
}
void Start()
{
}
void Update()
{
}
void AddQuest(QuestClass quest)
{
CurrentQuestList.Add(quest);
print(CurrentQuestList.Count);
ShowCurrentQuests();
}
void RemoveQuest(QuestClass quest)
{
CurrentQuestList.Remove(quest);
print(CurrentQuestList.Count);
}
Test script:
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
print("Q");
EventsManager.AcceptQuest(quest);
}
Thanks in advance!
Answer by Gilead7 · Nov 04, 2021 at 04:56 PM
Thanks Bunny! It took some time, but for those of you following at home... I had things mixed up. I didn't need the onEnable and onDisable nor did I need the AcceptQuest functions in the QuestGiver script. I thought the questgiver needed to know that the player accepted the quest, but that wasn't the case. I made a new function that is called when the player clicks on the yes button, rather than using an image and an onclick function. In that function I called the event and as long as the panel was open, it worked! Thanks again Bunny for giving me just what I needed to figure out the rest. First time using events/delegates so this is kinda new to me. Hope this helps somebody out there!
Answer by Bunny83 · Nov 01, 2021 at 05:09 PM
To quote the relevant parts from your code:
public static QuestAddDelegate OnAcceptQuestEvent;
public static void AcceptQuest(QuestClass quest)
{
if(OnAcceptQuestEvent != null)
{
OnAcceptQuestEvent(quest);
}
}
}
private void OnEnable()
{
EventsManager.OnAcceptQuestEvent += AddQuest;
}
void AddQuest(QuestClass quest)
{
EventsManager.AcceptQuest(quest);
Quest.questStatus = QuestStatus.InProcess;
}
Look at those snippets carefully if you can see your issue.
Try just follow the execution
AcceptQuest --> OnAcceptQuestEvent --> AddQuest --> AcceptQuest --> ...
Your answer
Follow this Question
Related Questions
Question about unsubscribing eventhandlers OnDisable and OnEnable 2 Answers
How do Delegates and Events work? 1 Answer
Multiple Cars not working 1 Answer
Unity Events, Listeners, Delegates, etc. 1 Answer
Distribute terrain in zones 3 Answers