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)
- This topic has 33 replies, 3 voices, and was last updated 4 months, 3 weeks ago by
Jonathan Teo.
-
AuthorPosts
-
March 31, 2025 at 10:41 pm #17722::
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”.
April 1, 2025 at 5:00 am #17730::Is your hearts defined in NPCRelationshipState as such: public float Hearts { get { return friendshipPoints / 250; } }
April 1, 2025 at 5:39 am #17731::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; } } }
April 1, 2025 at 6:02 am #17732::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)}"); }
April 1, 2025 at 4:52 pm #17739::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.”); }”
April 2, 2025 at 7:05 am #17766::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 hereApril 2, 2025 at 7:07 pm #17797::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?
April 2, 2025 at 7:11 pm #17798::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.
April 5, 2025 at 6:08 am #17829::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?
April 5, 2025 at 6:34 am #17830::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.
April 5, 2025 at 6:38 am #17831::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.
April 5, 2025 at 6:39 am #17832::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.
April 5, 2025 at 6:50 am #17833::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 😉
April 7, 2025 at 5:52 pm #17856 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: