How can I check value stored in a list when pressing button with the same name?
I've assigned how much a person would like to do x activity and have it in a list. Using buttons you can ask the npc what it wants to do from given activities. How would you make it so each button would check the value the npc has for that activity?
This is not from the activities, but its done the same way: gamingTypes.Add (new Gaming ("Board Games", Random.Range(-5, 5)));
How can I make so if the button that says "Running", checks the script for the value the character has in running?
EIT: I have some activities like the ones below. As a start, I'm making it so it generates how much a npc likes those activities, and you can ask the npc if it wants to do X activity. I'm doing it using buttons, with all the options in the screen, and I wanted to try to make it so, if the player clicks on Running, it would look for "Running", on the list, and get the value. How should I go about this? Sorry if this is too basic, had been a while since I programmed the last time:
activity.Add (new Activities ("Fishing", Random.Range(-5, 5)));
activity.Add (new Activities ("Running", Random.Range(-5, 5)));
activity.Add (new Activities ("Hunting", Random.Range(-5, 5)));
Something like this perhaps:
int value = ga$$anonymous$$gTypes.Single(x => x.name == button.name).value;
Since you have provided no code, I can only guess as to what you have called your variables.
Sorry, thanks anyway. Edited the question explaining properly and adding some code
please post your code with your list so we can help
Sorry, I'm stupid. Edited the question. Hope it helps!
This is simple, yet the answer is not simple. You need to create the logic for this system to work, it's not some miraculously built-in thing just because you read it on a board game tutorial.
What I tend to do for displaying dialog is creating a class that is a dialog entry template, a dialog controller, and instantiate the dialogs as needed. So lets say your dialog entries can contain an activity, that would be included in the template class, then define it during instantiation. $$anonymous$$eep the activities separate. Like this. (this is a basic example mockup, don't expect it to work with copy and paste)
public class DialogEntry : $$anonymous$$onoBehaviour
{
public Text text;
public Activity activity;
public void OnDialogSelected()
{
DialogController.Instance.RegisterSelection(this);
}
}
and
public class DialogController : $$anonymous$$onoBehaviour
{
public static DialogController Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<DialogController>();
}
return instance;
}
private set
{
instance = value;
}
}
private static DiaogController instance;
public DialogEntry template;
public RectTransform dialogAnchor;
public void Initialize(Dialog dialog)
{
foreach (DialogActivityPair pair in dialog)
{
DialogEntry entry = Instantiate(template, dialogAnchor);
entry.text.text = pair.text;
entry.activity = pair.activity;
}
}
public void RegisterSelection(DialogEntry entry)
{
if (entry.activity == someExpectedActivity)
{
//Do something based on expected activity.
}
}
}
Answer by Pinkuboxu · May 03, 2018 at 01:52 PM
@tormentoarmagedoom Raises a good idea but didn't really supply an implementation. Also, I think a Dictionary would simplify this greatly.
A very simple version but this is the code you would put on the NPC:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Activity : MonoBehaviour {
Dictionary<string, int> activities =
new Dictionary<string, int>();
void Start ()
{
activities.Add("Fishing", Random.Range(-5, 5));
activities.Add("Running", Random.Range(-5, 5));
activities.Add("Hunting", Random.Range(-5, 5));
}
public void GetActivity(string activity)
{
print("My " + activity + " is " + activities[activity]);
}
}
Of course you would probably want to change the class name to something else like ActivityPool and have a method that helped add activities to the NPC rather than just pop them in at start... among other things.
Here is how to set up a Button for use with this.
[1]: /storage/temp/116139-dictexamp1.png
You are right! Just checked unity's video on dictionaries, and, if I understood correctly, it allows me to access the information much easier and faster! Gonna keep learning about both rn, and will reply to the code and all that later.
I have a question tho, just so I know I'm getting this properly. Could I have NPC's stats, likes... stored in their own dictionaries... I'm guessing I'll also be able to edit the values... $$anonymous$$an, thanks a lot. I can kinda see it now. With lists it was kind of a mess thinking about accessing the information stored, but this way seems way better.
Btw, I'm guessing dictionaries are also better when storing a lot of information. Am I right assu$$anonymous$$g this?
Edit: I'm just wondering, should I give the random value as I was doing it (when adding it to the list/dictionary), or using For to go through the dictionary adding the value? I'm gonna have a lot of stats, so I don't know what'd be the best way
Gonna learn properly about it. Thanks a lot!
Yes, Lists are kinda/sorta just a bit of arbitrary arrays, and dictionaries are more of "Hashtables" which ins$$anonymous$$d of an integer index to grab the data... it can technically be any other type of data as a key to access another type of data. I think I'm ok in saying that the sky is the limit, kinda. You can use your own class/struct as a key or value, I think. You could have any data, or for Unity sake GameObject, point to another bit of data. Just remember $$anonymous$$ISS principle and don't get so bogged with stuff that you lose interest. As amazing as all of the new data types that exist, I feel it sometimes detracts from what we started with in low level coding. I'm not saying you have to learn any assembly to have a good time, but some core CSci is probably good medicine. There's always a really easy and simple way to do something, even before all of the candy we get in C#, there's just so many ways of doing things now that it's kind of $$anonymous$$d boggling. Good luck and I look forward to hearing what you come up with.
As for the other part, adding stats for NPCs or what not, I'd look into scriptable objects if you need a way to add persistant data that you input by hand, however if you need something more procedural/runtime generated the method you are doing is fine but I'd wrap it in a class or struct so it's more flexable and re-usable.
Answer by tormentoarmagedoom · May 03, 2018 at 11:20 AM
Good day.
Ypu should check the event trigger system. It allows you to execute a method with parameters, like the button name.
Bye
Your answer
Follow this Question
Related Questions
Store instantiated object in List 2 Answers
Getting the element that has the same element number on another list ? 2 Answers
Remembering list of camera positions is not working 0 Answers
Calling functions via buttons when cooldown finished 1 Answer
Show and hide more buttons after clicking different button 1 Answer