Point and click dialogue

Hi,

I’m looking to implement a point and click style dialogue system in my game. The idea would be to see an NPC, click on them and a box of dialogue options appear. Does anyone know a good way to implement this kind of thing? I’m having trouble getting ym head around it.

Thanks in advance.

The most straightforward way to handle the UI aspect would probably be to use the built-in GUI system.

Other than that though, you’ll probably have to ask a more specific question. There are several parts to the problem you mention - detecting mouse clicks, handling the UI, managing dialog flow, etc. What part in particular do you need help with?

To be more specific, I was thinking about a GUI, one panel/box would hold, say, four selectable questions. There would be another box above it which would display text answers to the questions. The system would work by clicking on a character and the dialogue GUI (both boxes) appearing. It can be cleared from view by clicking outside of the GUI’s bounds.

Also (sorry for so many questions), I’m having a bit of trouble with my First Person Controller, in that the mouse isn’t centered when the game starts which means looking around can be a bit of a pain - is there any way to sort this out?

To clarify something about the GUI. The two things that are confusing me are, A) how to bring the Dialogue GUI up when a character has been clicked. (I know I can use OnMouseUp (), but I’m not sure what code I’d enter beneath it, i.e, so when the mouse has been clicked the GUI pops into view - would this just be “setVisible”?). B) Whether or not it will be possible to implement that ‘Answer’ box, where you can see the NPC response; I was thinking about creating an empty ‘Response’ String and a text box, then writing Switch statements that would change the string being displayed in the text box, depending on what question has been asked.

I created a quick GUI in Photoshop to use. The idea is that in the Questions box there will be say 3or 4 buttons with dialogue options. The Answers box will have a text box displaying a generic welcome, which will change when a Question Button is clicked. The whole GUI should only appear when an NPC is clicked on, and needs to disapear once the user has clicked outside of it.

I’m pretty sure I know how to set up the questions, what I don’t know how to do is the last part; controlling it’s visability with OnMouseUp.

This might provide a starting point for the code:

http://lemuria.org/projects/wiki/UnityConversationMaker

Hi Tom,

Thanks for the reply. I took a look at your code but still couldn’t find what I was looking for unfortunately. I’m just looking for someway to toggle the visibility of the GUI I made using the mouse.

Here’s a quick (and somewhat cursory) answer:

  1. Add a Boolean variable to your script indicating whether the GUI should be on or off.

  2. Set it to false or true as appropriate (e.g. in OnMouseDown()).

  3. In OnGUI(), create the GUI, but only if the aforementioned Boolean variable is true.

Thanks Jesse.

How do I create the GUI, though, This is the part I’m having trouble with; I understand how to make a GUI from scratch, but I don’t know how to reference a premade GUI texture in code. As I see it the code should look a little like this:

bool isClicked = false;

function OnGUI()
{
if(!isClicked) return;
//Return the 2D texture for my GUI
//Underneath this I code in the buttons, etc.
}
function OnMouseUp()
{
isClicked = true;
}

Then there would be the code for clicking the exit button. I think I have the gist of it, I just need to know what line of code to use to call the texture.

Check out the documentation; it includes a lot of examples (including examples of how to create specific types of controls).

I already have, as it happens. I still couldn’t find a solution to my problem: I’m not sure how to reference my custom made GUI image through code, or - having solved that - toggle it’s visibility via the mouse.

Well, you already posted some code that indicates you do in fact have an idea of how toggle the GUI’s visibility using the mouse.

As for the documentation, I think it covers all the controls that the GUI system supports, so you should be able to find what you’re looking for in there. (You might also check the scripting reference for the control functions, as most of them have multiple overloads.)

If you’re still having trouble with this, maybe you could try this. Forget about the mouse toggling for now, and just try to get the GUI on the screen the way you want it. If you have trouble with that, post your code and/or explain what exactly it is that you’re stuck on.

Hi guys,

I think I’ve solved it, I’m going to get onto coding the solution in a bit. I didn’t realise you could use GUI.Skin and then drag and drop the skin in the properties bar on the left.

I’m getting a weird error, it’s saying bool and boolean aren’t recognised?

This is the code:

private Boolean isClicked = false;

This is the error:

Assets/Scripts/dialogueScript.js(1,9): BCE0044: expecting function, found ‘Boolean’.

Post the exact error message, along with the script in its entirety, with a comment indicating which statement is generating the error.

private var isClicked : Boolean = false;
var dialogueSkin = GUISkin;

function OnGUI()
{
GUI.skin = dialogueSkin;
if(!isClicked) return;

//Return the 2D texture for my GUI

print (“dialogue GUI is displayed”);
}

function OnMouseUp()
{
isClicked = true;

The error is:

Assets/Scripts/dialogueScript.js(1,25): BCE0018: The name ‘Boolean’ does not denote a valid type (‘not found’). Did you mean ‘System.Boolean’?

I changed the code slightly, but the error is just as before.

It should be a lower-case ‘B’.

Thanks Jesse. Now I’m getting another error, though:

Assets/Scripts/dialogueScript.js(6,20): BCE0022: Cannot convert ‘System.Type’ to ‘UnityEngine.GUISkin’. I thinks its because of these two lines:

var dialogueSkin = GUISkin;

function OnGUI()
{
GUI.skin = dialogueSkin;
if(!isClicked) return;

Don’t worry, I fixed it. It should have been a colon instead of an equals sign for var dialogueSkin = GUISkin; .

I’ve thought the problem through and thought it would be best to do it like this:

private var isClicked : boolean = false;
var dialogueGUI : Texture2D;
var newSkin : GUISkin;

function dialogueMenu()
{

GUI.DrawTexture(Rect (0,0,700,450), dialogueGUI, ScaleMode.ScaleToFit, true, 1.0f);
}
function OnGUI()
{

GUI.skin = newSkin;

if(!isClicked) return;

dialogueMenu();

print (“dialogue GUI is displayed”);
}

function OnMouseUp()
{
isClicked = true;
}

Basically when the mouse is clicked OnGUI returns the dialogueMenu function which draw the texture and from there I’ll code buttons, etc.

This code won’t compile though, I’m getting another error:

Assets/Scripts/dialogueScript.js(11,10): BCE0044: expecting (, found ‘OnGUI’. I’m guessing it’s the line in bold.

God I must be tired, I left off a matching }. Again though, I’m getting more errors. One that keeps popping up:

ArgumentException: You are not allowed to call get_guiTexture when declaring a variable.
Move it to the line after without a variable declaration.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
dialogueScript…ctor () (at Assets/Scripts/dialogueScript.js:2)

I’ve tried coding this so many different way, even leaving out the image altogether, but I still can’t get it to take my code.

Can someone help me out and show me what it is I’m doing wrong, cause I don’t get it, and for some reason I can’t even draw out a basic GUI; which was going to be my last resort.