Quest counter and messaging

Announcements, support questions, and discussion for Quest Machine.
Post Reply
nicmar
Posts: 133
Joined: Wed Aug 21, 2019 2:39 am

Quest counter and messaging

Post by nicmar »

Hey Tony! I worked for a while with messaging and quests, but I don't quite get it. I want to do the following:

1) When a player gets an item "Cheese burger", set a counter in any quests that has a counter called "Cheese burger" to the amount of the player inventory.

I'm doing this when item is added, and it's received by my "QuestItemMessage" script below.

Code: Select all

MessageSystem.SendMessage(this, "SetCount", item.name, count);

Code: Select all

public class QuestItemMessage : MonoBehaviour, IMessageHandler {
	private void OnEnable() {
		MessageSystem.AddListener(this, "QuestItem", string.Empty);
		MessageSystem.AddListener(this, "SetCount", string.Empty);
	}

	private void OnDisable() {
		MessageSystem.RemoveListener(this);
	}

	public void OnMessage(MessageArgs messageArgs) {
		switch (messageArgs.message) {
			case "SetCount":
				SetQuestCounter(messageArgs.parameter,BlueGoo.Utils.Utils.ParseInt(messageArgs.values[0].ToString()));
				Debug.Log("SetCount: " + messageArgs.parameter + " " + messageArgs.values[0]);
				break;
			case "QuestItem":
				Debug.Log("QuestItem: " + messageArgs.parameter + " " + messageArgs.values[0]);
				break;
		}
	}

	private void SetQuestCounter(string counterName, int value) {
		foreach (var kvp in QuestMachine.GetAllQuestInstances()) {
			var quests = kvp.Value;
			if (quests == null) continue;

			for (int i = 0; i < quests.Count; i++) {
				var quest = quests[i];
				if (quest == null) continue;
				for (int j = 0; j < quest.counterList.Count; j++) {
					if (quest.counterList[j].name.ToString() == counterName) {
						quest.counterList[j].currentValue = value;
					}
				}
			}
		}
	}
}
I thought the setting in the image below would make it update the counter when it got the SetCount message, but it doesn't. It confuses me that it says "Set To Parameter" (which is "Cheese burger"). I want it to set to the value, which is "count" in the above code sample. How does this work?
Skärmavbild 2019-10-31 kl. 22.13.18.png
Skärmavbild 2019-10-31 kl. 22.13.18.png (22.94 KiB) Viewed 1810 times

2) When this happens, set a quest node successful (Condition: Counter: Cheese burger >= 1) I guess would work, if I could just set the counter.

3) When the quest is completed, add 1000 credits, and I solved this with a custom QuestAction.

So basically it's just (1) i need help with :) However it might not be the optimal way to do it like this, if there are hundreds of quest. So maybe there's a better way to have a condition that checks if the player has a specific amount of an item?

Thank you in advance!

-niclas


PS: A small "display bug" on CounterQuestCondition:117. In the debug log it says "QuestMachine" instead of "Quest Machine" that most other script to. Just a minor issue :) Also QuestSubasset:145 has this.

Code: Select all

if (Debug.isDebugBuild) Debug.LogWarning("QuestMachine: QuestCounterCondition.OnCounterChanged(" + StringField.GetStringValue(counter.name) + " = " + counter.currentValue + "): requiredCounterValue field is null. Please contact the developer.", quest);
Attachments
Skärmavbild 2019-10-31 kl. 21.35.01.png
Skärmavbild 2019-10-31 kl. 21.35.01.png (15.68 KiB) Viewed 1810 times
Working on SpaceChef - A wacky open world space western, featuring a chef with nothing to loose, after he loses everything.. ;) Follow our work on @BlueGooGames.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Quest counter and messaging

Post by Tony Li »

Hi Niclas,

I don't think you need the QuestItemMessage script.

The way your counter is set up (according to the first screenshot), you can run this one line of code to set the counter to 5:

Code: Select all

MessageSystem.SendMessage(this, "SetCount" "Cheese burger", 5);
The counter is already automatically listening for the "SetCount" + "Cheese burger" message. You don't need another script to also listen for the message.


p.s. - If any of your scripts are listening for Message System messages for other reasons, MessageArgs provides a helper property intValue to get the first value passed with the message (e.g., 5 in the example above). You don't have to do any special value casting like in your QuestItemMessage script. Just use messageArgs.intValue.

p.p.s. - Thanks for letting me know about the typos. I'll fix those in the next update.
nicmar
Posts: 133
Joined: Wed Aug 21, 2019 2:39 am

Re: Quest counter and messaging

Post by nicmar »

Hey, thanks for the reply and your comments!

I noticed that my script actually worked when I looked at it correctly. I seem to have looked at the Quest scriptable object (?) instead of the instance. And when I looked at the instance in the inspector, it didn't update until i clicked somewhere in the inspector.

However, without the script, and just the Sendmessage you suggested nothing happens:

Code: Select all

MessageSystem.SendMessage(this, "SetCount", item.name, count);
Is the "Value mode" and it's settings (see screenshot above) correctly setup for this?

I've triple checked for spellnings of "Cheese burger" that it's with a space, and a small b, etc.

With debug on, I get this in the logs but nothing happens with the currentValue:

Code: Select all

MessageSystem.SendMessage(sender=CharacterRPG (BlueGoo.InventorySystem.InfiniteInventory): SetCount,Cheese burger)
Do you know what could be wrong or what I should check?
Working on SpaceChef - A wacky open world space western, featuring a chef with nothing to loose, after he loses everything.. ;) Follow our work on @BlueGooGames.
nicmar
Posts: 133
Joined: Wed Aug 21, 2019 2:39 am

Re: Quest counter and messaging

Post by nicmar »

By the way, you didn't have a comma in your sample code, but I guess that was an error? :)
Working on SpaceChef - A wacky open world space western, featuring a chef with nothing to loose, after he loses everything.. ;) Follow our work on @BlueGooGames.
nicmar
Posts: 133
Joined: Wed Aug 21, 2019 2:39 am

Re: Quest counter and messaging

Post by nicmar »

By the way, the upside to my script, is that I don't have to set up messages for each quest, just the counters, and as long as it matches the item name, it will be updated automatically. So as it's currently working, i'm currently happy. Will look into the int-thing you mentioned though :)
Working on SpaceChef - A wacky open world space western, featuring a chef with nothing to loose, after he loses everything.. ;) Follow our work on @BlueGooGames.
nicmar
Posts: 133
Joined: Wed Aug 21, 2019 2:39 am

Re: Quest counter and messaging

Post by nicmar »

I just thought of the downside though, that if I have like 20 quests, and each item added or deleted would loop through all quests and their counters, might take a little cpu. Maybe not a big issue? :)
Working on SpaceChef - A wacky open world space western, featuring a chef with nothing to loose, after he loses everything.. ;) Follow our work on @BlueGooGames.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Quest counter and messaging

Post by Tony Li »

Unlikely to be an issue. Let's say the player has 20 quests, and each quest has 3 item counters. That's a loop of 60 iterations, which is likely to be almost imperceptible to the profiler, must less any real human eye.
Post Reply