A few newbie scripting questions

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Avo
Posts: 6
Joined: Sat Mar 10, 2018 5:57 am

A few newbie scripting questions

Post by Avo »

Hi everyone, and thanks to the creator for a great tool.

So, I'm having a few questions, that I'm having a hard time figuring out, and I hope I might be able to get some help here :)

Context: My game is a 2D top down-ish game.

Question 1: How to change the cursor to something else when hovering over a "Usable" person with a conversation?
I can use the "Reticle" on the selector, but that just puts a sprite on top of the character (which also stays during the conversation, which isn't wanted) - Is there an event or some interface I can implement to get information about this? I guess something like OnUse, just before that.

Question 2: Is it possible to register a click on something "Usable" when you're too far away. Then by script walk there and trigger that conversation
I can't seem to find any event for this. Of course I will take care of the walking by myself in my own code, but first I quess I need some kind of event like OnUseTooFarAway(Transform actor) or something like that, and then save the information and trigger it manually when my character has walked there?

That's my questions for now. It's a pleasure to work with the dialogue system so far! :)
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: A few newbie scripting questions

Post by Abelius »

You should take a look at a plugin like Adventure Creator. It has the features you want and many, many more.
Unity 2019.4.9f1
Dialogue System 2.2.15
Avo
Posts: 6
Joined: Sat Mar 10, 2018 5:57 am

Re: A few newbie scripting questions

Post by Avo »

Abelius wrote: Sat Mar 10, 2018 7:18 am You should take a look at a plugin like Adventure Creator. It has the features you want and many, many more.
Hi Abelius - Thanks for the reply!

I've got Adventure Creator, but I don't think that's really suited for my game, since it's not really an adventure game. If no other possibility exist, then I might look into it, but it seems a lot of work to drag in a huge framework for those 2 things :)
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: A few newbie scripting questions

Post by Tony Li »

Adventure Creator is a hefty framework and another big tool to learn, but it's really handy for the tasks you asked about. The Dialogue System has pretty good Adventure Creator integration, too. It's still possible to do this without Adventure Creator, of course, but it requires some scripting. The first one's pretty easy, but most of the work of the second one is outside of the Dialogue System's scope and will require more code.

To change the cursor, hook into the Selector's OnSelectedUsable() and OnDeselectedUsable() UnityEvents, or the SelectedUsableObject and DeselectedUsableObject C# events. For example, add this script to the Usable object:

UsableCursor.cs

Code: Select all

using UnityEngine;

public class UsableCursor : MonoBehaviour
{
    public Texture2D cursor;
    public Vector2 hotspot = Vector2.zero;
    public CursorMode cursorMode = CursorMode.Auto;
}
And this script on the GameObject (e.g., player) that has the Selector:

SelectorCursor.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

[RequireComponent(typeof(Selector))]
public class SelectorCursor : MonoBehaviour
{
    public Texture2D defaultCursor;
    public Vector2 hotspot = Vector2.zero;
    public CursorMode cursorMode = CursorMode.Auto;

    private void Start()
    {
        Cursor.SetCursor(defaultCursor, hotspot, cursorMode);
    }

    void OnEnable()
    {
        var selector = GetComponent<Selector>();
        selector.SelectedUsableObject += OnSelectedUsable;
        selector.DeselectedUsableObject += OnDeselectedUsable;
    }

    void OnDisable()
    {
        var selector = GetComponent<Selector>();
        selector.SelectedUsableObject -= OnSelectedUsable;
        selector.DeselectedUsableObject -= OnDeselectedUsable;
    }

    private void OnSelectedUsable(Usable usable)
    {
        var usableCursor = usable.GetComponent<UsableCursor>();
        if (usableCursor != null)
        {
            Cursor.SetCursor(usableCursor.cursor, usableCursor.hotspot, usableCursor.cursorMode);
        }
    }

    private void OnDeselectedUsable(Usable usable)
    {
        Cursor.SetCursor(defaultCursor, hotspot, cursorMode);
    }
}
If you want to use two different cursors for a Usable target -- one when it's in range, one when it's too far to use -- then you can add two cursor variables to UsableCursor.cs. In SelectorCursor, add an Update method or coroutine to OnSelectedUsable that checks if the selector's CurrentDistance is under the usable's maxUseDistance.



To register a click when too far away, hook into the TooFarEvent() UnityEvent. You can use UnityEvent.AddListener to hook into this in a C# script if you don't want to assign it in the inspector. Your code that handles this event is responsible for moving the player within range and then sending OnUse to the usable:

Code: Select all

usable.SendMessage("OnUse", player.transform);
Avo
Posts: 6
Joined: Sat Mar 10, 2018 5:57 am

Re: A few newbie scripting questions

Post by Avo »

Thanks a lot Tony. I don't mind scripting at all, so that's no problem.

So this is the exact answer I was looking for, thanks! :)
Post Reply