Forum begins after the advertisement:


[Part 32] NPC Dialogue Conditions (specifically Jeff & Ben)

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part 32] NPC Dialogue Conditions (specifically Jeff & Ben)

Viewing 14 posts - 21 through 34 (of 34 total)
  • Author
    Posts
  • #17722
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Yes, I’m loading a new game every time. After the first cutscene with Ben, I also do get a relationship saved, but for some reason it has issues obtaining the correct value. Since you say that nothing is wrong with my scripts, I’m deeply confused at the cause of this. I certainly believe that the condition “NPCRelationship_Ben.Hearts, int, >=, 2” is the correct setup for the condition. It just keeps saying the error: “Object reference not set to an instance of an object GameBlackboard.TryGetValue[T] (System.String key, T& value) (at Assets/Scripts/Game State/GameBlackboard.cs:118)” If it is to any help, when I click the error, it highlights the “Player” object inside of the “Essentials” object. The interaction works if I just delete that singular condition, however, I now also get the error: “No such value AnimalCount found”.

    #17730
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    Is your hearts defined in NPCRelationshipState as such: public float Hearts { get { return friendshipPoints / 250; } }

    #17731
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Yes, that’s exactly how it’s defined. This is my entire NPCRelationshipState script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    [System.Serializable]
    public class NPCRelationshipState
    {
        public string name;
        public int friendshipPoints;
    
        public bool hasTalkedToday;
        public bool giftGivenToday;
    
        public NPCRelationshipState(string name, int friendshipPoints)
        {
            this.name = name;
            this.friendshipPoints = friendshipPoints;
        }
    
        public NPCRelationshipState(string name)
        {
            this.name = name;
            friendshipPoints = 0;
        }
    
        //Ever 250 friendship points is a heart
        public float Hearts
        {
            get { return friendshipPoints / 250; }
        }
    
    }
    #17732
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    In that case Add debug logging in the GetNestedObject method to see exactly what’s failing:

    object GetNestedObject(string keys, object parent)
    {
        // Add at beginning of method
        Debug.Log($"GetNestedObject trying to access '{keys}' on object type {parent?.GetType().Name ?? "null"}");
    
        // Rest of method...
    
        // Add before getting property/field
        Debug.Log($"Looking for '{keyPart}' in {currentObject.GetType().Name}");
    
        // Add after getting property value
        Debug.Log($"Property value: {property.GetValue(currentObject)}");
    }
    #17739
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Thanks. So, when trying that (I also had to rename the “Debug()”-function and all the calls to it to avoid an error), it says the following: Ben: Debug.Log 1: “GetNestedObject trying to access ‘Hearts’ on object type NPCRelationshipState” Debug.Log 2: “Looking for ‘Hearts’ in NPCRelationshipState” Debug.Log 3: says “Property not found when this is used: ” if (property != null) { Debug.Log($”Property value: {property.GetValue(currentObject)}”); } else { Debug.Log(“Property not found.”); }” Abby: Debug.Log 1: “GetNestedObject trying to access ‘Hearts’ on object type NPCRelationshipState” Debug.Log 2: “Looking for ‘Hearts’ in NPCRelationshipState” Debug.Log 3: says “Property not found when this is used: ” if (property != null) { Debug.Log($”Property value: {property.GetValue(currentObject)}”); } else { Debug.Log(“Property not found.”); }”

    #17766
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    If you were to change the condition to NPCRelationship_Ben.friendshipPoints >= 500, do you get an error?

    Also change your debug function to output more details about what’s being stored on the blackboard:

    public void DebugDetailed(string context = "")
    {
        UnityEngine.Debug.Log($"===== BLACKBOARD DEBUG ({context}) =====");
    
        foreach(var entry in entries)
        {
            string key = entry.Key;
            object value = entry.Value;
    
            if (value == null)
            {
                UnityEngine.Debug.Log($"Key: {key} | Value: NULL");
                continue;
            }
    
            string valueType = value.GetType().Name;
            string valueStr;
    
            // More detailed output for complex types
            if (value is NPCRelationshipState npcRel)
            {
                valueStr = $"NPCRelationshipState[name={npcRel.name}, friendshipPoints={npcRel.friendshipPoints}, Hearts={npcRel.Hearts}, talked={npcRel.hasTalkedToday}, gift={npcRel.giftGivenToday}]";
            }
            else
            {
                valueStr = value.ToString();
            }
    
            UnityEngine.Debug.Log($"Key: {key} | Type: {valueType} | Value: {valueStr}");
        }
        UnityEngine.Debug.Log("===== END BLACKBOARD DEBUG =====");
    }

    And if it helps you can include the context when the blackboard values have changed by having their calls like this blackboard.DebugDetailed($"After unlocking {character.name}"); And post your output here

    #17797
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Thanks! When trying NPCRelationship_Ben.friendshipPoints >= 500 I get the exact same error as with >= 2: “InvalidCastException: Specified cast is not valid. GameBlackboard.TryGetValue[T] (System.String key, T& value) (at Assets/Scripts/Game State/GameBlackboard.cs:118)”

    The new debug function outputs this when attempting to talk to Ben in the Yodel Ranch (where the bug happens): “===== BLACKBOARD DEBUG () =====” (I didn’t include the context) “Key: Location | Type: Location | Value: YodelRanch” “Key: CurrentlyIndoor | Type: Boolean | Value: True” “Key: Timestamp | Type: GameTimestamp | Value: GameTimestamp” “Key: CutsceneIntroduction | Type: Boolean | Value: True” “Key: PlayerName | Type: String | Value: Bob” “Key: NPCRelationship_Ben | Type: NPCRelationshipState | Value: NPCRelationshipState[name=Ben, friendshipPoints=0, “Hearts=0, talked=False, gift=False]” “===== END BLACKBOARD DEBUG =====”

    When attempting to talk to Abby, it says: “===== BLACKBOARD DEBUG () =====” (I didn’t include the context) “Key: Location | Type: Location | Value: Town” “Key: CurrentlyIndoor | Type: Boolean | Value: False” “Key: Timestamp | Type: GameTimestamp | Value: GameTimestamp” “Key: CutsceneIntroduction | Type: Boolean | Value: True” “Key: PlayerName | Type: String | Value: Bob” “Key: NPCRelationship_Ben | Type: NPCRelationshipState | Value: NPCRelationshipState[name=Ben, friendshipPoints=0, Hearts=0, talked=False, gift=False]” “Key: NPCRelationship_Abby | Type: NPCRelationshipState | Value: NPCRelationshipState[name=Abby, friendshipPoints=20, Hearts=0, talked=True, gift=False]” “===== END BLACKBOARD DEBUG =====”

    It seems that all the first dialogues work, but thereafter, no dialogue can be had. It also for some reason say that “CutsceneIntroduction” is “True”. Does that mean that it thinks I’m still in the cutscene or what?

    #17798
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    When avoiding the cutscene (setting the condition for it to be “Location >= 6”), making the debug function not mention that it is in a cutscene, it actually still doesn’t work.

    #17829
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    Your location should be = 6, not >= 6 as it is a discrete value.

    Would it work if you changed the Value Type of the Hearts/Friendship as a float value instead of an Int?

    What version of Unity are you using?

    #17830
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Hey. So, the reason I used the condition “Location >= 6” in the Introduction cutscene just was to test if it was the cutscene that caused the problem (which it wasn’t), and I’ve changed it back now.

    Changing the Value Type of the friendshipPoints to a float value instead of an int didn’t actually do anything.

    I’m on verson 6000.0.42f1.

    #17831
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    OMG, I misunderstood your suggestion and tried changing the value in the “NPCRelationshipState”. Changing the Value Type to float in Ben and Abby’s dialogue finally fixed it… Thank you 😅 The most frustrating part is how simple of a fix it was haha.

    #17832
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    OMG, I misunderstood your suggestion and tried changing the value in the “NPCRelationshipState”. Changing the Value Type to float in Ben and Abby’s dialogue finally fixed it… Thank you 😅 The most frustrating part is how simple of a fix it actually is haha.

    #17833
    Raunsamsing
    Level 5
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Now, the final error is this: “No such value AnimalCount found. UnityEngine.Debug:LogError (object)” Which occurs every time I try talking to Ben. At least it doesn’t deny me from speaking to him at all, but I am pretty sure it won’t register it, if I actually had some animals. How can I fix this? Tell me if I need to show you some more scripts 😉

    #17856
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    You can just leave that error be for now, it also pops up on our end

Viewing 14 posts - 21 through 34 (of 34 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: