Referencing Actors and fields in custom script

Announcements, support questions, and discussion for the Dialogue System.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Referencing Actors and fields in custom script

Post by annathehank »

Hello again!

This time I'm working on a notification system for the map screen if the player has a conversation they can have with an NPC ready.

The thing I'm stuck on is figuring out what coding to use to pull Actor field data from the manager in the script.

I have two actor fields I added in the database, RelLvl that increases or decreases as you interact normally with NPCs, and TalkLvl that increases after you have a specific conversation with them. It's set up so that every ten points unlocks a new conversation, as long as the previous has already been activated.

This is what I have set up, I just don't know how to fill in the blanks to get the data from the manager synced up.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
using System.Security.Cryptography.X509Certificates;

public class charalert : MonoBehaviour
{//Alert notification to display on map screen
    public GameObject AlertNotif;

    void Start()
    {
        //what I assume pulls actor data from the manager)
        var NPCconvos = Code to pull Actor data;
        //cycling through each actor component)
        foreach (Actor in NPCconvos)
        {//Look to see if the level of relationship matches the conversation level player is on
            if ((RelLvl*10)+1 >= TalkLvl)
            {//display altert notification on map screen
                Actor.GetComponent<charalert>().AlertNotif.SetActive(true);
            }
        }
    }
Thank you in advance!
User avatar
Tony Li
Posts: 21986
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

Hi,

The list of actors is DialogueManager.masterDatabase.actors.

To get the runtime value of an actor's field, use DialogueLua.GetActorField.

Your script might look something like:

Code: Select all

public class charalert : MonoBehaviour
{//Alert notification to display on map screen
    public GameObject AlertNotif;

    void Start()
    {
        //(cycling through each actor component)
        foreach (Actor actor in DialogueManager.masterDatabase.actors)
        {//Look to see if the level of relationship matches the conversation level player is on
            int relLevel = DialogueLua.GetActorField(actor.Name, "RelLvl").asInt;
            int talkLevel =DialogueLua.GetActorField(actor.Name, "TalkLvl").asInt; 
            if ((RelLvl*10)+1 >= TalkLvl)
            {//display altert notification on map screen
                // You need to match up an Actor (which is just data in the dialogue database) with
                // a GameObject. If the GameObject has a DialogueActor component, you can use
                // CharacterInfo.GetRegisteredActorTransform:
                Transform actorTransform = CharacterInfo.GetRegisteredActorTransform(actor.Name);
                if (actorTransform != null)
                {
                    actorTransform.GetComponent<charalert>().AlertNotif.SetActive(true);
                }
        }
    }
}
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

Thank you for the help!
There doesn't appear to be any compiler errors, yet when I run the game at this scene, I get an error for this line of code

Code: Select all

foreach (Actor actor in DialogueManager.masterDatabase.actors)
That says: NullReferenceException: Object reference not set to an instance of an object

Do I need to define the first 'Actor' somewhere earlier?
User avatar
Tony Li
Posts: 21986
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

Does your scene have a Dialogue Manager GameObject? And is a dialogue database assigned to it?
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

It does, yes.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

I ran it again and the error just...went away...
So I guess it fixed itslef :lol:
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

However, it still doesn't seem to be turning on the alert.

Here's what the script looks like now (I realized I had the two values swapped)

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;

public class charalert : MonoBehaviour
{//Alert notification to display on map screen
    public GameObject AlertNotif;

    void Start()
    {
      
        //cycling through each actor component)
        foreach (Actor actor in DialogueManager.masterDatabase.actors)
        {//Look to see if the level of relationship matches the conversation level player is on
            int RelLvl = DialogueLua.GetActorField(actor.Name, "RelLvl").asInt;
            int TalkLvl = DialogueLua.GetActorField(actor.Name, "Talklvl").asInt;
            if ((TalkLvl*10)+1 >= RelLvl)
            {//display altert notification on map screen
               Transform actorTransform = PixelCrushers.DialogueSystem.CharacterInfo.GetRegisteredActorTransform(actor.Name);
                if (actorTransform != null)
                {
                    actorTransform.GetComponent<charalert>().AlertNotif.SetActive(true);
                }
               
            }
        }
    }
And here's the components of the GameObject it's attached to:
Image

I auto-set Elise's talk value to 1 and relationship value to 10, so 1*10+1 is greater than 10, but it's not setting the image active.
User avatar
Tony Li
Posts: 21986
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

Hi,

I recommend attaching the debugger. Set a breakpoint on the first line of that Start() method, and step through each line of code so you can check the values.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

I believe I followed all the steps to enter the debugging mode.

But...I'm not sure what values I'm looking for and where?

Image
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

Oop, found what I was doing wrong there.

This is where the issue appears. Image

It's not changing the talklvl to what it actually is set to when it's on Elise's name.

Image
Post Reply