- Home /
How to make an inventory for my text advenutre?
I am creating a text adventure game, and using strings and text instead of traditional objects. Below I have my script that handles all events/searches for items in game (It probably looks very intimidating since it is made to work for many different scenarios).
What I would like to happen when an "item" (string) is found in game, for it to be added to the inventory, which would be a separate scene that can be reached from any scene, but the inventory contents itself would be an object in every scene, for use in gathering items/selling/buying etc. Here is my code and how I load my scenes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//IMPORTANT NOTE TO SELF: REPLACE ITEMSFOUND LIST WITH INVENTORY LIST, SO THAT ITEMS MAY BE ADDED TO INVENTORY
//NOTES ON THIS SCRIPT: this script may seem uneccesary, and is fairly complicated in the number of public variables being thrown about,
//but I am using many public variables to maximize reusability and in the long run reduce the file size of this project.
public class itemGenerator : MonoBehaviour {
//sound plays on find items
public AudioSource items;
//sound plays on finding nothing
public AudioSource noItems;
//The following 3 strings are so that I may use this script for other items in the future
//item1 to find
public string item1;
//item2 to find
public string item2;
//item3 to find
public string item3;
//text in the button to continue may change depending on the situation, these strings handle that
public string textInButton0;
public string textInButton1;
//allows me to drag any text from my scene to use as the textInButton text
public Text textInButton;
//allows me to drag any text from my scene to use as the displayItemsFound text
public Text displayItemsFound;
//list to display items found
public List<string> itemsFound = new List<string>();
//this will handle how many items will be found during each run of the script
int numberOfItems;
//makes sure the right amount of items is spawned
int itemsSpawned;
//uses integer values and assigns them to a string name for the items
int itemType;
//min range is usually set to 0, unless I always want the player to find an item or have an event occur
public int minRange;
//max range is equal to one more than the number of items I want
public int maxRange;
//these options are made public so that I may use this script for different items in different scenes
//startStatement allows me to manipulate what the text says before displaying items/events
public string startStatement;
//the 0th item which is what will display if nothing is found
public string itemType0;
//first item/event to add to the displayItemsFound text
public string itemType1;
//second item/event to add to the displayItemsFound text
public string itemType2;
//third item/event to add to the displayItemsFound text
public string itemType3;
//this int lets me choose how many times to increment the amount of itemsSpawned, which will make the function itemTypes run more or less
public int itemSpawnedNumberToAdd;
void Start () {
//the amount of items spawned will be set to zero
itemsSpawned = 0;
//the number of items found on each run of the scene will be chosen here
numberOfItems = Random.Range(minRange,maxRange);
//if one or more items is found, the itemTypes function is run
//a positive sound is played
if (numberOfItems >= 1) {
textInButton.text = textInButton1;
displayItemsFound.text += (startStatement);
itemTypes ();
}
//if no items or events occur, the game notifies the player accordingly via the itemType0 string, and the button text is changed as well
//a negative sound is played
if (numberOfItems == 0){
displayItemsFound.text = (itemType0);
textInButton.text = textInButton0;
noItems.Play ();
}
}
//this function handles which items (strings) are displayed, and how many
//remember that this is a text adventure, so I may use strings instead of instantiating physical items via prefabs
void itemTypes(){
//if the amount of items spawned is less than the required number of items to be found, throw out a random number in the range
//the random number will decide which items are found
if (itemsSpawned < numberOfItems) {
itemType = Random.Range (minRange, maxRange);
items.Play ();
//if the itemType int equals 1, "spawn" the item assigned to itemType1 and add it to the items list
if (itemType == 1) {
itemsFound.Add (itemType1);
displayItemsFound.text += (itemType1);
//itemType
itemsSpawned = itemsSpawned += itemSpawnedNumberToAdd;
//itemSpawned++
}
//if the itemType int equals 2, "spawn" the item assigned to itemType2 and add it to the items list
if (itemType == 2) {
itemsFound.Add (itemType2);
displayItemsFound.text += (itemType2);
itemsSpawned = itemsSpawned += itemSpawnedNumberToAdd;
}
//if the itemType int equals 3, "spawn" the item assigned to itemType1 and add it to the items list
if (itemType == 3) {
itemsFound.Add (itemType3);
displayItemsFound.text += (itemType3);
itemsSpawned = itemsSpawned += itemSpawnedNumberToAdd;
}
//runs the itemTypes script again, this will loop until the number of items spawned is greater than the amount of items needed
//the number of itemsSpawned will equal one more than needed, but the function will not run again at this point
itemTypes ();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class buttonScript : MonoBehaviour {
public string sceneToLoad;
void OnMouseDown(){
Application.LoadLevel (sceneToLoad);
}
}
Your answer
Follow this Question
Related Questions
How i can make a script-made button interactable? 0 Answers
Creating a custom List or Collection 1 Answer
A node in a childnode? 1 Answer
Equipting a Item/Weapon 1 Answer
Which is better for an inventory system, Array or List? 0 Answers