Hi, I've spent some more time with the documentation, but I'm breaking my head on this a bit. I went the ItemName route.
I've managed to add assets to the database, using a QuestData scriptable object I have made.
It creates a Quest, gives it a name, and adds a requiredItem, requiredAmount and currentAmount as variables.
It creates it the following way: The name of the Quest asset in the database is QuestGiver.QuestName,
and if there's an item --> the variable name would be QuestGiver.QuestName.ItemName, which it would take from the InventoryItem class, and for each item variable there's a variable for requiredAmount and currentAmount, which are taken from the QuestData.
This is the OnValidate method on the QuestData script.
Code: Select all
private void OnValidate()
{
if (!QuestDatabase)
{
Debug.Log("No Quest Database for this Quest Asset.", this);
return;
}
if (InventoryItem)
{
RequiredItemName = InventoryItem.GetItemName();
}
if (String.IsNullOrEmpty(RequiredItemName) && IsItemAcquisition)
{
Debug.Log("Item Acquisition Quest has no item.");
return;
}
//Add Quest To Database
string consolidatedQuestName = $"{QuestGiver}.{QuestName}";
Debug.Log("Has database?: " + QuestDatabase);
if (!DialogueDatabase.ContainsName(QuestDatabase.items, consolidatedQuestName))
{
Item questDialogueDatabaseItem = new Item();
questDialogueDatabaseItem.fields = new List<Field>();
questDialogueDatabaseItem.IsItem = false;
questDialogueDatabaseItem.Name = consolidatedQuestName;
questDialogueDatabaseItem.Description = QuestDescription;
QuestDatabase.items.Add(questDialogueDatabaseItem);
QuestSystemItem = questDialogueDatabaseItem;
Debug.Log("Has Quest been added?: " +
DialogueDatabase.ContainsName(QuestDatabase.items, consolidatedQuestName));
Debug.Log("Quest " + consolidatedQuestName + " has been added to the current database");
}
//if Quest is ItemAcquisition
if (IsItemAcquisition)
{
var questItemName = consolidatedQuestName +"."+ RequiredItemName;
if (!DialogueDatabase.ContainsName(QuestDatabase.variables, questItemName))
{
Variable questItemNameVariable = new Variable();
questItemNameVariable.fields = new List<Field>();
questItemNameVariable.Type = FieldType.Text;
questItemNameVariable.Name = questItemName;
questItemNameVariable.InitialValue = RequiredItemName;
QuestDatabase.variables.Add(questItemNameVariable);
Debug.Log("Variable " + questItemNameVariable.Name +"with the initial value of " + questItemNameVariable.InitialValue);
Variable questItemRequiredNumber = new Variable();
questItemRequiredNumber.fields = new List<Field>();
questItemRequiredNumber.Type = FieldType.Number;
questItemRequiredNumber.Name = questItemName + "." + "RequiredAmount";
questItemRequiredNumber.InitialFloatValue = RequiredItemNumber;
QuestDatabase.variables.Add(questItemRequiredNumber);
Debug.Log("Variable " + questItemRequiredNumber.Name +"with the initial value of " + questItemNameVariable.InitialFloatValue);
Variable questItemCurrentItem = new Variable();
questItemCurrentItem.fields = new List<Field>();
questItemCurrentItem.Type = FieldType.Number;
questItemCurrentItem.Name = questItemName + "." + "CurrentAmount";
questItemCurrentItem.InitialFloatValue = 0f;
QuestDatabase.variables.Add(questItemCurrentItem);
Debug.Log("Variable " + questItemRequiredNumber.Name +"with the initial value of " + questItemNameVariable.InitialFloatValue);
}
}
}
In the Inventory I created the following methods:
Code: Select all
public bool QuestSystem_HasRequiredAmountOfItem(string itemName, double requiredAmount)
{
for (var i = 0; i < slots.Length; i++)
{
Debug.Log($"{slots[i].item.GetItemName()} & {itemName}");
if (slots[i].item.GetItemName() == itemName)
{
Debug.Log($"current items / required : {slots[i].number} , {requiredAmount}");
return (slots[i].number >= requiredAmount);
}
}
return false;
}
Code: Select all
public void QuestSystem_RemoveItem(string itemName, double number)
{
for (var i = 0; i < slots.Length; i++)
{
if (slots[i].item.GetItemName() == itemName)
{
RemoveFromSlot(i, (int)number);
}
}
}
And I managed to make to work with a bit of a trickery. I registered the methods for the dropmenu in this manner:
This way I can instead of typing or copy-pasting, just choose the variable from a drop-menu, even though this is not a type-safe way, to say the least, but if the naming conventions are right, it shouldn't be a problem. It works, but it's dirty.
Is there anything you would recommend to avoid the problems of this method? In the first quest that I made to test this it worked as expected -
QuestSystem_HasRequiredAmountOfItem(itemName, requiredAmount) returns the correct value and
QuestSystem_RemoveItem(string itemName, double number) removes the given amount.
My question is how can I set the value of CurrentAmount, or map the item quantity (InventorySlot.number) to a LUA variable/data record that I can then pass to the Quest Entry. I have a InvetoryUpdated event, that I want to set the variable then, but I'm not sure how to approach that.