- Home /
Check slected mission status in inspector
Hi, I'm working in an adventure game where multiple missions can be done and some NPC will react to you different if you complete a concret one. So I need my NPC to check if the player has this mission as acomplished or not but there will be 100+ of tasks to do so I would need to expose the mission to check in the inspector (var missionToCheck = 1; or something)and type directly the one to check in every NPC. Missions are stored in MissionStatus.js as booleans (var mission1 = false, var mission2 = false, etc) and when one is complete it turns true in this script. How to tell my NPC wich boolean to check? If I use a String in the inspector and type mission1 for instance it doesn't work (or I made a mistake) cause it send a String to check and I need to check the boolean. When the script checks if the String is true or false allways returns true cause it has a String, it doesn't check if the String says mission1 or mission22 and then check for the boolean, and that's what I need.
Sorry if it's too long and for my english. Thanks in advance
Answer by SilverTabby · Jul 03, 2011 at 08:32 PM
It looks like you are looking for a Hashtable. A hashtable stores a variable number of key and value pairs.
A key is an object that is used like an address, usually a string like a user name: "guest1", "Leroy Jenkins", etc.
A value is what you are trying to store, this can be anything. An example is a password: "letmein", "p4ssw0rd", etc.
In the username/password example, whenever I input the username to the Hashtable, it will spit out the associated password. I can then check that against what the user said is their password and accept or deny a login request.
Hashtables are very processor efficient usually finding your stored value in a short, constant amount of time no matter how many objects are stored, but can become major memory hogs if there is too much information stored in them.
Here is an example of how to use a Hashtable (in C#, not javascript):
using UnityEngine;
using System.Collections;
using System.Collections.Hashtable;
Class Example : MonoBehaviour
{
int SIZE = 32;
void Start()
{
//creates an empty Hashtable that can store SIZE key/value pairs
Hashtable quests = new Hashtable(SIZE);
//will automatically resize if more than SIZE keys are added.
//you might need to use this if it says it can't find the namespace
//System.Collections.Hashtable ... = new System.Collections.Hashtable();
//creates a new key "MainQuest" and assigns the value -1 to it.
quests.Add("MainQuest", -1);
Debug.Log(quests["MainQuest"]);//outputs -1
//thorws an exception if a key already exists
try
{ quests.Add("MainQuest", 0);//throws an exception }
catch
{ Debug.Log("The value \"MainQuest\" already exists!"); }
Debug.Log(quests["MainQuest"]); //outputs -1
//can return and change values in this manner
quests["MainQuest"] = 1;
Debug.Log(quests["MainQuest"]);//outputs 1
//can create new key/value pairs in this manner
quests["SideQuest"] = 2;
Debug.Log(quests["SideQuest"]);//outputs 2
}
}
There is a lot more to Hastables than I have written here, but these are the basics to using them.
hi SilverTabby, thanks a lot for your answer. When I read it yesterday I didn't understand it cause I'm not familiar with hashtables so I was reading about them before reply. Now I have created some arrays with values and all the stuff but I have a doubt. The array it's created in the Start function so every time I hit Play it is created and working fine. So if I acces from other script and change some value cause a quest was finished it works and I can check this values from my NPC in order to react according whith them. But... if the array is recreated every time I strart the game the values are reset to the ones in the Start function. If I wanted to use booleans was cause they are easy to work with but I don't see the way to mantain the arrays values for playing next time without loosing the work done. Sorry for my limited knowledge but I will apreciatte some more help if you have the time. Thanks in advance. (I vote possitive your reply cause I assume it works and it's just my fault)
Sorry, stupid question. I can store data in a xml file. Thanks a lot for your help :)
Hashtables and other data structures like them are complex things. The only reason I knew about Hashtables is because I took a formal Computer Science course in school. If you don't understand it at first, that's normal - it's quite literally a concept people go to college to learn.
Just look it over, try a few things, take a break if you're having trouble, and then come back and try a few more things. You'll figure it out eventually.
Good luck with your quests!
Your answer
Follow this Question
Related Questions
Pass bool value from multiple objects to one file to check their values? 1 Answer
Problem with OnTriggerStay2D function 2 Answers
Boolean Uncheck Inspector if False 2 Answers
How do I check a boolean from another script in C#? 0 Answers
Why would a boolean change when checked by another script? 1 Answer