Referencing multiple class array variable element in script
Hello everyone (and sorry if this is complicated/confusing, it's my first question on this site)! I'm trying to make a simple text-based adventure demo for my game design portfolio and I'm an amateur at best when it comes to coding. I'm using some old code from an online course and basically what I'm trying to do is have multiple item interactions in different rooms or target items while using the same item for usage. For this I tried having in the Item script, a class array variable named Interactions with at least two elements. Like this:
However, I tried making sense of the current code in order to make a specific array element run depending on the designed location, but to no avail. This are the classes that I used for item interaction:
Interaction.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Interaction
{
public Action action;
[TextArea]
public string response;
public List<Item>itemsToDisable = new List<Item>();
public List<Item>itemsToEnable = new List<Item>();
// public List<Item> connectionsToDisable = new List<Item>();
// public List<Item> connectionsToEnable = new List<Item>();
// not in current design to have item interaction that enables/disables location connectors
}
Item.cs
public class Item : MonoBehaviour
{
public string itemName;
[TextArea]
public string description;
public bool playerCanTake;
public bool itemEnabled = true;
public Interaction[] interactions;
public Item targetItem = null;
public bool InteractWith(GameController controller, string actionKeyword)
{
foreach (Interaction interaction in interactions)
{
// if bool statement on locations to execute a specific interaction element
if (interaction.action.keyword == actionKeyword)
{
foreach(Item disableItem in interaction.itemsToDisable)
disableItem.itemEnabled = false;
foreach (Item enableItem in interaction.itemsToEnable)
enableItem.itemEnabled = true;
controller.currentText.text = interaction.response;
return true;
}
}
return false;
}
}
Use.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Actions/Use")]
public class Use : Action
{
public override void RespondToInput(GameController controller, string noun)
{
//use items in room
if (UseItems(controller, controller.player.currentLocation.items, noun))
return;
//use item in inventory
if (UseItems(controller, controller.player.inventory, noun))
return;
controller.currentText.text = "I couldn't do that.";
}
private bool UseItems(GameController controller, List<Item> items, string noun)
{
foreach (Item item in items)
{
if (item.itemName == noun)
{
if (controller.player.CanUseItem(controller, item))
{ if (item.InteractWith(controller, "use"))
return true;
}
controller.currentText.text = "It didn't do anything.";
return true;
}
}
return false;
}
}
Player.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Location currentLocation;
public List<Item> inventory = new List<Item>();
public Interaction interaction;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public bool ChangeLocation(GameController controller, string connectionNoun)
{
Connection connection = currentLocation.GetConnection(connectionNoun);
if (connection != null)
{
if (connection.connectionEnabled)
{
currentLocation = connection.location;
return true;
}
}
return false;
}
internal bool CanUseItem(GameController controller, Item item)
{
if (item.targetItem == null)
return true;
if (HasItem(item))
return true;
if (currentLocation.HasItem(item))
return true;
return false;
}
private bool HasItem(Item itemToCheck)
{
foreach (Item item in inventory)
{
if (item == itemToCheck)
return true;
}
return false;
}
}
Does anyone know a solution for making this possible? If yes, much appreciated!