Hi everyone. So I am creating a dialogue system that uses an event action system to try and practice de-coupling code. However, I am running into an error that my overload ‘StartDialog’ does not match delegate ‘Action’.
I understand that the issue here is that my event action is not written correctly (if I can even use an event action fort this) since StartDialog prompts a return of ‘dialog’, which doesn’t match the return of () I have. However, I am unsure how to correctly fix this. Can someone help me understand how to correct it?
Here is the code for the DialogController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogController : MonoBehaviour
{
public Text nameText;
public Text dialogText;
private Queue<string> sentences;
// Start is called before the first frame update
void Start()
{
PlayerBroker.StartDialog += StartDialog;
sentences = new Queue<string>();
}
public void StartDialog(Dialog dialog)
{
nameText.text = dialog.name;
sentences.Clear(); //clear any sentences in the queue
foreach (string sentence in dialog.sentences) //for each sentence, enqueue it
{
sentences.Enqueue(sentence);
}
DisplayNextSentence(); //then run the display method
}
public void DisplayNextSentence()
{
if (sentences.Count == 0) //if no more sentences
{
EndDialog(); //end the dialog
return;
}
string sentence = sentences.Dequeue(); //otherwise, dequeue the sentences (FIFO order)
dialogText.text = sentence;
}
void EndDialog()
{
Debug.Log("Dialog over".);
//here is where any end dialog animation can go
}
}
and here is the broker:
using System;
public class PlayerBroker
{
//this class is to broker event actions for the player
public static event Action MoveVertical;
public static event Action MoveHorizontal;
public static event Action CanTalk;
public static event Action StartDialog;
public static void CallMoveVertical()
{
MoveVertical?.Invoke(); //if movevertical is not null, invoke it
}
public static void CallMoveHorizontal()
{
MoveHorizontal?.Invoke(); //if movehorizontal is not null, invoke it
}
public static void CallCanTalk()
{
CanTalk?.Invoke();
}
public static void CallStartDialog()
{
StartDialog?.Invoke();
}
}
here is the dialog class too in case that matters:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialog
{
//this passes to DIalogController whenever we start a new dialog
//has all info we need for a dialog
public string name;
[TextArea(1, 10)] //specifies minimum and maximum amount of lines the text uses
public string[] sentences;
}
public void StartDialog(Dialog dialog)
this method requires a Dialog in order to work.
So when you invoke new dialogue you must pass it as parameter into PlayerBroker.CallStartDialog(someDialogue);
Where do you store you dialogs?
You need reference to actual dialog in order to work with it.
I see. The way I have it set up now Dialog is housed as an array of sentences in the Dialog class, which are created using the inspector. The class script is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialog
{
//this passes to DIalogController whenever we start a new dialog
//has all info we need for a dialog
public string name;
[TextArea(1, 10)] //specifies minimum and maximum amount of lines the text uses
public string[] sentences;
}
Does that help? Perhaps the way I have the dialog set up does not work with an event action in the way I have it set up.
So you have field like public Dialog someDialog;
somewhere.
it’s in MonoBehaviour or ScriptableObject?
You need to pass it into PlayerBroker.CallStartDialog(someDialog);
in order to start it.
Okay, I think I am starting to get it now. So the text I am writing for the dialogue needs to be referenced somewhere.
Am I incorrect in thinking they are referenced here:
public class DialogController : MonoBehaviour
{
public Text nameText;
public Text dialogText;
private Queue<string> sentences;
And then defined here (line 44):
dialogText.text = sentence;
And created using the inspector, since its public Text dialogText.
Is an easier way to do this using a ScriptableObject then to house all the dialogue instead of using a public variable defined in the inspector?
My apologies for possibly making this a little confusing, for this project I took scripts I had previously used for dialogue and are now reworking them to use this event action system in order to try and de-couple it, so I may be a bit turned around on how things need to be referenced.
public Text dialogText;```
just a UI elements that display text.
```private Queue<string> sentences;```
is just storing lines only for current dialogue to process them.
You need actual Dialog instance in order to work with it.
For test, make component and attach in to any gameobject in scene and fill data in inspector
```csharp
public class TestDialogue : MonoBehaviour
{
public Dialog d;
void Start()
{
PlayerBroker.CallStartDialog(d);
}
}
Okay I created the TestDialogue script and attached it to an object, which let me edit in the inspector. However, I still get the error “There is no argument given that corresponds to the required formal parameter ‘d’ of ‘PlayerBroker.CallStartDialog(Dialog)’” Is this because the DialogTrigger script needs a reference to the TestDialogue script I created? ANd if so, how can I do that without coupling the two scripts with something like ‘FindObjectOfType’ (since that is what I am trying to avoid)?
Remove PlayerBroker.CallStartDialog(Dialog);
from wherever you calling it for now. You should pass a variable but you pass a class type.
You already have it in TestDialogue PlayerBroker.CallStartDialog(d);
Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!
You can fix your own typing mistakes. Here’s how:
Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
the description of the error itself (google this; you are NEVER the first one!)
the file it occurred in (critical!)
the line number and character position (the two numbers in parentheses)
also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?