This is fairly difficult to explain. I am working on a text adventure at the moment and I am parsing input from a player.
When a player enters input, there can be two possible subjects (let's call them subject1 and subject2). For example, in the following: "hit door with stick", subject1 is "door" and subject2 is "stick".
In this case, both door and stick are of the type "Item". There can also be subjects of the type "Character".
The problem is that if I parse items, then characters, the item parse will find the item as subject1 even if it's actually the second subject. The code I am using looks like this:
public static void ParseForSubjects(string Input, Player CurrentPlayer, ref object Subject1, ref object Subject2)
{
// This method doesn't really work properly, as it looks up Inventory items, Environment items and then Characters in order
// when it may well be that a character is subject1 and an item is subject2, but they will be reversed because of the parsing order
Input = Input.ToLower();
// Parse items in Player inventory
foreach (Item InventoryItem in CurrentPlayer.Inventory)
{
if (Input.Contains(InventoryItem.Name.ToLower()))
{
if (Subject1 == null)
{
Subject1 = InventoryItem;
}
else
{
Subject2 = InventoryItem;
}
}
}
// Parse items in environment
foreach (Item EnvironmentalItem in CurrentPlayer.CurrentArea.Items)
{
if (Input.Contains(EnvironmentalItem.Name.ToLower()))
{
if (Subject1 == null)
{
Subject1 = EnvironmentalItem;
}
else
{
Subject2 = EnvironmentalItem;
}
}
}
// Parse present characters
foreach (Character PresentCharacter in CurrentPlayer.CurrentArea.Characters)
{
if (Input.Contains(PresentCharacter.Name.ToLower()))
{
if (Subject1 == null)
{
Subject1 = PresentCharacter;
}
else
{
Subject2 = PresentCharacter;
}
}
}
}
I'm sure if this is really clear enough. Basically regardless of the type, I need subject1 to be the first subject in the Input string and subject2 to be the second subject in the Input string.
Feel free to ask questions, this probably isn't 100% clear.