- Home /
Scavenger Hunt List Bools Question
hey hey hey. I have an interesting problem I'm kinda struggling with a little: I'm working on a scavenger hunt scenario where the user can say something like: "We are looking for a rubber spider"
What happens is a bool called: lookingForRubberSpider = false Gets changed to true
Simple enough for that. On the flip side if you say: "Looks like we have found the rubber spider"
Then the lookingForRubberSpider bool gets changed back to false.
This is ideal and it works great.... but what I'm wondering is is there an alternate way for a scenario like this:
Lets say you have found the rubber spider and for some reason you were to say: "We are looking for a rubber spider" To somehow treat the situation as Well we found it once, why are we looking for it again??
Not sure if I made sense there?
My first initial thought was to perhaps set up a secondary bool called: SpiderFound
And perhaps have that bool referenced in order to be able to return with something like: "we already found a rubber spider, why are you looking for it again"
But if I do that I'm wondering how best to set the SpiderFound bool back to false....???
Maybe when the scavenger hunt is over perhaps with a statement like:
"That was a fun scavenger Hunt" SpiderFound = false
Any thoughts on ways to approach something like this?
O$$anonymous$$ After some experimentation I got this to work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScavHunt : $$anonymous$$onoBehaviour {
public Text LookingForText;
public Text FoundText;
private enum RubberSpiderQuest
{
not_started,
started,
complete
}
private RubberSpiderQuest myRubberSpiderQuest;
void Start (){
LookingForText.GetComponent<Text>().enabled = false;
FoundText.GetComponent<Text>().enabled = false;
}
// Update is called once per frame
void Update () {
if (myRubberSpiderQuest == RubberSpiderQuest.started)
{
LookingForText.GetComponent<Text>().enabled = true;
FoundText.GetComponent<Text>().enabled = false;
}
if (myRubberSpiderQuest == RubberSpiderQuest.complete)
{
LookingForText.GetComponent<Text>().enabled = false;
FoundText.GetComponent<Text>().enabled = true;
}
}
public void lookingForRubSpider()
{
myRubberSpiderQuest = RubberSpiderQuest.started;
}
public void foundRubSpider()
{
myRubberSpiderQuest = RubberSpiderQuest.complete;
}
}
Why don't you just an integer called numberOfSpidersFound. You can increment it how many times you want. You can test if it's greater than 0.
This is bad program$$anonymous$$g to specify things like that you are searching for a spider. I would suggest something along these lines.
first your enum, I usually just create an Enums.cs class and remove the class section of it so it only contains enums.
public enum QuestState
{
NotStarted,
Started.
Acquired,
Completed
}
Then your scavenger hunt items
[Serializable]
public class ScavengerHuntItem
{
public int id;
public QuestState state;
}
then your monobehaviour for the in-game items
public class Objective : $$anonymous$$onoBehaviour
{
public ScavengerHuntItem item;
public void CollectItem()
{
if (Hunt.Instance.itemsList.Contains[item.id))
{
if (Hunt.Instance.itemsList[item.id].state != QuestState.Acquired)
Hunt.Instance.itemsList[item.id].state = QuestState.Acquired;
}
}
}
Then your scavenger hunt class
public class Hunt : $$anonymous$$onoBehaviour
{
public static Hunt Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<Hunt>();
}
return _instance;
}
private set {_instance = value;}
}
private static hunt _instance;
public ScavengerHuntItem[] items;
private Dictionary<int, ScavengerHuntItem> itemsList;
void Awake()
{
Instance = this;
itemsList = new Dictionary<int, ScavengerHuntItem>();
foreach (ScavengerHuntItem item in items)
{
itemsList.Add(item.id, item);
}
}
}
Answer by Habitablaba · Jun 30, 2017 at 10:41 PM
One idea is to use a few enums to take care of this.
public enum QuestState
{
not_started,
started,
complete
}
public enum Quest
{
find_spider,
find_letter_opener,
find_wine_bottle,
find_dirty_sock,
... etc....
total
}
// elsewhere...
public QuestState[] Quests;
// elsewhere...
Quests = new QuestState[Quests.total]();
Then you could just use the Quest enum to index into the QuestState array and change the value to whatever state you needed for each individual quest.
Disclaimer: This is completely untested and barely thought through. It spans several files and I'm not even sure it will work, but it is an idea.
Thanks @Habitablaba,
This looks potentially awesome man :) Hmmm I've not had a lot of experience with those, I've heard of them but have never played with them much.... any chance you can expand on this a little it sounds like it's exactly what I might need.
I have 13 items on my scavenger hunt list. $$anonymous$$y first initial thought was to upon closing the app just have the bool get change to false in the player prefs....
But I was thinking more while the app is running, keeping track of it that way. So that if you found the spider, but for some strange reason, for which in my case there could be many, were to say you were looking for it again, then have my A.I. say something like: Why are we doing that again when we already have found the rubber spider. ;)
An enum can be thought of as aliasing a number. Every entry into the enum is just adding 1 to the previous value, unless explicitly told otherwise. So
public enum Numbers
{
zero,
one,
two,
three,
four
}
will give you Numbers.zero, Numbers.one, Numbers.two, Numbers.three, and Numbers.four, which will have the values 0, 1, 2, 3, and 4, respectively.
Ins$$anonymous$$d of an array of QuestStates, you could have an array of ints. and every time a spider is found, you could increment the value in the array at the index Quest.find_spider (or whatever). This way you can do a check to see if the value is > 0 and give the use a message about how they've done this already. You could even make the message change depending on how many spiders the user found
Thanks @Habitablaba, I think I get where you are going with this, so if I did something like:
public enum Spiders
{
1,2,3,4,5,
}
And then referenced it like this:
if (Spiders >0)
{
Debug.Log("You Already Found A Spider");
}
Am I getting that right or am I missing something? The item or items only needs to be found once, but the possibility of either deliberately or accidentally mentioning you found the spider can happen often though.
@Habitablaba I suppose to I might be able to do:
public enum RubberSpiderQuestState
{
not_started,
started,
complete
}
And then in my if statements that are checking the state do this?
if (RubberSpiderQuestState == complete)
{
Debug.Log("You Already Found The Spider, Why Are You Looking For It Again?");
}
And if I'm understanding you right I would set the sate with something like:??
I tell my A.I. "Hey pal, it looks like we found the Rubber Spider"
RubberSpiderQuestState = complete;
Does that seem right?
Ah, you'll have to forgive my ignorance with these, I've never used them before... I think if I'm understanding it right, I would set up my enum like this:
private enum RubberSpiderQuest
{
not_started
started
complet
}
private RubberSpiderQuest myRubberSpiderQuest;
And then for checking that state I would use:
if (myRubberSpiderQuest == RubberSpiderQuest.complete)
{
Debug.Log("You Already Found The Rubber Spider");
}
And then to set the state I would use:
RubberSpiderQuest = complete;
Does that seem correct? Anyone? $$anonymous$$ay just require some experimentation I guess. :/
Hey @zentaiguy
making an enum called Spiders is not what I was trying to drive you towards. You would have an enum that would be a collection of all the things you are trying to find in your game, and then a second enum that would track if the player had found that item or not. (and the second version where you keep track of the number of times they've found that item).
So lets say you want the user to find a spider, a chicken, a spoon, and a chainsaw. Your first enum would look something like this:
public enum ObjectsToFind
{
spider,
chicken,
spoon,
chainsaw
}
Now lets assume you want to keep track of how many times they've found a item. You'd have an array in another script (whatever script you're using to keep track of these things; a player controller, or a game manager...). It might look like this:
public int[] FoundObjects;
Then, when you find an item, let's say in this case, that they found a spider:
FoundObjects[ObjectsToFind.spider] += 1;
Now, you can look at the value in that index and check to see if it is greater than one. If it is, then they have found the spider already and you can send them a message.
Does this make sense to you ?
@ Habitablaba, yes that does make perfect sense, although The method of using: not_started, started and complete seemed to work pretty good, that way I can track each items state. I guess both work :)
yep, both are good. The argument I have for the int[] ins$$anonymous$$d of the status enum is that you can then change the message to the user as they find more of the same object.
If they've found the spider twice, you can tell them "Hey man, you did this already, go do something else." but if they found the spider five times, you can tell them "Hey man, seriously stop trying to find the spider, you got a big long list of stuff to find and the spider is only one thing on it. What are you, spider man?!?"
whereas if all you track is state, then all you know is that they've found it before.
Your answer
Follow this Question
Related Questions
How Do I check if a Bool was True a few moments ago 2 Answers
Could use some help on making my runaway/chase/follow/patrol script cooperate with each other 1 Answer
c# Ignoring conditional statement? 1 Answer
How can i reverse all the booleans in a method? 2 Answers
my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer